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

main
Last change on this file was 2a552fe, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 16 months ago

Added transactional methods to secure the database integrity and refactoring

  • Property mode set to 100644
File size: 15.7 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.DTO.CurrentOrderDTO;
4import com.example.autopartz.model.DTO.OrderInfo;
5import com.example.autopartz.model.*;
6import com.example.autopartz.model.manytomany.OrderContainsPart;
7import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
8import com.example.autopartz.model.views.DeliveriesInProgress;
9import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
10import com.example.autopartz.repository.*;
11import com.example.autopartz.service.*;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.web.bind.annotation.*;
15
16import javax.servlet.http.HttpServletRequest;
17import javax.servlet.http.HttpServletResponse;
18import javax.servlet.http.HttpSession;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Objects;
23import java.util.stream.Collectors;
24
25@Controller
26@RequestMapping("/")
27public class HomeController {
28 private final LoginService loginService;
29 private final PartService partService;
30 private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
31 private final CarService carService;
32 private final CategoryService categoryService;
33 private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
34 private final WarehouseRepository warehouseRepository;
35 private final OrderContainsPartRepository orderContainsPartRepository;
36 private final OrderService orderService;
37 private final UserService userService;
38 private final DeliveriesInProgressRepository deliveriesInProgressRepository;
39 private final DeliveryService deliveryService;
40 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
41 private final CarCategoryReportRepository carCategoryReportRepository;
42 private final PartManufacturersReportRepository partManufacturersReportRepository;
43 private final MostPurchasedPartRepository mostPurchasedPartRepository;
44 private final PriceService priceService;
45 private final WarehousemanReportRepository warehousemanReportRepository;
46 private final PartRepository partRepository;
47 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
48 OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, CarCategoryReportRepository carCategoryReportRepository, PartManufacturersReportRepository partManufacturersReportRepository, MostPurchasedPartRepository mostPurchasedPartRepository, PriceService priceService, WarehousemanReportRepository warehousemanReportRepository, PartRepository partRepository) {
49 this.loginService = loginService;
50 this.partService = partService;
51 this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
52 this.carService = carService;
53 this.categoryService = categoryService;
54 this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
55 this.warehouseRepository = warehouseRepository;
56 this.orderContainsPartRepository = orderContainsPartRepository;
57 this.orderService = orderService;
58 this.userService = userService;
59 this.deliveriesInProgressRepository = deliveriesInProgressRepository;
60 this.deliveryService = deliveryService;
61 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
62 this.carCategoryReportRepository = carCategoryReportRepository;
63 this.partManufacturersReportRepository = partManufacturersReportRepository;
64 this.mostPurchasedPartRepository = mostPurchasedPartRepository;
65 this.priceService = priceService;
66 this.warehousemanReportRepository = warehousemanReportRepository;
67 this.partRepository = partRepository;
68 }
69
70 @GetMapping()
71 public String getHomePage(Model model, HttpServletRequest request){
72 model.addAttribute("bodyContent","home");
73 model.addAttribute("user",request.getRemoteUser());
74 return "master-template";
75 }
76 @GetMapping("/products")
77 public String getProducts(Model model){
78 model.addAttribute("parts",partService.findAll());
79 model.addAttribute("cars",carService.findAll());
80 model.addAttribute("categories",categoryService.findAll());
81 model.addAttribute("bodyContent","products");
82 return "master-template";
83 }
84 @GetMapping("/services")
85 public String getServices(Model model){
86 model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
87 model.addAttribute("bodyContent","services");
88 return "master-template";
89 }
90 @GetMapping("/currentOrder")
91 public String getCurrentOrder(Model model,HttpSession session){
92 if(session.getAttribute("order")==null){
93 model.addAttribute("hasError",true);
94 model.addAttribute("error","Нарачката е празна");
95 }
96 else {
97 Order o = (Order) session.getAttribute("order");
98 model.addAttribute("hasError",false);
99 model.addAttribute("order",o);
100 List<CurrentOrderDTO> list = new ArrayList<>();
101 int total = 0;
102 List<OrderContainsPart> qList = orderContainsPartRepository.findAllByOrderid(o.getOrderid());
103 for (OrderContainsPart orderContainsPart : qList) {
104 int pr = orderContainsPart.getQuantity_order() *
105 priceService.findPriceForPart(partService.findById(orderContainsPart.getPartid())).stream().findFirst().get().getAmount();
106 CurrentOrderDTO temp = new CurrentOrderDTO(
107 partService.findById(orderContainsPart.getPartid()).getName(),
108 partService.findById(orderContainsPart.getPartid()).getManufacturer().getName(),
109 orderContainsPart.getQuantity_order(),
110 pr);
111 list.add(temp);
112 total += pr;
113 }
114 model.addAttribute("total",total);
115 model.addAttribute("parts",list);
116 }
117 model.addAttribute("bodyContent","currentOrder");
118 return "master-template";
119 }
120 @GetMapping("/filtered")
121 public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
122 List<PartsForCarTypeAndCategory> tmp = partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category);
123 if(tmp.size()==0){
124 model.addAttribute("hasError",true);
125 model.addAttribute("error","Не постојат такви производи, обидете се повторно");
126 }
127 else {
128 model.addAttribute("hasError",false);
129 model.addAttribute("filtered", tmp);
130 }
131 model.addAttribute("selectedCar","Previously selected : "+cartype);
132 model.addAttribute("selectedCategory","Previously selected : " + category);
133 model.addAttribute("cars",carService.findAll());
134 model.addAttribute("categories",categoryService.findAll());
135 model.addAttribute("bodyContent","filteredParts");
136 return "master-template";
137 }
138 @GetMapping("/login")
139 public String getLoginPage(Model model){
140 model.addAttribute("bodyContent","login");
141 return "master-template";
142 }
143 @GetMapping("/register")
144 public String getRegisterPage(Model model){
145 model.addAttribute("bodyContent","register");
146 return "master-template";
147 }
148 @PostMapping("/login")
149 public void handleLogin(@RequestParam String username, @RequestParam String password){
150 User u = loginService.login(username,password);
151 System.out.println(u.getName());
152 }
153 @PostMapping("/register")
154 public void handleRegister(@RequestParam String username, @RequestParam String name,
155 @RequestParam String password, @RequestParam String rpassword,
156 @RequestParam String email, @RequestParam String number,
157 @RequestParam String role, HttpServletResponse response, HttpSession session){
158 System.out.println(username + name + password + rpassword + email + number + role);
159 if(Objects.equals(role, "warehouseman")){
160 session.setAttribute("username", username);
161 session.setAttribute("name", name);
162 session.setAttribute("password", password);
163 session.setAttribute("rpassword", rpassword);
164 session.setAttribute("email", email);
165 session.setAttribute("number", number);
166 try {
167 response.sendRedirect("/registerWarehouseman");
168 } catch (IOException e) {
169 throw new RuntimeException(e);
170 }
171 }
172 else {
173 loginService.register(name, username, email, number, password, role);
174 try {
175 response.sendRedirect("/login");
176 } catch (IOException e) {
177 throw new RuntimeException(e);
178 }
179 }
180 }
181 @GetMapping("/registerWarehouseman")
182 public String getSelectPage(Model model){
183 model.addAttribute("locations",warehouseRepository.findAll());
184 model.addAttribute("bodyContent","selectWarehouse");
185 return "master-template";
186 }
187 @PostMapping("/finishRegister")
188 public void handleWarehousemanRegister(@RequestParam String location, HttpServletResponse response, HttpSession session){
189 System.out.println("here?");
190 String username = (String) session.getAttribute("username");
191 String name = (String) session.getAttribute("name");
192 String password = (String) session.getAttribute("password");
193 String email = (String) session.getAttribute("email");
194 String number = (String) session.getAttribute("number");
195 Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
196 loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
197 try {
198 response.sendRedirect("/login");
199 } catch (IOException e) {
200 throw new RuntimeException(e);
201 }
202 }
203 @GetMapping("/access_denied")
204 public String accessDenied(Model model){
205 model.addAttribute("bodyContent","access_denied");
206 return "master-template";
207 }
208 @GetMapping("/myWarehouse")
209 public String myWarehouse(Model model, HttpServletRequest request){
210 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
211 Warehouse warehouse = whm.getWarehouse();
212 List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getId());
213 model.addAttribute("bodyContent","myWarehouse");
214 model.addAttribute("warehouse",warehouse);
215 model.addAttribute("parts", partIsInStockInWarehouseList);
216 return "master-template";
217 }
218 @GetMapping("myDeliveries")
219 public String myDeliveries(Model model, HttpServletRequest request){
220 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
221 List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
222 deliveries = deliveries.stream().sorted((p1,p2)->p2.getOrder().getDate().compareTo(p1.getOrder().getDate())).collect(Collectors.toList());
223 model.addAttribute("bodyContent","myDeliveries");
224 model.addAttribute("deliveries",deliveries);
225 return "master-template";
226 }
227 @GetMapping("myNextDeliveries")
228 public String myNextDeliveries(Model model, HttpServletRequest request){
229 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
230 List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
231 if(ldip.size()==0){
232 model.addAttribute("hasError",true);
233 model.addAttribute("error","Сите достави се завршени");
234 }
235 else {
236 model.addAttribute("hasError",false);
237 model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
238 }
239 model.addAttribute("bodyContent","myNextDeliveries");
240 return "master-template";
241 }
242 @PostMapping("/finishDelivery/{id}")
243 public void finishDelivery(@PathVariable Integer id, HttpServletResponse response){
244 Delivery d = deliveryService.findByOrder(orderService.findById(id));
245 d.setStatus("finished");
246 deliveryService.update(d);
247 try {
248 response.sendRedirect("/myDeliveries");
249 } catch (IOException e) {
250 throw new RuntimeException(e);
251 }
252 }
253 @GetMapping("/order/{id}")
254 public String getOrderInfo(@PathVariable Integer id, Model model){
255 List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
256 List<OrderInfo> partList = new ArrayList<>();
257 for (OrderContainsPart orderContainsPart : list) {
258 OrderInfo oi = new OrderInfo(partService.findById(orderContainsPart.getPartid()).getName(),
259 orderContainsPart.getQuantity_order(), partService.findById(orderContainsPart.getPartid()).getManufacturer().getName());
260 partList.add(oi);
261 }
262 model.addAttribute("parts",partList);
263 model.addAttribute("o",orderService.findById(id));
264 model.addAttribute("bodyContent","orderInfo");
265 return "master-template";
266 }
267 @GetMapping("/carCategoryReport")
268 public String getCarCategoryInfo(Model model){
269 model.addAttribute("data",carCategoryReportRepository.findAll());
270 model.addAttribute("bodyContent","carCategoryReport");
271 return "master-template";
272 }
273 @GetMapping("/partManufacturersReport")
274 public String getPartManufacturersReport(Model model){
275 model.addAttribute("data",partManufacturersReportRepository.findAll());
276 model.addAttribute("bodyContent","partManufacturersReport");
277 return "master-template";
278
279 }
280 @GetMapping("/mostPurchasedPart")
281 public String getMostPurchasedPart(Model model){
282 model.addAttribute("data",mostPurchasedPartRepository.findAll());
283 model.addAttribute("bodyContent","mostPurchasedPart");
284 return "master-template";
285 }
286 @GetMapping("/myWarehouseReport")
287 public String getMyWarehouseReport(Model model, HttpServletRequest request){
288 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
289 Warehouse wh = whm.getWarehouse();
290 Integer whId = wh.getId();
291 model.addAttribute("data", warehousemanReportRepository.findByWid(whId));
292 model.addAttribute("bodyContent","myWarehouseReport");
293 return "master-template";
294 }
295 @PostMapping("/myWarehouse/{pname}")
296 public void addPartToWarehouse(@PathVariable String pname,@RequestParam Integer quantity, HttpServletRequest request, HttpServletResponse response){
297 Integer pId = partRepository.findAllByName(pname).stream().findFirst().get().getId();
298 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
299 Warehouse wh = whm.getWarehouse();
300 Integer whId = wh.getId();
301 partService.addPartToWarehouse(pId,quantity,whId);
302 try {
303 response.sendRedirect("/myWarehouseReport");
304 } catch (IOException e) {
305 throw new RuntimeException(e);
306 }
307 }
308}
Note: See TracBrowser for help on using the repository browser.