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

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

Added price and total price in currentOrder

  • Property mode set to 100644
File size: 13.8 KB
RevLine 
[feffc2f]1package com.example.autopartz.controller;
2
[1bd8d1e]3import com.example.autopartz.model.*;
[89865ae]4import com.example.autopartz.model.DTO.CurrentOrderDTO;
[9dcbf44]5import com.example.autopartz.model.DTO.OrderInfo;
6import com.example.autopartz.model.manytomany.OrderContainsPart;
[1bd8d1e]7import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
[9dcbf44]8import com.example.autopartz.model.views.DeliveriesInProgress;
9import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
[1bd8d1e]10import com.example.autopartz.repository.*;
[7d43957]11import com.example.autopartz.service.*;
[feffc2f]12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
[9dcbf44]14import org.springframework.web.bind.annotation.*;
[feffc2f]15
[ae042f4]16import javax.servlet.http.HttpServletRequest;
[7d43957]17import javax.servlet.http.HttpServletResponse;
18import javax.servlet.http.HttpSession;
19import java.io.IOException;
[9dcbf44]20import java.util.ArrayList;
[1bd8d1e]21import java.util.List;
[7d43957]22import java.util.Objects;
[ae042f4]23
[feffc2f]24@Controller
25@RequestMapping("/")
26public class HomeController {
27 private final LoginService loginService;
28 private final PartService partService;
[60de3eb]29 private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
30 private final CarService carService;
31 private final CategoryService categoryService;
[6832924]32 private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
[7d43957]33 private final WarehouseRepository warehouseRepository;
34 private final OrderContainsPartRepository orderContainsPartRepository;
35 private final OrderService orderService;
[1bd8d1e]36 private final UserService userService;
[9dcbf44]37 private final DeliveriesInProgressRepository deliveriesInProgressRepository;
[1bd8d1e]38 private final DeliveryService deliveryService;
39 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
[cab5859]40 private final CarCategoryReportRepository carCategoryReportRepository;
41 private final PartManufacturersReportRepository partManufacturersReportRepository;
42 private final MostPurchasedPartRepository mostPurchasedPartRepository;
[89865ae]43 private final PriceService priceService;
[7d43957]44 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
[89865ae]45 OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, CarCategoryReportRepository carCategoryReportRepository, PartManufacturersReportRepository partManufacturersReportRepository, MostPurchasedPartRepository mostPurchasedPartRepository, PriceService priceService) {
[feffc2f]46 this.loginService = loginService;
47 this.partService = partService;
[60de3eb]48 this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
49 this.carService = carService;
50 this.categoryService = categoryService;
[6832924]51 this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
[7d43957]52 this.warehouseRepository = warehouseRepository;
53 this.orderContainsPartRepository = orderContainsPartRepository;
54 this.orderService = orderService;
[1bd8d1e]55 this.userService = userService;
[9dcbf44]56 this.deliveriesInProgressRepository = deliveriesInProgressRepository;
[1bd8d1e]57 this.deliveryService = deliveryService;
58 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
[cab5859]59 this.carCategoryReportRepository = carCategoryReportRepository;
60 this.partManufacturersReportRepository = partManufacturersReportRepository;
61 this.mostPurchasedPartRepository = mostPurchasedPartRepository;
[89865ae]62 this.priceService = priceService;
[feffc2f]63 }
64
65 @GetMapping()
[ae042f4]66 public String getHomePage(Model model, HttpServletRequest request){
[6832924]67 model.addAttribute("bodyContent","home");
[ae042f4]68 model.addAttribute("user",request.getRemoteUser());
[6832924]69 return "master-template";
70 }
71 @GetMapping("/products")
72 public String getProducts(Model model){
[feffc2f]73 model.addAttribute("parts",partService.findAll());
[60de3eb]74 model.addAttribute("cars",carService.findAll());
75 model.addAttribute("categories",categoryService.findAll());
[6832924]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");
[60de3eb]83 return "master-template";
84 }
[7d43957]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);
[89865ae]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);
[7d43957]111 }
112 model.addAttribute("bodyContent","currentOrder");
113 return "master-template";
114 }
[60de3eb]115 @GetMapping("/filtered")
116 public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
[9dcbf44]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 }
[60de3eb]126 model.addAttribute("bodyContent","filteredParts");
127 return "master-template";
[feffc2f]128 }
129 @GetMapping("/login")
[60de3eb]130 public String getLoginPage(Model model){
131 model.addAttribute("bodyContent","login");
132 return "master-template";
[feffc2f]133 }
134 @GetMapping("/register")
[60de3eb]135 public String getRegisterPage(Model model){
136 model.addAttribute("bodyContent","register");
137 return "master-template";
[feffc2f]138 }
139 @PostMapping("/login")
140 public void handleLogin(@RequestParam String username, @RequestParam String password){
141 User u = loginService.login(username,password);
[676144b]142 System.out.println(u.getName());
[feffc2f]143 }
144 @PostMapping("/register")
145 public void handleRegister(@RequestParam String username, @RequestParam String name,
[7d43957]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 }
[feffc2f]193 }
[1bd8d1e]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 }
[9dcbf44]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 }
[cab5859]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 }
[feffc2f]276}
Note: See TracBrowser for help on using the repository browser.