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

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

Admin views for adding things to the database

  • Property mode set to 100644
File size: 10.5 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
78 }
79 else {
80 Deliveryman dm = (Deliveryman) userService.findById(id);
81 dm.setEmployed_from(LocalDate.now());
82 deliverymanRepository.save(dm);
83 try {
84 response.sendRedirect("/viewUsers");
85 } catch (IOException e) {
86 throw new RuntimeException(e);
87 }
88 }
89 }
90 @GetMapping("/addPart")
91 public String addPart(Model model){
92 model.addAttribute("categories",categoryRepository.findAll());
93 model.addAttribute("warehouses",warehouseRepository.findAll());
94 model.addAttribute("cars",carRepository.findAll());
95 model.addAttribute("manufacturers",partManufacturerRepository.findAll());
96 model.addAttribute("bodyContent","addPart");
97 return "master-template";
98 }
99 @PostMapping("/addPart")
100 public void addPart(@RequestParam String name, @RequestParam(required = false) String description,
101 @RequestParam Integer manufacturer, @RequestParam List<Car> cars,
102 @RequestParam List<Category> categories, @RequestParam Integer warehouse,
103 @RequestParam Integer quantity, @RequestParam Integer amount, HttpServletResponse response){
104 // Part(String name, String description, PartManufacturer manufacturer, List<Category> categoryList, List<Warehouse> warehouseList, List<Car> carList) {
105 Part newPart = new Part(name, description==null ? "" : description, partManufacturerRepository.findById(manufacturer).get(),
106 categories, List.of(warehouseRepository.findById(warehouse).get()),cars);
107 partRepository.save(newPart);
108 priceService.save(new Price(amount, LocalDate.now(),newPart));
109 partIsInStockInWarehouseRepository.save(new PartIsInStockInWarehouse(newPart.getId(),warehouse,quantity));
110 for (Category c:categories
111 ) {
112 partIsFromCategoryRepository.save(new PartIsFromCategory(newPart.getId(),c.getId()));
113 }
114 for (Car car:cars){
115 partIsAppropriateForCarRepository.save(new PartIsAppropriateForCar(newPart.getId(),car.getId()));
116 }
117 try {
118 response.sendRedirect("/");
119 } catch (IOException e) {
120 throw new RuntimeException(e);
121 }
122 }
123 @GetMapping("/addCarManufacturer")
124 public String getCarManView(Model model){
125 model.addAttribute("bodyContent","addCarManufacturer");
126 return "master-template";
127 }
128 @PostMapping("/addCarManufacturer")
129 public void saveCarManufacturer(@RequestParam String name,@RequestParam String location,
130 Model model, HttpServletResponse response) {
131 carManufacturerRepository.save(new CarManufacturer(name,location));
132 try {
133 response.sendRedirect("/");
134 } catch (IOException e) {
135 throw new RuntimeException(e);
136 }
137 }
138 @GetMapping("/addPartManufacturer")
139 public String getPartManView(Model model){
140 model.addAttribute("bodyContent","addPartManufacturer");
141 return "master-template";
142 }
143 @PostMapping("/addPartManufacturer")
144 public void savePartManufacturer(@RequestParam String name,@RequestParam String location,
145 Model model, HttpServletResponse response) {
146 partManufacturerRepository.save(new PartManufacturer(name,location));
147 try {
148 response.sendRedirect("/");
149 } catch (IOException e) {
150 throw new RuntimeException(e);
151 }
152 }
153 @GetMapping("/addCategory")
154 public String getCategoryView(Model model){
155 model.addAttribute("bodyContent","addCategory");
156 return "master-template";
157 }
158 @PostMapping("/addCategory")
159 public void saveCategory(@RequestParam String name,
160 Model model, HttpServletResponse response) {
161 categoryRepository.save(new Category(name));
162 try {
163 response.sendRedirect("/");
164 } catch (IOException e) {
165 throw new RuntimeException(e);
166 }
167 }
168 @GetMapping("/addCar")
169 public String getCarView(Model model){
170 model.addAttribute("bodyContent","addCar");
171 model.addAttribute("manufacturers",carManufacturerRepository.findAll());
172 return "master-template";
173 }
174 @PostMapping("/addCar")
175 public void saveCar(@RequestParam Integer since,@RequestParam Integer till,
176 @RequestParam String name,@RequestParam Integer mId,
177 HttpServletResponse response) {
178 carRepository.save(new Car(since,till,name,carManufacturerRepository.findById(mId).get()));
179 try {
180 response.sendRedirect("/");
181 } catch (IOException e) {
182 throw new RuntimeException(e);
183 }
184 }
185 @GetMapping("/addRepairShop")
186 public String getRepairShopView(Model model){
187 model.addAttribute("bodyContent","addRepairShop");
188 model.addAttribute("manufacturers",carManufacturerRepository.findAll());
189 return "master-template";
190 }
191 @PostMapping("/addRepairShop")
192 public void saveRepairShop(@RequestParam String name,@RequestParam String location,
193 @RequestParam String number,@RequestParam Integer carMId,
194 HttpServletResponse response) {
195 RepairShop newRs = new RepairShop(name,location,number,
196 List.of(carManufacturerRepository.findById(carMId).get()));
197 repairShopRepository.save(newRs);
198 rsForCmRepository.save(new RsForCm(newRs.getId(), carMId));
199 try {
200 response.sendRedirect("/");
201 } catch (IOException e) {
202 throw new RuntimeException(e);
203 }
204 }
205 @GetMapping("/addWarehouse")
206 public String getWarehouseView(Model model){
207 model.addAttribute("bodyContent","addWarehouse");
208 return "master-template";
209 }
210 @PostMapping("/addWarehouse")
211 public void saveWarehouse(@RequestParam String name,
212 HttpServletResponse response) {
213 warehouseRepository.save(new Warehouse(name));
214 try {
215 response.sendRedirect("/");
216 } catch (IOException e) {
217 throw new RuntimeException(e);
218 }
219 }
220}
Note: See TracBrowser for help on using the repository browser.