source: src/main/java/com/example/autopartz/controller/HomeController.java@ 9dcbf44

main
Last change on this file since 9dcbf44 was 9dcbf44, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 18 months ago

Added full functionality for changing delivery status,
added bootstrap for some pages

  • Property mode set to 100644
File size: 11.4 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.model.DTO.OrderInfo;
5import com.example.autopartz.model.manytomany.OrderContainsPart;
6import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
7import com.example.autopartz.model.views.DeliveriesInProgress;
8import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
9import com.example.autopartz.repository.*;
10import com.example.autopartz.service.*;
11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.*;
14
15import javax.servlet.http.HttpServletRequest;
16import javax.servlet.http.HttpServletResponse;
17import javax.servlet.http.HttpSession;
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.List;
21import java.util.Objects;
22
23@Controller
24@RequestMapping("/")
25public class HomeController {
26 private final LoginService loginService;
27 private final PartService partService;
28 private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
29 private final CarService carService;
30 private final CategoryService categoryService;
31 private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
32 private final WarehouseRepository warehouseRepository;
33 private final OrderContainsPartRepository orderContainsPartRepository;
34 private final OrderService orderService;
35 private final UserService userService;
36 private final DeliveriesInProgressRepository deliveriesInProgressRepository;
37 private final DeliveryService deliveryService;
38 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
39 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
40 OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository) {
41 this.loginService = loginService;
42 this.partService = partService;
43 this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
44 this.carService = carService;
45 this.categoryService = categoryService;
46 this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
47 this.warehouseRepository = warehouseRepository;
48 this.orderContainsPartRepository = orderContainsPartRepository;
49 this.orderService = orderService;
50 this.userService = userService;
51 this.deliveriesInProgressRepository = deliveriesInProgressRepository;
52 this.deliveryService = deliveryService;
53 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
54 }
55
56 @GetMapping()
57 public String getHomePage(Model model, HttpServletRequest request){
58 model.addAttribute("bodyContent","home");
59 model.addAttribute("user",request.getRemoteUser());
60 return "master-template";
61 }
62 @GetMapping("/products")
63 public String getProducts(Model model){
64 model.addAttribute("parts",partService.findAll());
65 model.addAttribute("cars",carService.findAll());
66 model.addAttribute("categories",categoryService.findAll());
67 model.addAttribute("bodyContent","products");
68 return "master-template";
69 }
70 @GetMapping("/services")
71 public String getServices(Model model){
72 model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
73 model.addAttribute("bodyContent","services");
74 return "master-template";
75 }
76 @GetMapping("/currentOrder")
77 public String getCurrentOrder(Model model,HttpSession session){
78 if(session.getAttribute("order")==null){
79 model.addAttribute("hasError",true);
80 model.addAttribute("error","Нарачката е празна");
81 }
82 else {
83 Order o = (Order) session.getAttribute("order");
84 model.addAttribute("hasError",false);
85 model.addAttribute("order",o);
86 model.addAttribute("parts",orderService.findById(o.getOrderid()).getPartList());
87 }
88 model.addAttribute("bodyContent","currentOrder");
89 return "master-template";
90 }
91 @GetMapping("/filtered")
92 public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
93 List<PartsForCarTypeAndCategory> tmp = partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category);
94 if(tmp.size()==0){
95 model.addAttribute("hasError",true);
96 model.addAttribute("error","Не постојат такви производи, обидете се повторно");
97 }
98 else {
99 model.addAttribute("hasError",false);
100 model.addAttribute("filtered", tmp);
101 }
102 model.addAttribute("bodyContent","filteredParts");
103 return "master-template";
104 }
105 @GetMapping("/login")
106 public String getLoginPage(Model model){
107 model.addAttribute("bodyContent","login");
108 return "master-template";
109 }
110 @GetMapping("/register")
111 public String getRegisterPage(Model model){
112 model.addAttribute("bodyContent","register");
113 return "master-template";
114 }
115 @PostMapping("/login")
116 public void handleLogin(@RequestParam String username, @RequestParam String password){
117 User u = loginService.login(username,password);
118 System.out.println(u.getName());
119 }
120 @PostMapping("/register")
121 public void handleRegister(@RequestParam String username, @RequestParam String name,
122 @RequestParam String password, @RequestParam String rpassword,
123 @RequestParam String email, @RequestParam String number,
124 @RequestParam String role, HttpServletResponse response, HttpSession session){
125 System.out.println(username + name + password + rpassword + email + number + role);
126 if(Objects.equals(role, "warehouseman")){
127 session.setAttribute("username", username);
128 session.setAttribute("name", name);
129 session.setAttribute("password", password);
130 session.setAttribute("rpassword", rpassword);
131 session.setAttribute("email", email);
132 session.setAttribute("number", number);
133 try {
134 response.sendRedirect("/registerWarehouseman");
135 } catch (IOException e) {
136 throw new RuntimeException(e);
137 }
138 }
139 else {
140 loginService.register(name, username, email, number, password, role);
141 try {
142 response.sendRedirect("/login");
143 } catch (IOException e) {
144 throw new RuntimeException(e);
145 }
146 }
147 }
148 @GetMapping("/registerWarehouseman")
149 public String getSelectPage(Model model){
150 model.addAttribute("locations",warehouseRepository.findAll());
151 model.addAttribute("bodyContent","selectWarehouse");
152 return "master-template";
153 }
154 @PostMapping("/finishRegister")
155 public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
156 System.out.println("here?");
157 String username = (String) session.getAttribute("username");
158 String name = (String) session.getAttribute("name");
159 String password = (String) session.getAttribute("password");
160 String email = (String) session.getAttribute("email");
161 String number = (String) session.getAttribute("number");
162 Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
163 loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
164 try {
165 response.sendRedirect("/login");
166 } catch (IOException e) {
167 throw new RuntimeException(e);
168 }
169 }
170 @GetMapping("/access_denied")
171 public String accessDenied(Model model){
172 model.addAttribute("bodyContent","access_denied");
173 return "master-template";
174 }
175 @GetMapping("/myWarehouse")
176 public String myWarehouse(Model model, HttpServletRequest request){
177 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
178 Warehouse warehouse = whm.getWarehouse();
179 List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getID_warehouse());
180 model.addAttribute("bodyContent","myWarehouse");
181 model.addAttribute("warehouse",warehouse);
182 model.addAttribute("parts", partIsInStockInWarehouseList);
183 return "master-template";
184 }
185 @GetMapping("myDeliveries")
186 public String myDeliveries(Model model, HttpServletRequest request){
187 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
188 List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
189 model.addAttribute("bodyContent","myDeliveries");
190 model.addAttribute("deliveries",deliveries);
191 return "master-template";
192 }
193 @GetMapping("myNextDeliveries")
194 public String myNextDeliveries(Model model, HttpServletRequest request){
195 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
196 List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
197 if(ldip.size()==0){
198 model.addAttribute("hasError",true);
199 model.addAttribute("error","Сите достави се завршени");
200 }
201 else {
202 model.addAttribute("hasError",false);
203 model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
204 }
205 model.addAttribute("bodyContent","myNextDeliveries");
206 return "master-template";
207 }
208 @PostMapping("/finishDelivery/{id}")
209 public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
210 Delivery d = deliveryService.findByOrder(orderService.findById(id));
211 d.setStatus("finished");
212 deliveryService.update(d);
213 try {
214 response.sendRedirect("/myDeliveries");
215 } catch (IOException e) {
216 throw new RuntimeException(e);
217 }
218 }
219 @GetMapping("/order/{id}")
220 public String getOrderInfo(@PathVariable Integer id, Model model){
221 List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
222 List<OrderInfo> partList = new ArrayList<>();
223 for (int i = 0; i < list.size(); i++) {
224 OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
225 list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
226 partList.add(oi);
227 }
228 model.addAttribute("parts",partList);
229 model.addAttribute("o",orderService.findById(id));
230 model.addAttribute("bodyContent","orderInfo");
231 return "master-template";
232 }
233}
Note: See TracBrowser for help on using the repository browser.