source: src/main/java/com/example/autopartz/controller/PartController.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: 7.2 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.model.manytomany.OrderContainsPart;
5import com.example.autopartz.model.manytomany.RsForCm;
6import com.example.autopartz.repository.*;
7import com.example.autopartz.service.*;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.*;
11
12import javax.servlet.http.HttpServletRequest;
13import javax.servlet.http.HttpServletResponse;
14import javax.servlet.http.HttpSession;
15import java.io.IOException;
16import java.util.ArrayList;
17import java.util.List;
18
19@Controller
20@RequestMapping("/part")
21public class PartController {
22 private final PartService partService;
23 private final RepairShopService repairShopService;
24 private final PriceService priceService;
25 private final OrderService orderService;
26 private final UserService userService;
27 private final OrderContainsPartRepository orderContainsPartRepository;
28 private final DeliveryRepository deliveryRepository;
29 private final RepairRepository repairRepository;
30 private final CarSampleRepository carSampleRepository;
31 private final ServiceBookRepository serviceBookRepository;
32 private final DeliverymanRepository deliverymanRepository;
33 private final RsForCmRepository rsForCmRepository;
34 public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository, DeliveryRepository deliveryRepository, RepairRepository repairRepository, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, DeliverymanRepository deliverymanRepository, RsForCmRepository rsForCmRepository) {
35 this.partService = partService;
36 this.repairShopService = repairShopService;
37 this.priceService = priceService;
38 this.orderService = orderService;
39 this.userService = userService;
40 this.orderContainsPartRepository = orderContainsPartRepository;
41 this.deliveryRepository = deliveryRepository;
42 this.repairRepository = repairRepository;
43 this.carSampleRepository = carSampleRepository;
44 this.serviceBookRepository = serviceBookRepository;
45 this.deliverymanRepository = deliverymanRepository;
46 this.rsForCmRepository = rsForCmRepository;
47 }
48 @GetMapping("/{id}")
49 public String getPartPage(@PathVariable Integer id, Model model){
50 Part temp = partService.findById(id);
51 Integer amount = priceService.findPriceForPart(temp).stream().findFirst().orElseThrow(RuntimeException::new).getAmount();
52 model.addAttribute("part",temp);
53 model.addAttribute("amount",amount);
54 model.addAttribute("bodyContent","partinfo");
55 return "master-template";
56 }
57 @GetMapping("/delivery")
58 public String getDeliveryPage(Model model, HttpServletRequest request){
59 Client cl = (Client) userService.findByUsername(request.getRemoteUser());
60 List<CarSample> cs = carSampleRepository.findAllByClient(cl);
61 model.addAttribute("cars",cs);
62 if(cs.size()==0){
63 model.addAttribute("hasError",true);
64 model.addAttribute("error","Внеси твоја кола");
65 }
66 else {
67 model.addAttribute("hasError",false);
68 }
69 model.addAttribute("bodyContent","deliveryForPart");
70 return "master-template";
71 }
72 @PostMapping("/repairshopdelivery")
73 public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session){
74 // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
75 session.setAttribute("carVin",vin);
76 try {
77 response.sendRedirect("/part/chooseRepairShop");
78 } catch (IOException e) {
79 throw new RuntimeException(e);
80 }
81 }
82 @GetMapping("/chooseRepairShop")
83 public String chooseRepairShop(Model model, HttpSession session){
84 if(session.getAttribute("carVin")==null){
85 model.addAttribute("hasError",true);
86 model.addAttribute("error","Внеси твоја кола");
87 }
88 else {
89 model.addAttribute("hasError", false);
90 CarSample cs = carSampleRepository.findById((Integer) session.getAttribute("carVin")).get();
91 Integer idCM = cs.getCar().getCar_manufacturer().getId();
92 List<RsForCm> rsForCm = rsForCmRepository.findAllByCmid(idCM);
93 List<RepairShop> newRepairShopList = new ArrayList<>();
94 for (RsForCm forCm : rsForCm) {
95 newRepairShopList.add(repairShopService.getById(forCm.getRsid()));
96 }
97 model.addAttribute("authorized", newRepairShopList);
98 }
99 model.addAttribute("bodyContent", "chooseRepairShop");
100 return "master-template";
101 }
102 @PostMapping("/chooseRepairShop")
103 public void chooseRepairShop(@RequestParam Integer rs,HttpSession session, HttpServletResponse response){
104 Order o = (Order) session.getAttribute("order");
105 CarSample cs = carSampleRepository.findById((Integer)session.getAttribute("carVin")).get();
106 ServiceBook sb = serviceBookRepository.findByCarSample(cs);
107 RepairShop repairShop = repairShopService.getById(rs);
108 repairRepository.save(new Repair(o,repairShop,sb));
109 session.removeAttribute("order");
110 try {
111 response.sendRedirect("/");
112 } catch (IOException e) {
113 throw new RuntimeException(e);
114 }
115 }
116 @PostMapping("/homedelivery")
117 public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){
118 // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
119 Order o = (Order) session.getAttribute("order");
120 List<Deliveryman> deliverymanList = deliverymanRepository.findAll().stream().filter(dm->dm.getAuthorities().contains(Role.ROLE_DELIVERYMAN)).toList();
121 int num = deliverymanList.size();
122 int deliverer = (int) ((Math.random() * (num)));
123 Deliveryman dm = deliverymanList.get(deliverer);
124 deliveryRepository.save(new Delivery("in progress",address,dm,o));
125 session.removeAttribute("order");
126 try {
127 response.sendRedirect("/");
128 } catch (IOException e) {
129 throw new RuntimeException(e);
130 }
131 }
132 @PostMapping("/addToOrder/{id}")
133 public void addToOrder(@PathVariable Integer id,@RequestParam Integer quantity, HttpSession session, HttpServletResponse response, HttpServletRequest request){
134 if(session.getAttribute("order")==null){
135 User u = userService.findByUsername(request.getRemoteUser());
136 Order newOrder = orderService.createOrderAndAddPartToOrder((Client) u, id, quantity);
137 session.setAttribute("order",newOrder);
138 } else {
139 Order order = (Order) session.getAttribute("order");
140 orderContainsPartRepository.save(new OrderContainsPart(id, order.getOrderid(), quantity));
141 }
142 try {
143 response.sendRedirect("/products");
144 } catch (IOException e) {
145 throw new RuntimeException(e);
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.