1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.*;
|
---|
4 | import com.example.autopartz.model.manytomany.PartIsAppropriateForCar;
|
---|
5 | import com.example.autopartz.model.manytomany.PartIsFromCategory;
|
---|
6 | import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
|
---|
7 | import com.example.autopartz.repository.*;
|
---|
8 | import com.example.autopartz.service.PriceService;
|
---|
9 | import com.example.autopartz.service.UserService;
|
---|
10 | import org.springframework.stereotype.Controller;
|
---|
11 | import org.springframework.ui.Model;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
13 |
|
---|
14 | import javax.servlet.http.HttpServletResponse;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.time.LocalDate;
|
---|
17 | import java.time.LocalDateTime;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Objects;
|
---|
20 |
|
---|
21 | @Controller
|
---|
22 | @RequestMapping("/")
|
---|
23 | public class AdminController {
|
---|
24 | private final UserService userService;
|
---|
25 | private final PartIsFromCategoryRepository partIsFromCategoryRepository;
|
---|
26 | private final PartIsAppropriateForCarRepository partIsAppropriateForCarRepository;
|
---|
27 | private final WarehousemanRepository warehousemanRepository;
|
---|
28 | private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
|
---|
29 | private final DeliverymanRepository deliverymanRepository;
|
---|
30 | private final CategoryRepository categoryRepository;
|
---|
31 | private final PartRepository partRepository;
|
---|
32 | private final WarehouseRepository warehouseRepository;
|
---|
33 | private final CarRepository carRepository;
|
---|
34 | private final PartManufacturerRepository partManufacturerRepository;
|
---|
35 | private final PriceService priceService;
|
---|
36 |
|
---|
37 | public AdminController(UserService userService, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehousemanRepository warehousemanRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, PartRepository partRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService) {
|
---|
38 | this.userService = userService;
|
---|
39 | this.partIsFromCategoryRepository = partIsFromCategoryRepository;
|
---|
40 | this.partIsAppropriateForCarRepository = partIsAppropriateForCarRepository;
|
---|
41 | this.warehousemanRepository = warehousemanRepository;
|
---|
42 | this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
|
---|
43 | this.deliverymanRepository = deliverymanRepository;
|
---|
44 | this.categoryRepository = categoryRepository;
|
---|
45 | this.partRepository = partRepository;
|
---|
46 | this.warehouseRepository = warehouseRepository;
|
---|
47 | this.carRepository = carRepository;
|
---|
48 | this.partManufacturerRepository = partManufacturerRepository;
|
---|
49 | this.priceService = priceService;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping("/viewUsers")
|
---|
53 | public String getAllUsers(Model model){
|
---|
54 | List<User> pendingList = userService.findAllUsers().stream().filter(u->u.getAuthorities().contains(Role.ROLE_PENDING_DELIVERYMAN) || u.getAuthorities().contains(Role.ROLE_PENDING_WAREHOUSEMAN)).toList();
|
---|
55 | if(pendingList.size()==0){
|
---|
56 | model.addAttribute("hasError",true);
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | model.addAttribute("hasError",false);
|
---|
60 | model.addAttribute("users", pendingList);
|
---|
61 | }
|
---|
62 | model.addAttribute("bodyContent", "viewUsers");
|
---|
63 | return "master-template";
|
---|
64 | }
|
---|
65 | @PostMapping("/approve/{id}")
|
---|
66 | public void approve(@PathVariable Integer id, HttpServletResponse response){
|
---|
67 | if(Objects.equals(userService.findById(id).getAuthorities().stream().findFirst().get(),Role.ROLE_PENDING_WAREHOUSEMAN)){
|
---|
68 | Warehouseman wh = (Warehouseman) userService.findById(id);
|
---|
69 | wh.setEmployed_from(LocalDate.now());
|
---|
70 | warehousemanRepository.save(wh);
|
---|
71 |
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | Deliveryman dm = (Deliveryman) userService.findById(id);
|
---|
75 | dm.setEmployed_from(LocalDate.now());
|
---|
76 | deliverymanRepository.save(dm);
|
---|
77 | try {
|
---|
78 | response.sendRedirect("/viewUsers");
|
---|
79 | } catch (IOException e) {
|
---|
80 | throw new RuntimeException(e);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | @GetMapping("/addPart")
|
---|
85 | public String addPart(Model model){
|
---|
86 | model.addAttribute("categories",categoryRepository.findAll());
|
---|
87 | model.addAttribute("warehouses",warehouseRepository.findAll());
|
---|
88 | model.addAttribute("cars",carRepository.findAll());
|
---|
89 | model.addAttribute("manufacturers",partManufacturerRepository.findAll());
|
---|
90 | model.addAttribute("bodyContent","addPart");
|
---|
91 | return "master-template";
|
---|
92 | }
|
---|
93 | @PostMapping("/addPart")
|
---|
94 | public void addPart(@RequestParam String name, @RequestParam(required = false) String description,
|
---|
95 | @RequestParam Integer manufacturer, @RequestParam List<Car> cars,
|
---|
96 | @RequestParam List<Category> categories, @RequestParam Integer warehouse,
|
---|
97 | @RequestParam Integer quantity, @RequestParam Integer amount, HttpServletResponse response){
|
---|
98 | // Part(String name, String description, PartManufacturer manufacturer, List<Category> categoryList, List<Warehouse> warehouseList, List<Car> carList) {
|
---|
99 | Part newPart = new Part(name, description==null ? "" : description, partManufacturerRepository.findById(manufacturer).get(),
|
---|
100 | categories, List.of(warehouseRepository.findById(warehouse).get()),cars);
|
---|
101 | partRepository.save(newPart);
|
---|
102 | priceService.save(new Price(amount, LocalDate.now(),newPart));
|
---|
103 | partIsInStockInWarehouseRepository.save(new PartIsInStockInWarehouse(newPart.getId(),warehouse,quantity));
|
---|
104 | for (Category c:categories
|
---|
105 | ) {
|
---|
106 | partIsFromCategoryRepository.save(new PartIsFromCategory(newPart.getId(),c.getId()));
|
---|
107 | }
|
---|
108 | for (Car car:cars){
|
---|
109 | partIsAppropriateForCarRepository.save(new PartIsAppropriateForCar(newPart.getId(),car.getId()));
|
---|
110 | }
|
---|
111 | try {
|
---|
112 | response.sendRedirect("/");
|
---|
113 | } catch (IOException e) {
|
---|
114 | throw new RuntimeException(e);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|