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

main
Last change on this file was 2a552fe, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 16 months ago

Added transactional methods to secure the database integrity and refactoring

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