1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.Client;
|
---|
4 | import com.example.autopartz.model.Order;
|
---|
5 | import com.example.autopartz.model.Part;
|
---|
6 | import com.example.autopartz.model.User;
|
---|
7 | import com.example.autopartz.model.manytomany.OrderContainsPart;
|
---|
8 | import com.example.autopartz.repository.OrderContainsPartRepository;
|
---|
9 | import com.example.autopartz.service.*;
|
---|
10 | import org.springframework.stereotype.Controller;
|
---|
11 | import org.springframework.ui.Model;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
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 |
|
---|
19 | @Controller
|
---|
20 | @RequestMapping("/part")
|
---|
21 | public class PartController {
|
---|
22 | private final PartService partService;
|
---|
23 | private final RepairShopService repairShopService;
|
---|
24 | private final PriceService priceService;
|
---|
25 | private final OrderService orderService;
|
---|
26 | private final UserService userService;
|
---|
27 | private final OrderContainsPartRepository orderContainsPartRepository;
|
---|
28 | public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository) {
|
---|
29 | this.partService = partService;
|
---|
30 | this.repairShopService = repairShopService;
|
---|
31 | this.priceService = priceService;
|
---|
32 | this.orderService = orderService;
|
---|
33 | this.userService = userService;
|
---|
34 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
35 | }
|
---|
36 | @GetMapping("/{id}")
|
---|
37 | public String getPartPage(@PathVariable Integer id, Model model){
|
---|
38 | Part temp = partService.findById(id);
|
---|
39 | Integer amount = priceService.findPriceForPart(temp).stream().findFirst().orElseThrow(RuntimeException::new).getAmount();
|
---|
40 | model.addAttribute("part",temp);
|
---|
41 | model.addAttribute("amount",amount);
|
---|
42 | model.addAttribute("bodyContent","partinfo");
|
---|
43 | return "master-template";
|
---|
44 | }
|
---|
45 | @GetMapping("/delivery")
|
---|
46 | public String getDeliveryPage(Model model){
|
---|
47 | model.addAttribute("repairShops",repairShopService.findAll());
|
---|
48 | model.addAttribute("bodyContent","deliveryForPart");
|
---|
49 | return "master-template";
|
---|
50 | }
|
---|
51 | @PostMapping("/repairshopdelivery")
|
---|
52 | public void setRepairShopDelivery(@RequestParam String name, HttpServletResponse response, HttpSession session){
|
---|
53 | // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
|
---|
54 | session.removeAttribute("order");
|
---|
55 | try {
|
---|
56 | response.sendRedirect("/");
|
---|
57 | } catch (IOException e) {
|
---|
58 | throw new RuntimeException(e);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | @PostMapping("/homedelivery")
|
---|
62 | public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){
|
---|
63 | // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
|
---|
64 | session.removeAttribute("order");
|
---|
65 | try {
|
---|
66 | response.sendRedirect("/");
|
---|
67 | } catch (IOException e) {
|
---|
68 | throw new RuntimeException(e);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | @PostMapping("/addToOrder/{id}")
|
---|
72 | public void addToOrder(@PathVariable Integer id,@RequestParam Integer quantity, HttpSession session, HttpServletResponse response, HttpServletRequest request){
|
---|
73 | if(session.getAttribute("order")==null){
|
---|
74 | User u = userService.findByUsername(request.getRemoteUser());
|
---|
75 | Order newOrder = orderService.create((Client) u);
|
---|
76 | session.setAttribute("order",newOrder);
|
---|
77 | }
|
---|
78 | Order order = (Order) session.getAttribute("order");
|
---|
79 | orderContainsPartRepository.save(new OrderContainsPart(id,order.getOrderid(),quantity));
|
---|
80 | try {
|
---|
81 | response.sendRedirect("/products");
|
---|
82 | } catch (IOException e) {
|
---|
83 | throw new RuntimeException(e);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|