[feffc2f] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
[7d43957] | 3 | import com.example.autopartz.model.Client;
|
---|
| 4 | import com.example.autopartz.model.Order;
|
---|
[feffc2f] | 5 | import com.example.autopartz.model.Part;
|
---|
[7d43957] | 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.*;
|
---|
[feffc2f] | 10 | import org.springframework.stereotype.Controller;
|
---|
| 11 | import org.springframework.ui.Model;
|
---|
| 12 | import org.springframework.web.bind.annotation.*;
|
---|
| 13 |
|
---|
[7d43957] | 14 | import javax.servlet.http.HttpServletRequest;
|
---|
[ae042f4] | 15 | import javax.servlet.http.HttpServletResponse;
|
---|
[7d43957] | 16 | import javax.servlet.http.HttpSession;
|
---|
[feffc2f] | 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;
|
---|
[7d43957] | 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) {
|
---|
[feffc2f] | 29 | this.partService = partService;
|
---|
| 30 | this.repairShopService = repairShopService;
|
---|
| 31 | this.priceService = priceService;
|
---|
[7d43957] | 32 | this.orderService = orderService;
|
---|
| 33 | this.userService = userService;
|
---|
| 34 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
[feffc2f] | 35 | }
|
---|
| 36 | @GetMapping("/{id}")
|
---|
[ae042f4] | 37 | public String getPartPage(@PathVariable Integer id, Model model){
|
---|
[feffc2f] | 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);
|
---|
[60de3eb] | 42 | model.addAttribute("bodyContent","partinfo");
|
---|
| 43 | return "master-template";
|
---|
[feffc2f] | 44 | }
|
---|
[7d43957] | 45 | @GetMapping("/delivery")
|
---|
| 46 | public String getDeliveryPage(Model model){
|
---|
[feffc2f] | 47 | model.addAttribute("repairShops",repairShopService.findAll());
|
---|
[60de3eb] | 48 | model.addAttribute("bodyContent","deliveryForPart");
|
---|
| 49 | return "master-template";
|
---|
[feffc2f] | 50 | }
|
---|
| 51 | @PostMapping("/repairshopdelivery")
|
---|
[676144b] | 52 | public void setRepairShopDelivery(@RequestParam String name, HttpServletResponse response, HttpSession session){
|
---|
[feffc2f] | 53 | // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
|
---|
[676144b] | 54 | session.removeAttribute("order");
|
---|
[feffc2f] | 55 | try {
|
---|
| 56 | response.sendRedirect("/");
|
---|
| 57 | } catch (IOException e) {
|
---|
| 58 | throw new RuntimeException(e);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | @PostMapping("/homedelivery")
|
---|
[676144b] | 62 | public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){
|
---|
[feffc2f] | 63 | // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
|
---|
[676144b] | 64 | session.removeAttribute("order");
|
---|
[feffc2f] | 65 | try {
|
---|
| 66 | response.sendRedirect("/");
|
---|
| 67 | } catch (IOException e) {
|
---|
| 68 | throw new RuntimeException(e);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[7d43957] | 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.getID_order(),quantity));
|
---|
| 80 | try {
|
---|
| 81 | response.sendRedirect("/products");
|
---|
| 82 | } catch (IOException e) {
|
---|
| 83 | throw new RuntimeException(e);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
[feffc2f] | 86 | }
|
---|