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