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

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

Added all advanced reports, added bootstrap for every template

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