[feffc2f] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
[f484b14] | 3 | import com.example.autopartz.model.*;
|
---|
[7d43957] | 4 | import com.example.autopartz.model.manytomany.OrderContainsPart;
|
---|
[f484b14] | 5 | import com.example.autopartz.model.manytomany.RsForCm;
|
---|
| 6 | import com.example.autopartz.repository.*;
|
---|
[7d43957] | 7 | import com.example.autopartz.service.*;
|
---|
[feffc2f] | 8 | import org.springframework.stereotype.Controller;
|
---|
| 9 | import org.springframework.ui.Model;
|
---|
| 10 | import org.springframework.web.bind.annotation.*;
|
---|
| 11 |
|
---|
[7d43957] | 12 | import javax.servlet.http.HttpServletRequest;
|
---|
[ae042f4] | 13 | import javax.servlet.http.HttpServletResponse;
|
---|
[7d43957] | 14 | import javax.servlet.http.HttpSession;
|
---|
[feffc2f] | 15 | import java.io.IOException;
|
---|
[f484b14] | 16 | import java.util.ArrayList;
|
---|
| 17 | import java.util.List;
|
---|
[feffc2f] | 18 |
|
---|
| 19 | @Controller
|
---|
| 20 | @RequestMapping("/part")
|
---|
| 21 | public class PartController {
|
---|
| 22 | private final PartService partService;
|
---|
| 23 | private final RepairShopService repairShopService;
|
---|
| 24 | private final PriceService priceService;
|
---|
[7d43957] | 25 | private final OrderService orderService;
|
---|
| 26 | private final UserService userService;
|
---|
| 27 | private final OrderContainsPartRepository orderContainsPartRepository;
|
---|
[f484b14] | 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) {
|
---|
[feffc2f] | 35 | this.partService = partService;
|
---|
| 36 | this.repairShopService = repairShopService;
|
---|
| 37 | this.priceService = priceService;
|
---|
[7d43957] | 38 | this.orderService = orderService;
|
---|
| 39 | this.userService = userService;
|
---|
| 40 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
[f484b14] | 41 | this.deliveryRepository = deliveryRepository;
|
---|
| 42 | this.repairRepository = repairRepository;
|
---|
| 43 | this.carSampleRepository = carSampleRepository;
|
---|
| 44 | this.serviceBookRepository = serviceBookRepository;
|
---|
| 45 | this.deliverymanRepository = deliverymanRepository;
|
---|
| 46 | this.rsForCmRepository = rsForCmRepository;
|
---|
[feffc2f] | 47 | }
|
---|
| 48 | @GetMapping("/{id}")
|
---|
[ae042f4] | 49 | public String getPartPage(@PathVariable Integer id, Model model){
|
---|
[feffc2f] | 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);
|
---|
[60de3eb] | 54 | model.addAttribute("bodyContent","partinfo");
|
---|
| 55 | return "master-template";
|
---|
[feffc2f] | 56 | }
|
---|
[7d43957] | 57 | @GetMapping("/delivery")
|
---|
[f484b14] | 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 | }
|
---|
[60de3eb] | 69 | model.addAttribute("bodyContent","deliveryForPart");
|
---|
| 70 | return "master-template";
|
---|
[feffc2f] | 71 | }
|
---|
| 72 | @PostMapping("/repairshopdelivery")
|
---|
[f484b14] | 73 | public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session, HttpServletRequest request){
|
---|
[feffc2f] | 74 | // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
|
---|
[f484b14] | 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_car_manufacturer();
|
---|
| 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");
|
---|
[d4b888e] | 105 | CarSample cs = carSampleRepository.findById((Integer)session.getAttribute("carVin")).get();
|
---|
[f484b14] | 106 | ServiceBook sb = serviceBookRepository.findByCarSample(cs);
|
---|
| 107 | RepairShop repairShop = repairShopService.getById(rs);
|
---|
| 108 | repairRepository.save(new Repair(o,repairShop,sb));
|
---|
[d4b888e] | 109 | // o.setOrder_status("finished");
|
---|
| 110 | // orderService.save(o);
|
---|
[676144b] | 111 | session.removeAttribute("order");
|
---|
[feffc2f] | 112 | try {
|
---|
| 113 | response.sendRedirect("/");
|
---|
| 114 | } catch (IOException e) {
|
---|
| 115 | throw new RuntimeException(e);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | @PostMapping("/homedelivery")
|
---|
[676144b] | 119 | public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){
|
---|
[feffc2f] | 120 | // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
|
---|
[f484b14] | 121 | Order o = (Order) session.getAttribute("order");
|
---|
| 122 | List<Deliveryman> deliverymanList = deliverymanRepository.findAll().stream().filter(dm->dm.getAuthorities().contains(Role.ROLE_DELIVERYMAN)).toList();
|
---|
| 123 | int num = deliverymanList.size();
|
---|
| 124 | int deliverer = (int) ((Math.random() * (num)));
|
---|
| 125 | Deliveryman dm = deliverymanList.get(deliverer);
|
---|
| 126 | deliveryRepository.save(new Delivery("in progress",address,dm,o));
|
---|
[d4b888e] | 127 | // o.setOrder_status("finished");
|
---|
| 128 | // orderService.save(o);
|
---|
[676144b] | 129 | session.removeAttribute("order");
|
---|
[feffc2f] | 130 | try {
|
---|
| 131 | response.sendRedirect("/");
|
---|
| 132 | } catch (IOException e) {
|
---|
| 133 | throw new RuntimeException(e);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[7d43957] | 136 | @PostMapping("/addToOrder/{id}")
|
---|
| 137 | public void addToOrder(@PathVariable Integer id,@RequestParam Integer quantity, HttpSession session, HttpServletResponse response, HttpServletRequest request){
|
---|
| 138 | if(session.getAttribute("order")==null){
|
---|
| 139 | User u = userService.findByUsername(request.getRemoteUser());
|
---|
| 140 | Order newOrder = orderService.create((Client) u);
|
---|
| 141 | session.setAttribute("order",newOrder);
|
---|
| 142 | }
|
---|
| 143 | Order order = (Order) session.getAttribute("order");
|
---|
[1bd8d1e] | 144 | orderContainsPartRepository.save(new OrderContainsPart(id,order.getOrderid(),quantity));
|
---|
[7d43957] | 145 | try {
|
---|
| 146 | response.sendRedirect("/products");
|
---|
| 147 | } catch (IOException e) {
|
---|
| 148 | throw new RuntimeException(e);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[feffc2f] | 151 | }
|
---|