1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.Part;
|
---|
4 | import com.example.autopartz.service.PartService;
|
---|
5 | import com.example.autopartz.service.PriceService;
|
---|
6 | import com.example.autopartz.service.RepairShopService;
|
---|
7 | import jakarta.servlet.http.HttpServletResponse;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.*;
|
---|
11 |
|
---|
12 | import java.io.IOException;
|
---|
13 |
|
---|
14 | @Controller
|
---|
15 | @RequestMapping("/part")
|
---|
16 | public class PartController {
|
---|
17 | private final PartService partService;
|
---|
18 | private final RepairShopService repairShopService;
|
---|
19 | private final PriceService priceService;
|
---|
20 | public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService) {
|
---|
21 | this.partService = partService;
|
---|
22 | this.repairShopService = repairShopService;
|
---|
23 | this.priceService = priceService;
|
---|
24 | }
|
---|
25 | @GetMapping("/{id}")
|
---|
26 | public String getPartPage(@PathVariable Long id, Model model){
|
---|
27 | Part temp = partService.findById(id);
|
---|
28 | Integer amount = priceService.findPriceForPart(temp).stream().findFirst().orElseThrow(RuntimeException::new).getAmount();
|
---|
29 | model.addAttribute("part",temp);
|
---|
30 | model.addAttribute("amount",amount);
|
---|
31 | return "partinfo";
|
---|
32 | }
|
---|
33 | @GetMapping("/delivery/{id}")
|
---|
34 | public String getDeliveryPage(@PathVariable Long id, Model model){
|
---|
35 | model.addAttribute("repairShops",repairShopService.findAll());
|
---|
36 | model.addAttribute("partId",id);
|
---|
37 | return "deliveryForPart";
|
---|
38 | }
|
---|
39 | @PostMapping("/repairshopdelivery")
|
---|
40 | public void setRepairShopDelivery(@RequestParam String name, HttpServletResponse response){
|
---|
41 | // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
|
---|
42 | try {
|
---|
43 | response.sendRedirect("/");
|
---|
44 | } catch (IOException e) {
|
---|
45 | throw new RuntimeException(e);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | @PostMapping("/homedelivery")
|
---|
49 | public void setHomeDelivery(@RequestParam String address, HttpServletResponse response){
|
---|
50 | // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
|
---|
51 | try {
|
---|
52 | response.sendRedirect("/");
|
---|
53 | } catch (IOException e) {
|
---|
54 | throw new RuntimeException(e);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|