source: src/main/java/com/example/autopartz/controller/AdminController.java@ d5913de

main
Last change on this file since d5913de was d5913de, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 18 months ago

Fixed redirect

  • Property mode set to 100644
File size: 10.6 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.model.manytomany.PartIsAppropriateForCar;
5import com.example.autopartz.model.manytomany.PartIsFromCategory;
6import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
7import com.example.autopartz.model.manytomany.RsForCm;
8import com.example.autopartz.repository.*;
9import com.example.autopartz.service.PriceService;
10import com.example.autopartz.service.UserService;
11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.*;
14
15import javax.servlet.http.HttpServletResponse;
16import java.io.IOException;
17import java.time.LocalDate;
18import java.util.List;
19import java.util.Objects;
20
21@Controller
22@RequestMapping("/")
23public class AdminController {
24 private final UserService userService;
25 private final CarManufacturerRepository carManufacturerRepository;
26 private final PartIsFromCategoryRepository partIsFromCategoryRepository;
27 private final PartIsAppropriateForCarRepository partIsAppropriateForCarRepository;
28 private final WarehousemanRepository warehousemanRepository;
29 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
30 private final DeliverymanRepository deliverymanRepository;
31 private final CategoryRepository categoryRepository;
32 private final PartRepository partRepository;
33 private final WarehouseRepository warehouseRepository;
34 private final CarRepository carRepository;
35 private final PartManufacturerRepository partManufacturerRepository;
36 private final PriceService priceService;
37 private final RepairShopRepository repairShopRepository;
38 private final RsForCmRepository rsForCmRepository;
39
40 public AdminController(UserService userService, CarManufacturerRepository carManufacturerRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehousemanRepository warehousemanRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, PartRepository partRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService, RepairShopRepository repairShopRepository, RsForCmRepository rsForCmRepository) {
41 this.userService = userService;
42 this.carManufacturerRepository = carManufacturerRepository;
43 this.partIsFromCategoryRepository = partIsFromCategoryRepository;
44 this.partIsAppropriateForCarRepository = partIsAppropriateForCarRepository;
45 this.warehousemanRepository = warehousemanRepository;
46 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
47 this.deliverymanRepository = deliverymanRepository;
48 this.categoryRepository = categoryRepository;
49 this.partRepository = partRepository;
50 this.warehouseRepository = warehouseRepository;
51 this.carRepository = carRepository;
52 this.partManufacturerRepository = partManufacturerRepository;
53 this.priceService = priceService;
54 this.repairShopRepository = repairShopRepository;
55 this.rsForCmRepository = rsForCmRepository;
56 }
57
58 @GetMapping("/viewUsers")
59 public String getAllUsers(Model model){
60 List<User> pendingList = userService.findAllUsers().stream().filter(u->u.getAuthorities().contains(Role.ROLE_PENDING_DELIVERYMAN) || u.getAuthorities().contains(Role.ROLE_PENDING_WAREHOUSEMAN)).toList();
61 if(pendingList.size()==0){
62 model.addAttribute("hasError",true);
63 }
64 else {
65 model.addAttribute("hasError",false);
66 model.addAttribute("users", pendingList);
67 }
68 model.addAttribute("bodyContent", "viewUsers");
69 return "master-template";
70 }
71 @PostMapping("/approve/{id}")
72 public void approve(@PathVariable Integer id, HttpServletResponse response){
73 if(Objects.equals(userService.findById(id).getAuthorities().stream().findFirst().get(),Role.ROLE_PENDING_WAREHOUSEMAN)){
74 Warehouseman wh = (Warehouseman) userService.findById(id);
75 wh.setEmployed_from(LocalDate.now());
76 warehousemanRepository.save(wh);
77 try {
78 response.sendRedirect("/viewUsers");
79 } catch (IOException e) {
80 throw new RuntimeException(e);
81 }
82
83 }
84 else {
85 Deliveryman dm = (Deliveryman) userService.findById(id);
86 dm.setEmployed_from(LocalDate.now());
87 deliverymanRepository.save(dm);
88 try {
89 response.sendRedirect("/viewUsers");
90 } catch (IOException e) {
91 throw new RuntimeException(e);
92 }
93 }
94 }
95 @GetMapping("/addPart")
96 public String addPart(Model model){
97 model.addAttribute("categories",categoryRepository.findAll());
98 model.addAttribute("warehouses",warehouseRepository.findAll());
99 model.addAttribute("cars",carRepository.findAll());
100 model.addAttribute("manufacturers",partManufacturerRepository.findAll());
101 model.addAttribute("bodyContent","addPart");
102 return "master-template";
103 }
104 @PostMapping("/addPart")
105 public void addPart(@RequestParam String name, @RequestParam(required = false) String description,
106 @RequestParam Integer manufacturer, @RequestParam List<Car> cars,
107 @RequestParam List<Category> categories, @RequestParam Integer warehouse,
108 @RequestParam Integer quantity, @RequestParam Integer amount, HttpServletResponse response){
109 // Part(String name, String description, PartManufacturer manufacturer, List<Category> categoryList, List<Warehouse> warehouseList, List<Car> carList) {
110 Part newPart = new Part(name, description==null ? "" : description, partManufacturerRepository.findById(manufacturer).get(),
111 categories, List.of(warehouseRepository.findById(warehouse).get()),cars);
112 partRepository.save(newPart);
113 priceService.save(new Price(amount, LocalDate.now(),newPart));
114 partIsInStockInWarehouseRepository.save(new PartIsInStockInWarehouse(newPart.getId(),warehouse,quantity));
115 for (Category c:categories
116 ) {
117 partIsFromCategoryRepository.save(new PartIsFromCategory(newPart.getId(),c.getId()));
118 }
119 for (Car car:cars){
120 partIsAppropriateForCarRepository.save(new PartIsAppropriateForCar(newPart.getId(),car.getId()));
121 }
122 try {
123 response.sendRedirect("/");
124 } catch (IOException e) {
125 throw new RuntimeException(e);
126 }
127 }
128 @GetMapping("/addCarManufacturer")
129 public String getCarManView(Model model){
130 model.addAttribute("bodyContent","addCarManufacturer");
131 return "master-template";
132 }
133 @PostMapping("/addCarManufacturer")
134 public void saveCarManufacturer(@RequestParam String name,@RequestParam String location,
135 Model model, HttpServletResponse response) {
136 carManufacturerRepository.save(new CarManufacturer(name,location));
137 try {
138 response.sendRedirect("/");
139 } catch (IOException e) {
140 throw new RuntimeException(e);
141 }
142 }
143 @GetMapping("/addPartManufacturer")
144 public String getPartManView(Model model){
145 model.addAttribute("bodyContent","addPartManufacturer");
146 return "master-template";
147 }
148 @PostMapping("/addPartManufacturer")
149 public void savePartManufacturer(@RequestParam String name,@RequestParam String location,
150 Model model, HttpServletResponse response) {
151 partManufacturerRepository.save(new PartManufacturer(name,location));
152 try {
153 response.sendRedirect("/");
154 } catch (IOException e) {
155 throw new RuntimeException(e);
156 }
157 }
158 @GetMapping("/addCategory")
159 public String getCategoryView(Model model){
160 model.addAttribute("bodyContent","addCategory");
161 return "master-template";
162 }
163 @PostMapping("/addCategory")
164 public void saveCategory(@RequestParam String name,
165 Model model, HttpServletResponse response) {
166 categoryRepository.save(new Category(name));
167 try {
168 response.sendRedirect("/");
169 } catch (IOException e) {
170 throw new RuntimeException(e);
171 }
172 }
173 @GetMapping("/addCar")
174 public String getCarView(Model model){
175 model.addAttribute("bodyContent","addCar");
176 model.addAttribute("manufacturers",carManufacturerRepository.findAll());
177 return "master-template";
178 }
179 @PostMapping("/addCar")
180 public void saveCar(@RequestParam Integer since,@RequestParam Integer till,
181 @RequestParam String name,@RequestParam Integer mId,
182 HttpServletResponse response) {
183 carRepository.save(new Car(since,till,name,carManufacturerRepository.findById(mId).get()));
184 try {
185 response.sendRedirect("/");
186 } catch (IOException e) {
187 throw new RuntimeException(e);
188 }
189 }
190 @GetMapping("/addRepairShop")
191 public String getRepairShopView(Model model){
192 model.addAttribute("bodyContent","addRepairShop");
193 model.addAttribute("manufacturers",carManufacturerRepository.findAll());
194 return "master-template";
195 }
196 @PostMapping("/addRepairShop")
197 public void saveRepairShop(@RequestParam String name,@RequestParam String location,
198 @RequestParam String number,@RequestParam Integer carMId,
199 HttpServletResponse response) {
200 RepairShop newRs = new RepairShop(name,location,number,
201 List.of(carManufacturerRepository.findById(carMId).get()));
202 repairShopRepository.save(newRs);
203 rsForCmRepository.save(new RsForCm(newRs.getId(), carMId));
204 try {
205 response.sendRedirect("/");
206 } catch (IOException e) {
207 throw new RuntimeException(e);
208 }
209 }
210 @GetMapping("/addWarehouse")
211 public String getWarehouseView(Model model){
212 model.addAttribute("bodyContent","addWarehouse");
213 return "master-template";
214 }
215 @PostMapping("/addWarehouse")
216 public void saveWarehouse(@RequestParam String name,
217 HttpServletResponse response) {
218 warehouseRepository.save(new Warehouse(name));
219 try {
220 response.sendRedirect("/");
221 } catch (IOException e) {
222 throw new RuntimeException(e);
223 }
224 }
225}
Note: See TracBrowser for help on using the repository browser.