source: src/main/java/com/example/autopartz/controller/HomeController.java@ 37966cf

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

Filtered parts fixed

  • Property mode set to 100644
File size: 15.8 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("selectedCar","Previously selected : "+cartype);
132 model.addAttribute("selectedCategory","Previously selected : " + category);
133 model.addAttribute("cars",carService.findAll());
134 model.addAttribute("categories",categoryService.findAll());
135 model.addAttribute("bodyContent","filteredParts");
136 return "master-template";
137 }
138 @GetMapping("/login")
139 public String getLoginPage(Model model){
140 model.addAttribute("bodyContent","login");
141 return "master-template";
142 }
143 @GetMapping("/register")
144 public String getRegisterPage(Model model){
145 model.addAttribute("bodyContent","register");
146 return "master-template";
147 }
148 @PostMapping("/login")
149 public void handleLogin(@RequestParam String username, @RequestParam String password){
150 User u = loginService.login(username,password);
151 System.out.println(u.getName());
152 }
153 @PostMapping("/register")
154 public void handleRegister(@RequestParam String username, @RequestParam String name,
155 @RequestParam String password, @RequestParam String rpassword,
156 @RequestParam String email, @RequestParam String number,
157 @RequestParam String role, HttpServletResponse response, HttpSession session){
158 System.out.println(username + name + password + rpassword + email + number + role);
159 if(Objects.equals(role, "warehouseman")){
160 session.setAttribute("username", username);
161 session.setAttribute("name", name);
162 session.setAttribute("password", password);
163 session.setAttribute("rpassword", rpassword);
164 session.setAttribute("email", email);
165 session.setAttribute("number", number);
166 try {
167 response.sendRedirect("/registerWarehouseman");
168 } catch (IOException e) {
169 throw new RuntimeException(e);
170 }
171 }
172 else {
173 loginService.register(name, username, email, number, password, role);
174 try {
175 response.sendRedirect("/login");
176 } catch (IOException e) {
177 throw new RuntimeException(e);
178 }
179 }
180 }
181 @GetMapping("/registerWarehouseman")
182 public String getSelectPage(Model model){
183 model.addAttribute("locations",warehouseRepository.findAll());
184 model.addAttribute("bodyContent","selectWarehouse");
185 return "master-template";
186 }
187 @PostMapping("/finishRegister")
188 public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
189 System.out.println("here?");
190 String username = (String) session.getAttribute("username");
191 String name = (String) session.getAttribute("name");
192 String password = (String) session.getAttribute("password");
193 String email = (String) session.getAttribute("email");
194 String number = (String) session.getAttribute("number");
195 Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
196 loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
197 try {
198 response.sendRedirect("/login");
199 } catch (IOException e) {
200 throw new RuntimeException(e);
201 }
202 }
203 @GetMapping("/access_denied")
204 public String accessDenied(Model model){
205 model.addAttribute("bodyContent","access_denied");
206 return "master-template";
207 }
208 @GetMapping("/myWarehouse")
209 public String myWarehouse(Model model, HttpServletRequest request){
210 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
211 Warehouse warehouse = whm.getWarehouse();
212 List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getId());
213 model.addAttribute("bodyContent","myWarehouse");
214 model.addAttribute("warehouse",warehouse);
215 model.addAttribute("parts", partIsInStockInWarehouseList);
216 return "master-template";
217 }
218 @GetMapping("myDeliveries")
219 public String myDeliveries(Model model, HttpServletRequest request){
220 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
221 List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
222 model.addAttribute("bodyContent","myDeliveries");
223 model.addAttribute("deliveries",deliveries);
224 return "master-template";
225 }
226 @GetMapping("myNextDeliveries")
227 public String myNextDeliveries(Model model, HttpServletRequest request){
228 Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
229 List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
230 if(ldip.size()==0){
231 model.addAttribute("hasError",true);
232 model.addAttribute("error","Сите достави се завршени");
233 }
234 else {
235 model.addAttribute("hasError",false);
236 model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
237 }
238 model.addAttribute("bodyContent","myNextDeliveries");
239 return "master-template";
240 }
241 @PostMapping("/finishDelivery/{id}")
242 public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
243 Delivery d = deliveryService.findByOrder(orderService.findById(id));
244 d.setStatus("finished");
245 deliveryService.update(d);
246 try {
247 response.sendRedirect("/myDeliveries");
248 } catch (IOException e) {
249 throw new RuntimeException(e);
250 }
251 }
252 @GetMapping("/order/{id}")
253 public String getOrderInfo(@PathVariable Integer id, Model model){
254 List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
255 List<OrderInfo> partList = new ArrayList<>();
256 for (int i = 0; i < list.size(); i++) {
257 OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
258 list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
259 partList.add(oi);
260 }
261 model.addAttribute("parts",partList);
262 model.addAttribute("o",orderService.findById(id));
263 model.addAttribute("bodyContent","orderInfo");
264 return "master-template";
265 }
266 @GetMapping("/carCategoryReport")
267 public String getCarCategoryInfo(Model model){
268 model.addAttribute("data",carCategoryReportRepository.findAll());
269 model.addAttribute("bodyContent","carCategoryReport");
270 return "master-template";
271 }
272 @GetMapping("/partManufacturersReport")
273 public String getPartManufacturersReport(Model model){
274 model.addAttribute("data",partManufacturersReportRepository.findAll());
275 model.addAttribute("bodyContent","partManufacturersReport");
276 return "master-template";
277
278 }
279 @GetMapping("/mostPurchasedPart")
280 public String getMostPurchasedPart(Model model){
281 model.addAttribute("data",mostPurchasedPartRepository.findAll());
282 model.addAttribute("bodyContent","mostPurchasedPart");
283 return "master-template";
284 }
285 @GetMapping("/myWarehouseReport")
286 public String getMyWarehouseReport(Model model, HttpServletRequest request){
287 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
288 Warehouse wh = whm.getWarehouse();
289 Integer whId = wh.getId();
290 model.addAttribute("data", warehousemanReportRepository.findByWid(whId));
291 model.addAttribute("bodyContent","myWarehouseReport");
292 return "master-template";
293 }
294 @PostMapping("/myWarehouse/{pname}")
295 public void addPartToWarehouse(@PathVariable String pname,@RequestParam Integer quantity, HttpServletRequest request, HttpServletResponse response){
296 Integer pId = partRepository.findAllByName(pname).stream().findFirst().get().getId();
297 Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
298 Warehouse wh = whm.getWarehouse();
299 Integer whId = wh.getId();
300 PartIsInStockInWarehouseId tmp = new PartIsInStockInWarehouseId(pId,whId);
301 PartIsInStockInWarehouse temp = partIsInStockInWarehouseRepository.findById(tmp).get();
302 temp.setQuantity(temp.getQuantity()+quantity);
303 partIsInStockInWarehouseRepository.save(temp);
304 try {
305 response.sendRedirect("/myWarehouseReport");
306 } catch (IOException e) {
307 throw new RuntimeException(e);
308 }
309 }
310}
Note: See TracBrowser for help on using the repository browser.