1 | package com.example.autopartz.controller;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.*;
|
---|
4 | import com.example.autopartz.model.views.RepairsForUser;
|
---|
5 | import com.example.autopartz.repository.*;
|
---|
6 | import com.example.autopartz.service.CarService;
|
---|
7 | import com.example.autopartz.service.RepairService;
|
---|
8 | import com.example.autopartz.service.UserService;
|
---|
9 | import org.springframework.stereotype.Controller;
|
---|
10 | import org.springframework.ui.Model;
|
---|
11 | import org.springframework.web.bind.annotation.*;
|
---|
12 |
|
---|
13 | import javax.servlet.http.HttpServletRequest;
|
---|
14 | import javax.servlet.http.HttpServletResponse;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | @RequestMapping("/")
|
---|
21 | public class UserController {
|
---|
22 | private final OrdersForUserRepository ordersForUserRepository;
|
---|
23 | private final RepairsForUserRepository repairsForUserRepository;
|
---|
24 | private final ReviewsForUserRepository reviewsForUserRepository;
|
---|
25 | private final UserService userService;
|
---|
26 | private final CarService carService;
|
---|
27 | private final CarSampleRepository carSampleRepository;
|
---|
28 | private final ServiceBookRepository serviceBookRepository;
|
---|
29 | private final RepairService repairService;
|
---|
30 | private final ReviewRepository reviewRepository;
|
---|
31 | private final RepairRepository repairRepository;
|
---|
32 | private final OrderRepository orderRepository;
|
---|
33 |
|
---|
34 | public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, RepairService repairService, ReviewRepository reviewRepository,
|
---|
35 | RepairRepository repairRepository,
|
---|
36 | OrderRepository orderRepository) {
|
---|
37 | this.ordersForUserRepository = ordersForUserRepository;
|
---|
38 | this.repairsForUserRepository = repairsForUserRepository;
|
---|
39 | this.reviewsForUserRepository = reviewsForUserRepository;
|
---|
40 | this.userService = userService;
|
---|
41 | this.carService = carService;
|
---|
42 | this.carSampleRepository = carSampleRepository;
|
---|
43 | this.serviceBookRepository = serviceBookRepository;
|
---|
44 | this.repairService = repairService;
|
---|
45 | this.reviewRepository = reviewRepository;
|
---|
46 | this.repairRepository = repairRepository;
|
---|
47 | this.orderRepository = orderRepository;
|
---|
48 | }
|
---|
49 | @GetMapping("orders")
|
---|
50 | public String getOrdersForUser(Model model, HttpServletRequest request){
|
---|
51 | Integer id = userService.findByUsername(request.getRemoteUser()).getId();
|
---|
52 | model.addAttribute("userOrders",ordersForUserRepository.findAllByUserid(id));
|
---|
53 | model.addAttribute("bodyContent","ordersForUser");
|
---|
54 | return "master-template";
|
---|
55 | }
|
---|
56 | @GetMapping("repairs")
|
---|
57 | public String getRepairsForUser(Model model, HttpServletRequest request){
|
---|
58 | Integer id = userService.findByUsername(request.getRemoteUser()).getId();
|
---|
59 | model.addAttribute("userRepairs",repairsForUserRepository.findAllByUserid(id));
|
---|
60 | List<Boolean> hasReview = new ArrayList<>(100);
|
---|
61 | for (int i = 0; i < orderRepository.findAll().size(); i++) {
|
---|
62 | hasReview.add(false);
|
---|
63 | }
|
---|
64 | for(RepairsForUser rfu: repairsForUserRepository.findAll()){
|
---|
65 | if(reviewRepository.findByRepair(repairService.findById(rfu.getRepairid()))==null){
|
---|
66 | hasReview.set(rfu.getRepairid(),false);
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | hasReview.set(rfu.getRepairid(),true);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | model.addAttribute("hasReview", hasReview);
|
---|
73 | model.addAttribute("bodyContent","repairsForUser");
|
---|
74 | return "master-template";
|
---|
75 | }
|
---|
76 | @GetMapping("reviews")
|
---|
77 | public String getReviewsForUser(Model model,HttpServletRequest request){
|
---|
78 | Integer id = userService.findByUsername(request.getRemoteUser()).getId();
|
---|
79 | model.addAttribute("userReviews",reviewsForUserRepository.findAllByUserid(id));
|
---|
80 | model.addAttribute("bodyContent","reviewsForUser");
|
---|
81 | return "master-template";
|
---|
82 | }
|
---|
83 | @GetMapping("/addCarSampleForUser")
|
---|
84 | public String addCarSampleForUser(Model model){
|
---|
85 | model.addAttribute("bodyContent","addCarSampleForUser");
|
---|
86 | model.addAttribute("cars",carService.findAll());
|
---|
87 | return "master-template";
|
---|
88 | }
|
---|
89 | @PostMapping("/addCarSampleForUser")
|
---|
90 | public void addCarSampleForUser(@RequestParam Integer vin, @RequestParam Integer year, @RequestParam Integer power,
|
---|
91 | @RequestParam Integer displacement, @RequestParam String fuel,
|
---|
92 | @RequestParam Integer km, @RequestParam Integer cartype,
|
---|
93 | HttpServletRequest request, HttpServletResponse response){
|
---|
94 | CarSample cs = new CarSample(vin,year,power,displacement,fuel,km, (Client) userService.findByUsername(request.getRemoteUser()),carService.findById(cartype));
|
---|
95 | carSampleRepository.save(cs);
|
---|
96 | serviceBookRepository.save(new ServiceBook(cs));
|
---|
97 | try {
|
---|
98 | response.sendRedirect("/");
|
---|
99 | } catch (IOException e) {
|
---|
100 | throw new RuntimeException(e);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | @GetMapping("/repairs/addReview/{id}")
|
---|
104 | public String addReviewFor(@PathVariable Integer id, Model model){
|
---|
105 | model.addAttribute("bodyContent","addReviewPage");
|
---|
106 | model.addAttribute("repair", repairService.findById(id));
|
---|
107 | return "master-template";
|
---|
108 | }
|
---|
109 | @PostMapping("/repairs/addReview/{id}")
|
---|
110 | public void addReview(@PathVariable Integer id, @RequestParam Integer rating, @RequestParam(required = false) String comment,
|
---|
111 | Model model, HttpServletResponse response, HttpServletRequest request){
|
---|
112 | Client client = (Client) userService.findByUsername(request.getRemoteUser());
|
---|
113 | Repair repair = repairService.findById(id);
|
---|
114 | reviewRepository.save(new Review(rating,
|
---|
115 | comment==null ? "" : comment, repair,client));
|
---|
116 | try {
|
---|
117 | response.sendRedirect("/repairs");
|
---|
118 | } catch (IOException e) {
|
---|
119 | throw new RuntimeException(e);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|