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