1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.*;
|
---|
4 | import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
|
---|
5 | import com.example.autopartz.repository.*;
|
---|
6 | import com.example.autopartz.service.*;
|
---|
7 | import org.springframework.stereotype.Controller;
|
---|
8 | import org.springframework.ui.Model;
|
---|
9 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
10 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
11 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
13 |
|
---|
14 | import javax.servlet.http.HttpServletRequest;
|
---|
15 | import javax.servlet.http.HttpServletResponse;
|
---|
16 | import javax.servlet.http.HttpSession;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Objects;
|
---|
20 |
|
---|
21 | @Controller
|
---|
22 | @RequestMapping("/")
|
---|
23 | public class HomeController {
|
---|
24 | private final LoginService loginService;
|
---|
25 | private final PartService partService;
|
---|
26 | private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
|
---|
27 | private final CarService carService;
|
---|
28 | private final CategoryService categoryService;
|
---|
29 | private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
|
---|
30 | private final WarehouseRepository warehouseRepository;
|
---|
31 | private final OrderContainsPartRepository orderContainsPartRepository;
|
---|
32 | private final OrderService orderService;
|
---|
33 | private final UserService userService;
|
---|
34 | private final DeliveryService deliveryService;
|
---|
35 | private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
|
---|
36 | public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
|
---|
37 | OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository) {
|
---|
38 | this.loginService = loginService;
|
---|
39 | this.partService = partService;
|
---|
40 | this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
|
---|
41 | this.carService = carService;
|
---|
42 | this.categoryService = categoryService;
|
---|
43 | this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
|
---|
44 | this.warehouseRepository = warehouseRepository;
|
---|
45 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
46 | this.orderService = orderService;
|
---|
47 | this.userService = userService;
|
---|
48 | this.deliveryService = deliveryService;
|
---|
49 | this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping()
|
---|
53 | public String getHomePage(Model model, HttpServletRequest request){
|
---|
54 | model.addAttribute("bodyContent","home");
|
---|
55 | model.addAttribute("user",request.getRemoteUser());
|
---|
56 | return "master-template";
|
---|
57 | }
|
---|
58 | @GetMapping("/products")
|
---|
59 | public String getProducts(Model model){
|
---|
60 | model.addAttribute("parts",partService.findAll());
|
---|
61 | model.addAttribute("cars",carService.findAll());
|
---|
62 | model.addAttribute("categories",categoryService.findAll());
|
---|
63 | model.addAttribute("bodyContent","products");
|
---|
64 | return "master-template";
|
---|
65 | }
|
---|
66 | @GetMapping("/services")
|
---|
67 | public String getServices(Model model){
|
---|
68 | model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
|
---|
69 | model.addAttribute("bodyContent","services");
|
---|
70 | return "master-template";
|
---|
71 | }
|
---|
72 | @GetMapping("/currentOrder")
|
---|
73 | public String getCurrentOrder(Model model,HttpSession session){
|
---|
74 | if(session.getAttribute("order")==null){
|
---|
75 | model.addAttribute("hasError",true);
|
---|
76 | model.addAttribute("error","Нарачката е празна");
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | Order o = (Order) session.getAttribute("order");
|
---|
80 | model.addAttribute("hasError",false);
|
---|
81 | model.addAttribute("order",o);
|
---|
82 | model.addAttribute("parts",orderService.findById(o.getOrderid()).getPartList());
|
---|
83 | }
|
---|
84 | model.addAttribute("bodyContent","currentOrder");
|
---|
85 | return "master-template";
|
---|
86 | }
|
---|
87 | @GetMapping("/filtered")
|
---|
88 | public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
|
---|
89 | model.addAttribute("filtered", partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category));
|
---|
90 | model.addAttribute("bodyContent","filteredParts");
|
---|
91 | return "master-template";
|
---|
92 | }
|
---|
93 | @GetMapping("/login")
|
---|
94 | public String getLoginPage(Model model){
|
---|
95 | model.addAttribute("bodyContent","login");
|
---|
96 | return "master-template";
|
---|
97 | }
|
---|
98 | @GetMapping("/register")
|
---|
99 | public String getRegisterPage(Model model){
|
---|
100 | model.addAttribute("bodyContent","register");
|
---|
101 | return "master-template";
|
---|
102 | }
|
---|
103 | @PostMapping("/login")
|
---|
104 | public void handleLogin(@RequestParam String username, @RequestParam String password){
|
---|
105 | User u = loginService.login(username,password);
|
---|
106 | System.out.println(u.getName());
|
---|
107 | }
|
---|
108 | @PostMapping("/register")
|
---|
109 | public void handleRegister(@RequestParam String username, @RequestParam String name,
|
---|
110 | @RequestParam String password, @RequestParam String rpassword,
|
---|
111 | @RequestParam String email, @RequestParam String number,
|
---|
112 | @RequestParam String role, HttpServletResponse response, HttpSession session){
|
---|
113 | System.out.println(username + name + password + rpassword + email + number + role);
|
---|
114 | if(Objects.equals(role, "warehouseman")){
|
---|
115 | session.setAttribute("username", username);
|
---|
116 | session.setAttribute("name", name);
|
---|
117 | session.setAttribute("password", password);
|
---|
118 | session.setAttribute("rpassword", rpassword);
|
---|
119 | session.setAttribute("email", email);
|
---|
120 | session.setAttribute("number", number);
|
---|
121 | try {
|
---|
122 | response.sendRedirect("/registerWarehouseman");
|
---|
123 | } catch (IOException e) {
|
---|
124 | throw new RuntimeException(e);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | loginService.register(name, username, email, number, password, role);
|
---|
129 | try {
|
---|
130 | response.sendRedirect("/login");
|
---|
131 | } catch (IOException e) {
|
---|
132 | throw new RuntimeException(e);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | @GetMapping("/registerWarehouseman")
|
---|
137 | public String getSelectPage(Model model){
|
---|
138 | model.addAttribute("locations",warehouseRepository.findAll());
|
---|
139 | model.addAttribute("bodyContent","selectWarehouse");
|
---|
140 | return "master-template";
|
---|
141 | }
|
---|
142 | @PostMapping("/finishRegister")
|
---|
143 | public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
|
---|
144 | System.out.println("here?");
|
---|
145 | String username = (String) session.getAttribute("username");
|
---|
146 | String name = (String) session.getAttribute("name");
|
---|
147 | String password = (String) session.getAttribute("password");
|
---|
148 | String email = (String) session.getAttribute("email");
|
---|
149 | String number = (String) session.getAttribute("number");
|
---|
150 | Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
|
---|
151 | loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
|
---|
152 | try {
|
---|
153 | response.sendRedirect("/login");
|
---|
154 | } catch (IOException e) {
|
---|
155 | throw new RuntimeException(e);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | @GetMapping("/access_denied")
|
---|
159 | public String accessDenied(Model model){
|
---|
160 | model.addAttribute("bodyContent","access_denied");
|
---|
161 | return "master-template";
|
---|
162 | }
|
---|
163 | @GetMapping("/myWarehouse")
|
---|
164 | public String myWarehouse(Model model, HttpServletRequest request){
|
---|
165 | Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
|
---|
166 | Warehouse warehouse = whm.getWarehouse();
|
---|
167 | List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getID_warehouse());
|
---|
168 | model.addAttribute("bodyContent","myWarehouse");
|
---|
169 | model.addAttribute("warehouse",warehouse);
|
---|
170 | model.addAttribute("parts", partIsInStockInWarehouseList);
|
---|
171 | return "master-template";
|
---|
172 | }
|
---|
173 | @GetMapping("myDeliveries")
|
---|
174 | public String myDeliveries(Model model, HttpServletRequest request){
|
---|
175 | Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
|
---|
176 | List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
|
---|
177 | model.addAttribute("bodyContent","myDeliveries");
|
---|
178 | model.addAttribute("deliveries",deliveries);
|
---|
179 | return "master-template";
|
---|
180 | }
|
---|
181 | }
|
---|