Changeset f484b14 for src/main/java/com/example/autopartz/controller
- Timestamp:
- 01/06/23 02:51:45 (23 months ago)
- Branches:
- main
- Children:
- d4b888e
- Parents:
- 1bd8d1e
- Location:
- src/main/java/com/example/autopartz/controller
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/autopartz/controller/AdminController.java
r1bd8d1e rf484b14 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.model.Deliveryman; 3 4 import com.example.autopartz.model.Role; 4 5 import com.example.autopartz.model.User; 5 6 import com.example.autopartz.model.Warehouseman; 7 import com.example.autopartz.repository.DeliverymanRepository; 6 8 import com.example.autopartz.repository.WarehousemanRepository; 7 9 import com.example.autopartz.service.UserService; … … 13 15 import org.springframework.web.bind.annotation.RequestMapping; 14 16 15 import javax.annotation.security.RolesAllowed;16 17 import javax.servlet.http.HttpServletResponse; 17 18 import java.io.IOException; … … 25 26 private final UserService userService; 26 27 private final WarehousemanRepository warehousemanRepository; 28 private final DeliverymanRepository deliverymanRepository; 27 29 28 public AdminController(UserService userService, WarehousemanRepository warehousemanRepository ) {30 public AdminController(UserService userService, WarehousemanRepository warehousemanRepository, DeliverymanRepository deliverymanRepository) { 29 31 this.userService = userService; 30 32 this.warehousemanRepository = warehousemanRepository; 33 this.deliverymanRepository = deliverymanRepository; 31 34 } 32 35 … … 44 47 wh.setEmployed_from(LocalDate.now()); 45 48 warehousemanRepository.save(wh); 49 50 } 51 else { 52 Deliveryman dm = (Deliveryman) userService.findById(id); 53 dm.setEmployed_from(LocalDate.now()); 54 deliverymanRepository.save(dm); 46 55 try { 47 56 response.sendRedirect("/viewUsers"); -
src/main/java/com/example/autopartz/controller/PartController.java
r1bd8d1e rf484b14 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.model.Client; 4 import com.example.autopartz.model.Order; 5 import com.example.autopartz.model.Part; 6 import com.example.autopartz.model.User; 3 import com.example.autopartz.model.*; 7 4 import com.example.autopartz.model.manytomany.OrderContainsPart; 8 import com.example.autopartz.repository.OrderContainsPartRepository; 5 import com.example.autopartz.model.manytomany.RsForCm; 6 import com.example.autopartz.repository.*; 9 7 import com.example.autopartz.service.*; 10 8 import org.springframework.stereotype.Controller; … … 16 14 import javax.servlet.http.HttpSession; 17 15 import java.io.IOException; 16 import java.util.ArrayList; 17 import java.util.List; 18 18 19 19 @Controller … … 26 26 private final UserService userService; 27 27 private final OrderContainsPartRepository orderContainsPartRepository; 28 public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, 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) { 29 35 this.partService = partService; 30 36 this.repairShopService = repairShopService; … … 33 39 this.userService = userService; 34 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; 35 47 } 36 48 @GetMapping("/{id}") … … 44 56 } 45 57 @GetMapping("/delivery") 46 public String getDeliveryPage(Model model){ 47 model.addAttribute("repairShops",repairShopService.findAll()); 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 } 48 69 model.addAttribute("bodyContent","deliveryForPart"); 49 70 return "master-template"; 50 71 } 51 72 @PostMapping("/repairshopdelivery") 52 public void setRepairShopDelivery(@RequestParam String name, HttpServletResponse response, HttpSession session){73 public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session, HttpServletRequest request){ 53 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_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"); 105 CarSample cs = (CarSample)session.getAttribute("car"); 106 ServiceBook sb = serviceBookRepository.findByCarSample(cs); 107 RepairShop repairShop = repairShopService.getById(rs); 108 repairRepository.save(new Repair(o,repairShop,sb)); 54 109 session.removeAttribute("order"); 55 110 try { … … 62 117 public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){ 63 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)); 64 125 session.removeAttribute("order"); 65 126 try { -
src/main/java/com/example/autopartz/controller/UserController.java
r1bd8d1e rf484b14 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.repository.OrdersForUserRepository; 4 import com.example.autopartz.repository.RepairsForUserRepository; 5 import com.example.autopartz.repository.ReviewsForUserRepository; 3 import com.example.autopartz.model.CarSample; 4 import com.example.autopartz.model.Client; 5 import com.example.autopartz.model.ServiceBook; 6 import com.example.autopartz.repository.*; 7 import com.example.autopartz.service.CarService; 6 8 import com.example.autopartz.service.UserService; 7 9 import org.springframework.stereotype.Controller; 8 10 import org.springframework.ui.Model; 9 11 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.P athVariable;12 import org.springframework.web.bind.annotation.PostMapping; 11 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RequestParam; 12 15 13 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 import java.io.IOException; 14 19 15 20 @Controller … … 20 25 private final ReviewsForUserRepository reviewsForUserRepository; 21 26 private final UserService userService; 22 public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService) { 27 private final CarService carService; 28 private final CarSampleRepository carSampleRepository; 29 private final ServiceBookRepository serviceBookRepository; 30 public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) { 23 31 this.ordersForUserRepository = ordersForUserRepository; 24 32 this.repairsForUserRepository = repairsForUserRepository; 25 33 this.reviewsForUserRepository = reviewsForUserRepository; 26 34 this.userService = userService; 35 this.carService = carService; 36 this.carSampleRepository = carSampleRepository; 37 this.serviceBookRepository = serviceBookRepository; 27 38 } 28 39 @GetMapping("orders") … … 47 58 return "master-template"; 48 59 } 60 @GetMapping("/addCarSampleForUser") 61 public String addCarSampleForUser(Model model){ 62 model.addAttribute("bodyContent","addCarSampleForUser"); 63 model.addAttribute("cars",carService.findAll()); 64 return "master-template"; 65 } 66 @PostMapping("/addCarSampleForUser") 67 public void addCarSampleForUser(@RequestParam Integer vin, @RequestParam Integer year, @RequestParam Integer power, 68 @RequestParam Integer displacement, @RequestParam String fuel, 69 @RequestParam Integer km, @RequestParam Integer cartype, 70 HttpServletRequest request, HttpServletResponse response){ 71 CarSample cs = new CarSample(vin,year,power,displacement,fuel,km, (Client) userService.findByUsername(request.getRemoteUser()),carService.findById(cartype)); 72 carSampleRepository.save(cs); 73 serviceBookRepository.save(new ServiceBook(cs)); 74 try { 75 response.sendRedirect("/"); 76 } catch (IOException e) { 77 throw new RuntimeException(e); 78 } 79 } 49 80 }
Note:
See TracChangeset
for help on using the changeset viewer.