source: src/main/java/com/example/autopartz/controller/UserController.java@ 5e7345e

main
Last change on this file since 5e7345e was d4b888e, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 23 months ago

Added full functionality for adding review

  • Property mode set to 100644
File size: 6.0 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.model.views.RepairsForUser;
5import com.example.autopartz.repository.*;
6import com.example.autopartz.service.CarService;
7import com.example.autopartz.service.RepairService;
8import com.example.autopartz.service.UserService;
9import org.springframework.stereotype.Controller;
10import org.springframework.ui.Model;
11import org.springframework.web.bind.annotation.*;
12
13import javax.servlet.http.HttpServletRequest;
14import javax.servlet.http.HttpServletResponse;
15import java.io.IOException;
16import java.util.ArrayList;
17import java.util.List;
18
19@Controller
20@RequestMapping("/")
21public 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}
Note: See TracBrowser for help on using the repository browser.