[feffc2f] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
[1bd8d1e] | 3 | import com.example.autopartz.model.*;
|
---|
[89865ae] | 4 | import com.example.autopartz.model.DTO.CurrentOrderDTO;
|
---|
[9dcbf44] | 5 | import com.example.autopartz.model.DTO.OrderInfo;
|
---|
| 6 | import com.example.autopartz.model.manytomany.OrderContainsPart;
|
---|
[1bd8d1e] | 7 | import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
|
---|
[4d67d70] | 8 | import com.example.autopartz.model.manytomany.PartIsInStockInWarehouseId;
|
---|
[9dcbf44] | 9 | import com.example.autopartz.model.views.DeliveriesInProgress;
|
---|
| 10 | import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
|
---|
[1bd8d1e] | 11 | import com.example.autopartz.repository.*;
|
---|
[7d43957] | 12 | import com.example.autopartz.service.*;
|
---|
[feffc2f] | 13 | import org.springframework.stereotype.Controller;
|
---|
| 14 | import org.springframework.ui.Model;
|
---|
[9dcbf44] | 15 | import org.springframework.web.bind.annotation.*;
|
---|
[feffc2f] | 16 |
|
---|
[ae042f4] | 17 | import javax.servlet.http.HttpServletRequest;
|
---|
[7d43957] | 18 | import javax.servlet.http.HttpServletResponse;
|
---|
| 19 | import javax.servlet.http.HttpSession;
|
---|
| 20 | import java.io.IOException;
|
---|
[9dcbf44] | 21 | import java.util.ArrayList;
|
---|
[1bd8d1e] | 22 | import java.util.List;
|
---|
[7d43957] | 23 | import java.util.Objects;
|
---|
[ae042f4] | 24 |
|
---|
[feffc2f] | 25 | @Controller
|
---|
| 26 | @RequestMapping("/")
|
---|
| 27 | public class HomeController {
|
---|
| 28 | private final LoginService loginService;
|
---|
| 29 | private final PartService partService;
|
---|
[60de3eb] | 30 | private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
|
---|
| 31 | private final CarService carService;
|
---|
| 32 | private final CategoryService categoryService;
|
---|
[6832924] | 33 | private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
|
---|
[7d43957] | 34 | private final WarehouseRepository warehouseRepository;
|
---|
| 35 | private final OrderContainsPartRepository orderContainsPartRepository;
|
---|
| 36 | private final OrderService orderService;
|
---|
[1bd8d1e] | 37 | private final UserService userService;
|
---|
[9dcbf44] | 38 | private final DeliveriesInProgressRepository deliveriesInProgressRepository;
|
---|
[1bd8d1e] | 39 | private final DeliveryService deliveryService;
|
---|
| 40 | private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
|
---|
[cab5859] | 41 | private final CarCategoryReportRepository carCategoryReportRepository;
|
---|
| 42 | private final PartManufacturersReportRepository partManufacturersReportRepository;
|
---|
| 43 | private final MostPurchasedPartRepository mostPurchasedPartRepository;
|
---|
[89865ae] | 44 | private final PriceService priceService;
|
---|
[4d67d70] | 45 | private final WarehousemanReportRepository warehousemanReportRepository;
|
---|
| 46 | private final PartRepository partRepository;
|
---|
[7d43957] | 47 | public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
|
---|
[4d67d70] | 48 | OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, CarCategoryReportRepository carCategoryReportRepository, PartManufacturersReportRepository partManufacturersReportRepository, MostPurchasedPartRepository mostPurchasedPartRepository, PriceService priceService, WarehousemanReportRepository warehousemanReportRepository, PartRepository partRepository) {
|
---|
[feffc2f] | 49 | this.loginService = loginService;
|
---|
| 50 | this.partService = partService;
|
---|
[60de3eb] | 51 | this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
|
---|
| 52 | this.carService = carService;
|
---|
| 53 | this.categoryService = categoryService;
|
---|
[6832924] | 54 | this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
|
---|
[7d43957] | 55 | this.warehouseRepository = warehouseRepository;
|
---|
| 56 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
| 57 | this.orderService = orderService;
|
---|
[1bd8d1e] | 58 | this.userService = userService;
|
---|
[9dcbf44] | 59 | this.deliveriesInProgressRepository = deliveriesInProgressRepository;
|
---|
[1bd8d1e] | 60 | this.deliveryService = deliveryService;
|
---|
| 61 | this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
|
---|
[cab5859] | 62 | this.carCategoryReportRepository = carCategoryReportRepository;
|
---|
| 63 | this.partManufacturersReportRepository = partManufacturersReportRepository;
|
---|
| 64 | this.mostPurchasedPartRepository = mostPurchasedPartRepository;
|
---|
[89865ae] | 65 | this.priceService = priceService;
|
---|
[4d67d70] | 66 | this.warehousemanReportRepository = warehousemanReportRepository;
|
---|
| 67 | this.partRepository = partRepository;
|
---|
[feffc2f] | 68 | }
|
---|
| 69 |
|
---|
| 70 | @GetMapping()
|
---|
[ae042f4] | 71 | public String getHomePage(Model model, HttpServletRequest request){
|
---|
[6832924] | 72 | model.addAttribute("bodyContent","home");
|
---|
[ae042f4] | 73 | model.addAttribute("user",request.getRemoteUser());
|
---|
[6832924] | 74 | return "master-template";
|
---|
| 75 | }
|
---|
| 76 | @GetMapping("/products")
|
---|
| 77 | public String getProducts(Model model){
|
---|
[feffc2f] | 78 | model.addAttribute("parts",partService.findAll());
|
---|
[60de3eb] | 79 | model.addAttribute("cars",carService.findAll());
|
---|
| 80 | model.addAttribute("categories",categoryService.findAll());
|
---|
[6832924] | 81 | model.addAttribute("bodyContent","products");
|
---|
| 82 | return "master-template";
|
---|
| 83 | }
|
---|
| 84 | @GetMapping("/services")
|
---|
| 85 | public String getServices(Model model){
|
---|
| 86 | model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
|
---|
| 87 | model.addAttribute("bodyContent","services");
|
---|
[60de3eb] | 88 | return "master-template";
|
---|
| 89 | }
|
---|
[7d43957] | 90 | @GetMapping("/currentOrder")
|
---|
| 91 | public String getCurrentOrder(Model model,HttpSession session){
|
---|
| 92 | if(session.getAttribute("order")==null){
|
---|
| 93 | model.addAttribute("hasError",true);
|
---|
| 94 | model.addAttribute("error","Нарачката е празна");
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
| 97 | Order o = (Order) session.getAttribute("order");
|
---|
| 98 | model.addAttribute("hasError",false);
|
---|
| 99 | model.addAttribute("order",o);
|
---|
[89865ae] | 100 | List<CurrentOrderDTO> list = new ArrayList<>();
|
---|
| 101 | int total = 0;
|
---|
| 102 | List<OrderContainsPart> qList = orderContainsPartRepository.findAllByOrderid(o.getOrderid());
|
---|
| 103 | for (int i = 0; i < qList.size(); i++) {
|
---|
| 104 | int pr = qList.get(i).getQuantity_order()*
|
---|
| 105 | priceService.findPriceForPart(partService.findById(qList.get(i).getPartid())).stream().findFirst().get().getAmount();
|
---|
| 106 | CurrentOrderDTO temp = new CurrentOrderDTO(
|
---|
| 107 | partService.findById(qList.get(i).getPartid()).getName(),
|
---|
| 108 | partService.findById(qList.get(i).getPartid()).getManufacturer().getName(),
|
---|
| 109 | qList.get(i).getQuantity_order(),
|
---|
| 110 | pr);
|
---|
| 111 | list.add(temp);
|
---|
| 112 | total+=pr;
|
---|
| 113 | }
|
---|
| 114 | model.addAttribute("total",total);
|
---|
| 115 | model.addAttribute("parts",list);
|
---|
[7d43957] | 116 | }
|
---|
| 117 | model.addAttribute("bodyContent","currentOrder");
|
---|
| 118 | return "master-template";
|
---|
| 119 | }
|
---|
[60de3eb] | 120 | @GetMapping("/filtered")
|
---|
| 121 | public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
|
---|
[9dcbf44] | 122 | List<PartsForCarTypeAndCategory> tmp = partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category);
|
---|
| 123 | if(tmp.size()==0){
|
---|
| 124 | model.addAttribute("hasError",true);
|
---|
| 125 | model.addAttribute("error","Не постојат такви производи, обидете се повторно");
|
---|
| 126 | }
|
---|
| 127 | else {
|
---|
| 128 | model.addAttribute("hasError",false);
|
---|
| 129 | model.addAttribute("filtered", tmp);
|
---|
| 130 | }
|
---|
[37966cf] | 131 | model.addAttribute("selectedCar","Previously selected : "+cartype);
|
---|
| 132 | model.addAttribute("selectedCategory","Previously selected : " + category);
|
---|
| 133 | model.addAttribute("cars",carService.findAll());
|
---|
| 134 | model.addAttribute("categories",categoryService.findAll());
|
---|
[60de3eb] | 135 | model.addAttribute("bodyContent","filteredParts");
|
---|
| 136 | return "master-template";
|
---|
[feffc2f] | 137 | }
|
---|
| 138 | @GetMapping("/login")
|
---|
[60de3eb] | 139 | public String getLoginPage(Model model){
|
---|
| 140 | model.addAttribute("bodyContent","login");
|
---|
| 141 | return "master-template";
|
---|
[feffc2f] | 142 | }
|
---|
| 143 | @GetMapping("/register")
|
---|
[60de3eb] | 144 | public String getRegisterPage(Model model){
|
---|
| 145 | model.addAttribute("bodyContent","register");
|
---|
| 146 | return "master-template";
|
---|
[feffc2f] | 147 | }
|
---|
| 148 | @PostMapping("/login")
|
---|
| 149 | public void handleLogin(@RequestParam String username, @RequestParam String password){
|
---|
| 150 | User u = loginService.login(username,password);
|
---|
[676144b] | 151 | System.out.println(u.getName());
|
---|
[feffc2f] | 152 | }
|
---|
| 153 | @PostMapping("/register")
|
---|
| 154 | public void handleRegister(@RequestParam String username, @RequestParam String name,
|
---|
[7d43957] | 155 | @RequestParam String password, @RequestParam String rpassword,
|
---|
| 156 | @RequestParam String email, @RequestParam String number,
|
---|
| 157 | @RequestParam String role, HttpServletResponse response, HttpSession session){
|
---|
| 158 | System.out.println(username + name + password + rpassword + email + number + role);
|
---|
| 159 | if(Objects.equals(role, "warehouseman")){
|
---|
| 160 | session.setAttribute("username", username);
|
---|
| 161 | session.setAttribute("name", name);
|
---|
| 162 | session.setAttribute("password", password);
|
---|
| 163 | session.setAttribute("rpassword", rpassword);
|
---|
| 164 | session.setAttribute("email", email);
|
---|
| 165 | session.setAttribute("number", number);
|
---|
| 166 | try {
|
---|
| 167 | response.sendRedirect("/registerWarehouseman");
|
---|
| 168 | } catch (IOException e) {
|
---|
| 169 | throw new RuntimeException(e);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | else {
|
---|
| 173 | loginService.register(name, username, email, number, password, role);
|
---|
| 174 | try {
|
---|
| 175 | response.sendRedirect("/login");
|
---|
| 176 | } catch (IOException e) {
|
---|
| 177 | throw new RuntimeException(e);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | @GetMapping("/registerWarehouseman")
|
---|
| 182 | public String getSelectPage(Model model){
|
---|
| 183 | model.addAttribute("locations",warehouseRepository.findAll());
|
---|
| 184 | model.addAttribute("bodyContent","selectWarehouse");
|
---|
| 185 | return "master-template";
|
---|
| 186 | }
|
---|
| 187 | @PostMapping("/finishRegister")
|
---|
| 188 | public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
|
---|
| 189 | System.out.println("here?");
|
---|
| 190 | String username = (String) session.getAttribute("username");
|
---|
| 191 | String name = (String) session.getAttribute("name");
|
---|
| 192 | String password = (String) session.getAttribute("password");
|
---|
| 193 | String email = (String) session.getAttribute("email");
|
---|
| 194 | String number = (String) session.getAttribute("number");
|
---|
| 195 | Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
|
---|
| 196 | loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
|
---|
| 197 | try {
|
---|
| 198 | response.sendRedirect("/login");
|
---|
| 199 | } catch (IOException e) {
|
---|
| 200 | throw new RuntimeException(e);
|
---|
| 201 | }
|
---|
[feffc2f] | 202 | }
|
---|
[1bd8d1e] | 203 | @GetMapping("/access_denied")
|
---|
| 204 | public String accessDenied(Model model){
|
---|
| 205 | model.addAttribute("bodyContent","access_denied");
|
---|
| 206 | return "master-template";
|
---|
| 207 | }
|
---|
| 208 | @GetMapping("/myWarehouse")
|
---|
| 209 | public String myWarehouse(Model model, HttpServletRequest request){
|
---|
| 210 | Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
|
---|
| 211 | Warehouse warehouse = whm.getWarehouse();
|
---|
[4d67d70] | 212 | List<PartIsInStockInWarehouse> partIsInStockInWarehouseList = partIsInStockInWarehouseRepository.findAllByWarehouseid(warehouse.getId());
|
---|
[1bd8d1e] | 213 | model.addAttribute("bodyContent","myWarehouse");
|
---|
| 214 | model.addAttribute("warehouse",warehouse);
|
---|
| 215 | model.addAttribute("parts", partIsInStockInWarehouseList);
|
---|
| 216 | return "master-template";
|
---|
| 217 | }
|
---|
| 218 | @GetMapping("myDeliveries")
|
---|
| 219 | public String myDeliveries(Model model, HttpServletRequest request){
|
---|
| 220 | Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
|
---|
| 221 | List<Delivery> deliveries = deliveryService.findAllByDeliverer(dm);
|
---|
| 222 | model.addAttribute("bodyContent","myDeliveries");
|
---|
| 223 | model.addAttribute("deliveries",deliveries);
|
---|
| 224 | return "master-template";
|
---|
| 225 | }
|
---|
[9dcbf44] | 226 | @GetMapping("myNextDeliveries")
|
---|
| 227 | public String myNextDeliveries(Model model, HttpServletRequest request){
|
---|
| 228 | Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
|
---|
| 229 | List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
|
---|
| 230 | if(ldip.size()==0){
|
---|
| 231 | model.addAttribute("hasError",true);
|
---|
| 232 | model.addAttribute("error","Сите достави се завршени");
|
---|
| 233 | }
|
---|
| 234 | else {
|
---|
| 235 | model.addAttribute("hasError",false);
|
---|
| 236 | model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
|
---|
| 237 | }
|
---|
| 238 | model.addAttribute("bodyContent","myNextDeliveries");
|
---|
| 239 | return "master-template";
|
---|
| 240 | }
|
---|
| 241 | @PostMapping("/finishDelivery/{id}")
|
---|
| 242 | public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
|
---|
| 243 | Delivery d = deliveryService.findByOrder(orderService.findById(id));
|
---|
| 244 | d.setStatus("finished");
|
---|
| 245 | deliveryService.update(d);
|
---|
| 246 | try {
|
---|
| 247 | response.sendRedirect("/myDeliveries");
|
---|
| 248 | } catch (IOException e) {
|
---|
| 249 | throw new RuntimeException(e);
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | @GetMapping("/order/{id}")
|
---|
| 253 | public String getOrderInfo(@PathVariable Integer id, Model model){
|
---|
| 254 | List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
|
---|
| 255 | List<OrderInfo> partList = new ArrayList<>();
|
---|
| 256 | for (int i = 0; i < list.size(); i++) {
|
---|
| 257 | OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
|
---|
| 258 | list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
|
---|
| 259 | partList.add(oi);
|
---|
| 260 | }
|
---|
| 261 | model.addAttribute("parts",partList);
|
---|
| 262 | model.addAttribute("o",orderService.findById(id));
|
---|
| 263 | model.addAttribute("bodyContent","orderInfo");
|
---|
| 264 | return "master-template";
|
---|
| 265 | }
|
---|
[cab5859] | 266 | @GetMapping("/carCategoryReport")
|
---|
| 267 | public String getCarCategoryInfo(Model model){
|
---|
| 268 | model.addAttribute("data",carCategoryReportRepository.findAll());
|
---|
| 269 | model.addAttribute("bodyContent","carCategoryReport");
|
---|
| 270 | return "master-template";
|
---|
| 271 | }
|
---|
| 272 | @GetMapping("/partManufacturersReport")
|
---|
| 273 | public String getPartManufacturersReport(Model model){
|
---|
| 274 | model.addAttribute("data",partManufacturersReportRepository.findAll());
|
---|
| 275 | model.addAttribute("bodyContent","partManufacturersReport");
|
---|
| 276 | return "master-template";
|
---|
| 277 |
|
---|
| 278 | }
|
---|
| 279 | @GetMapping("/mostPurchasedPart")
|
---|
| 280 | public String getMostPurchasedPart(Model model){
|
---|
| 281 | model.addAttribute("data",mostPurchasedPartRepository.findAll());
|
---|
| 282 | model.addAttribute("bodyContent","mostPurchasedPart");
|
---|
| 283 | return "master-template";
|
---|
| 284 | }
|
---|
[4d67d70] | 285 | @GetMapping("/myWarehouseReport")
|
---|
| 286 | public String getMyWarehouseReport(Model model, HttpServletRequest request){
|
---|
| 287 | Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
|
---|
| 288 | Warehouse wh = whm.getWarehouse();
|
---|
| 289 | Integer whId = wh.getId();
|
---|
| 290 | model.addAttribute("data", warehousemanReportRepository.findByWid(whId));
|
---|
| 291 | model.addAttribute("bodyContent","myWarehouseReport");
|
---|
| 292 | return "master-template";
|
---|
| 293 | }
|
---|
| 294 | @PostMapping("/myWarehouse/{pname}")
|
---|
| 295 | public void addPartToWarehouse(@PathVariable String pname,@RequestParam Integer quantity, HttpServletRequest request, HttpServletResponse response){
|
---|
| 296 | Integer pId = partRepository.findAllByName(pname).stream().findFirst().get().getId();
|
---|
| 297 | Warehouseman whm = (Warehouseman) userService.findByUsername(request.getRemoteUser());
|
---|
| 298 | Warehouse wh = whm.getWarehouse();
|
---|
| 299 | Integer whId = wh.getId();
|
---|
| 300 | PartIsInStockInWarehouseId tmp = new PartIsInStockInWarehouseId(pId,whId);
|
---|
| 301 | PartIsInStockInWarehouse temp = partIsInStockInWarehouseRepository.findById(tmp).get();
|
---|
| 302 | temp.setQuantity(temp.getQuantity()+quantity);
|
---|
| 303 | partIsInStockInWarehouseRepository.save(temp);
|
---|
| 304 | try {
|
---|
| 305 | response.sendRedirect("/myWarehouseReport");
|
---|
| 306 | } catch (IOException e) {
|
---|
| 307 | throw new RuntimeException(e);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
[feffc2f] | 310 | }
|
---|