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