source: src/main/java/com/example/autopartz/controller/HomeController.java@ 4d67d70

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

Final touches

  • Property mode set to 100644
File size: 15.5 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.model.DTO.CurrentOrderDTO;
5import com.example.autopartz.model.DTO.OrderInfo;
6import com.example.autopartz.model.manytomany.OrderContainsPart;
7import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
8import com.example.autopartz.model.manytomany.PartIsInStockInWarehouseId;
9import com.example.autopartz.model.views.DeliveriesInProgress;
10import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
11import com.example.autopartz.repository.*;
12import com.example.autopartz.service.*;
13import org.springframework.stereotype.Controller;
14import org.springframework.ui.Model;
15import org.springframework.web.bind.annotation.*;
16
17import javax.servlet.http.HttpServletRequest;
18import javax.servlet.http.HttpServletResponse;
19import javax.servlet.http.HttpSession;
20import java.io.IOException;
21import java.util.ArrayList;
22import java.util.List;
23import java.util.Objects;
24
25@Controller
26@RequestMapping("/")
27public class HomeController {
28 private final LoginService loginService;
29 private final PartService partService;
30 private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
31 private final CarService carService;
32 private final CategoryService categoryService;
33 private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
34 private final WarehouseRepository warehouseRepository;
35 private final OrderContainsPartRepository orderContainsPartRepository;
36 private final OrderService orderService;
37 private final UserService userService;
38 private final DeliveriesInProgressRepository deliveriesInProgressRepository;
39 private final DeliveryService deliveryService;
40 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
41 private final CarCategoryReportRepository carCategoryReportRepository;
42 private final PartManufacturersReportRepository partManufacturersReportRepository;
43 private final MostPurchasedPartRepository mostPurchasedPartRepository;
44 private final PriceService priceService;
45 private final WarehousemanReportRepository warehousemanReportRepository;
46 private final PartRepository partRepository;
47 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
48 OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, CarCategoryReportRepository carCategoryReportRepository, PartManufacturersReportRepository partManufacturersReportRepository, MostPurchasedPartRepository mostPurchasedPartRepository, PriceService priceService, WarehousemanReportRepository warehousemanReportRepository, PartRepository partRepository) {
49 this.loginService = loginService;
50 this.partService = partService;
51 this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
52 this.carService = carService;
53 this.categoryService = categoryService;
54 this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
55 this.warehouseRepository = warehouseRepository;
56 this.orderContainsPartRepository = orderContainsPartRepository;
57 this.orderService = orderService;
58 this.userService = userService;
59 this.deliveriesInProgressRepository = deliveriesInProgressRepository;
60 this.deliveryService = deliveryService;
61 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
62 this.carCategoryReportRepository = carCategoryReportRepository;
63 this.partManufacturersReportRepository = partManufacturersReportRepository;
64 this.mostPurchasedPartRepository = mostPurchasedPartRepository;
65 this.priceService = priceService;
66 this.warehousemanReportRepository = warehousemanReportRepository;
67 this.partRepository = partRepository;
68 }
69
70 @GetMapping()
71 public String getHomePage(Model model, HttpServletRequest request){
72 model.addAttribute("bodyContent","home");
73 model.addAttribute("user",request.getRemoteUser());
74 return "master-template";
75 }
76 @GetMapping("/products")
77 public String getProducts(Model model){
78 model.addAttribute("parts",partService.findAll());
79 model.addAttribute("cars",carService.findAll());
80 model.addAttribute("categories",categoryService.findAll());
81 model.addAttribute("bodyContent","products");
82 return "master-template";
83 }
84 @GetMapping("/services")
85 public String getServices(Model model){
86 model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
87 model.addAttribute("bodyContent","services");
88 return "master-template";
89 }
90 @GetMapping("/currentOrder")
91 public String getCurrentOrder(Model model,HttpSession session){
92 if(session.getAttribute("order")==null){
93 model.addAttribute("hasError",true);
94 model.addAttribute("error","Нарачката е празна");
95 }
96 else {
97 Order o = (Order) session.getAttribute("order");
98 model.addAttribute("hasError",false);
99 model.addAttribute("order",o);
100 List<CurrentOrderDTO> list = new ArrayList<>();
101 int total = 0;
102 List<OrderContainsPart> qList = orderContainsPartRepository.findAllByOrderid(o.getOrderid());
103 for (int i = 0; i < qList.size(); i++) {
104 int pr = qList.get(i).getQuantity_order()*
105 priceService.findPriceForPart(partService.findById(qList.get(i).getPartid())).stream().findFirst().get().getAmount();
106 CurrentOrderDTO temp = new CurrentOrderDTO(
107 partService.findById(qList.get(i).getPartid()).getName(),
108 partService.findById(qList.get(i).getPartid()).getManufacturer().getName(),
109 qList.get(i).getQuantity_order(),
110 pr);
111 list.add(temp);
112 total+=pr;
113 }
114 model.addAttribute("total",total);
115 model.addAttribute("parts",list);
116 }
117 model.addAttribute("bodyContent","currentOrder");
118 return "master-template";
119 }
120 @GetMapping("/filtered")
121 public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
122 List<PartsForCarTypeAndCategory> tmp = partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category);
123 if(tmp.size()==0){
124 model.addAttribute("hasError",true);
125 model.addAttribute("error","Не постојат такви производи, обидете се повторно");
126 }
127 else {
128 model.addAttribute("hasError",false);
129 model.addAttribute("filtered", tmp);
130 }
131 model.addAttribute("bodyContent","filteredParts");
132 return "master-template";
133 }
134 @GetMapping("/login")
135 public String getLoginPage(Model model){
136 model.addAttribute("bodyContent","login");
137 return "master-template";
138 }
139 @GetMapping("/register")
140 public String getRegisterPage(Model model){
141 model.addAttribute("bodyContent","register");
142 return "master-template";
143 }
144 @PostMapping("/login")
145 public void handleLogin(@RequestParam String username, @RequestParam String password){
146 User u = loginService.login(username,password);
147 System.out.println(u.getName());
148 }
149 @PostMapping("/register")
150 public void handleRegister(@RequestParam String username, @RequestParam String name,
151 @RequestParam String password, @RequestParam String rpassword,
152 @RequestParam String email, @RequestParam String number,
153 @RequestParam String role, HttpServletResponse response, HttpSession session){
154 System.out.println(username + name + password + rpassword + email + number + role);
155 if(Objects.equals(role, "warehouseman")){
156 session.setAttribute("username", username);
157 session.setAttribute("name", name);
158 session.setAttribute("password", password);
159 session.setAttribute("rpassword", rpassword);
160 session.setAttribute("email", email);
161 session.setAttribute("number", number);
162 try {
163 response.sendRedirect("/registerWarehouseman");
164 } catch (IOException e) {
165 throw new RuntimeException(e);
166 }
167 }
168 else {
169 loginService.register(name, username, email, number, password, role);
170 try {
171 response.sendRedirect("/login");
172 } catch (IOException e) {
173 throw new RuntimeException(e);
174 }
175 }
176 }
177 @GetMapping("/registerWarehouseman")
178 public String getSelectPage(Model model){
179 model.addAttribute("locations",warehouseRepository.findAll());
180 model.addAttribute("bodyContent","selectWarehouse");
181 return "master-template";
182 }
183 @PostMapping("/finishRegister")
184 public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
185 System.out.println("here?");
186 String username = (String) session.getAttribute("username");
187 String name = (String) session.getAttribute("name");
188 String password = (String) session.getAttribute("password");
189 String email = (String) session.getAttribute("email");
190 String number = (String) session.getAttribute("number");
191 Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
192 loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
193 try {
194 response.sendRedirect("/login");
195 } catch (IOException e) {
196 throw new RuntimeException(e);
197 }
198 }
199 @GetMapping("/access_denied")
200 public String accessDenied(Model model){
201 model.addAttribute("bodyContent","access_denied");
202 return "master-template";
203 }
204 @GetMapping("/myWarehouse")
205 public String myWarehouse(Model model, HttpServletRequest request){
206 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
207 Warehouse warehouse = whm.getWarehouse();
208 List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getId());
209 model.addAttribute("bodyContent","myWarehouse");
210 model.addAttribute("warehouse",warehouse);
211 model.addAttribute("parts", partIsInStockInWarehouseList);
212 return "master-template";
213 }
214 @GetMapping("myDeliveries")
215 public String myDeliveries(Model model, HttpServletRequest request){
216 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
217 List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
218 model.addAttribute("bodyContent","myDeliveries");
219 model.addAttribute("deliveries",deliveries);
220 return "master-template";
221 }
222 @GetMapping("myNextDeliveries")
223 public String myNextDeliveries(Model model, HttpServletRequest request){
224 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
225 List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
226 if(ldip.size()==0){
227 model.addAttribute("hasError",true);
228 model.addAttribute("error","Сите достави се завршени");
229 }
230 else {
231 model.addAttribute("hasError",false);
232 model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
233 }
234 model.addAttribute("bodyContent","myNextDeliveries");
235 return "master-template";
236 }
237 @PostMapping("/finishDelivery/{id}")
238 public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
239 Delivery d = deliveryService.findByOrder(orderService.findById(id));
240 d.setStatus("finished");
241 deliveryService.update(d);
242 try {
243 response.sendRedirect("/myDeliveries");
244 } catch (IOException e) {
245 throw new RuntimeException(e);
246 }
247 }
248 @GetMapping("/order/{id}")
249 public String getOrderInfo(@PathVariable Integer id, Model model){
250 List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
251 List<OrderInfo> partList = new ArrayList<>();
252 for (int i = 0; i < list.size(); i++) {
253 OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
254 list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
255 partList.add(oi);
256 }
257 model.addAttribute("parts",partList);
258 model.addAttribute("o",orderService.findById(id));
259 model.addAttribute("bodyContent","orderInfo");
260 return "master-template";
261 }
262 @GetMapping("/carCategoryReport")
263 public String getCarCategoryInfo(Model model){
264 model.addAttribute("data",carCategoryReportRepository.findAll());
265 model.addAttribute("bodyContent","carCategoryReport");
266 return "master-template";
267 }
268 @GetMapping("/partManufacturersReport")
269 public String getPartManufacturersReport(Model model){
270 model.addAttribute("data",partManufacturersReportRepository.findAll());
271 model.addAttribute("bodyContent","partManufacturersReport");
272 return "master-template";
273
274 }
275 @GetMapping("/mostPurchasedPart")
276 public String getMostPurchasedPart(Model model){
277 model.addAttribute("data",mostPurchasedPartRepository.findAll());
278 model.addAttribute("bodyContent","mostPurchasedPart");
279 return "master-template";
280 }
281 @GetMapping("/myWarehouseReport")
282 public String getMyWarehouseReport(Model model, HttpServletRequest request){
283 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
284 Warehouse wh = whm.getWarehouse();
285 Integer whId = wh.getId();
286 model.addAttribute("data", warehousemanReportRepository.findByWid(whId));
287 model.addAttribute("bodyContent","myWarehouseReport");
288 return "master-template";
289 }
290 @PostMapping("/myWarehouse/{pname}")
291 public void addPartToWarehouse(@PathVariable String pname,@RequestParam Integer quantity, HttpServletRequest request, HttpServletResponse response){
292 Integer pId = partRepository.findAllByName(pname).stream().findFirst().get().getId();
293 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
294 Warehouse wh = whm.getWarehouse();
295 Integer whId = wh.getId();
296 PartIsInStockInWarehouseId tmp = new PartIsInStockInWarehouseId(pId,whId);
297 PartIsInStockInWarehouse temp = partIsInStockInWarehouseRepository.findById(tmp).get();
298 temp.setQuantity(temp.getQuantity()+quantity);
299 partIsInStockInWarehouseRepository.save(temp);
300 try {
301 response.sendRedirect("/myWarehouseReport");
302 } catch (IOException e) {
303 throw new RuntimeException(e);
304 }
305 }
306}
Note: See TracBrowser for help on using the repository browser.