source: src/main/java/com/example/autopartz/controller/HomeController.java@ 60e9cc1

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

Sredeni zabeleshki po diskusija

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