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