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

main
Last change on this file was 2a552fe, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 16 months ago

Added transactional methods to secure the database integrity and refactoring

  • Property mode set to 100644
File size: 5.4 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 RepairService repairService;
28 private final ReviewRepository reviewRepository;
29 private final OrderRepository orderRepository;
30
31 public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, RepairService repairService, ReviewRepository reviewRepository,
32 OrderRepository orderRepository) {
33 this.ordersForUserRepository = ordersForUserRepository;
34 this.repairsForUserRepository = repairsForUserRepository;
35 this.reviewsForUserRepository = reviewsForUserRepository;
36 this.userService = userService;
37 this.carService = carService;
38 this.repairService = repairService;
39 this.reviewRepository = reviewRepository;
40 this.orderRepository = orderRepository;
41 }
42 @GetMapping("orders")
43 public String getOrdersForUser(Model model, HttpServletRequest request){
44 Integer id = userService.findByUsername(request.getRemoteUser()).getId();
45 model.addAttribute("userOrders",ordersForUserRepository.findAllByUserid(id));
46 model.addAttribute("bodyContent","ordersForUser");
47 return "master-template";
48 }
49 @GetMapping("repairs")
50 public String getRepairsForUser(Model model, HttpServletRequest request){
51 Integer id = userService.findByUsername(request.getRemoteUser()).getId();
52 model.addAttribute("userRepairs",repairsForUserRepository.findAllByUserid(id));
53 List<Boolean> hasReview = new ArrayList<>(100);
54 for (int i = 0; i < orderRepository.findAll().size(); i++) {
55 hasReview.add(false);
56 }
57 for(RepairsForUser rfu: repairsForUserRepository.findAll()){
58 if(reviewRepository.findByRepair(repairService.findById(rfu.getRepairid()))==null){
59 hasReview.set(rfu.getRepairid(),false);
60 }
61 else {
62 hasReview.set(rfu.getRepairid(),true);
63 }
64 }
65 model.addAttribute("hasReview", hasReview);
66 model.addAttribute("bodyContent","repairsForUser");
67 return "master-template";
68 }
69 @GetMapping("reviews")
70 public String getReviewsForUser(Model model,HttpServletRequest request){
71 Integer id = userService.findByUsername(request.getRemoteUser()).getId();
72 model.addAttribute("userReviews",reviewsForUserRepository.findAllByUserid(id));
73 model.addAttribute("bodyContent","reviewsForUser");
74 return "master-template";
75 }
76 @GetMapping("/addCarSampleForUser")
77 public String addCarSampleForUser(Model model){
78 model.addAttribute("bodyContent","addCarSampleForUser");
79 model.addAttribute("cars",carService.findAll());
80 return "master-template";
81 }
82 @PostMapping("/addCarSampleForUser")
83 public void addCarSampleForUser(@RequestParam Integer vin, @RequestParam Integer year, @RequestParam Integer power,
84 @RequestParam Integer displacement, @RequestParam String fuel,
85 @RequestParam Integer km, @RequestParam Integer cartype,
86 HttpServletRequest request, HttpServletResponse response){
87 userService.addCarSampleForUser(vin,year,power,displacement,fuel,km,cartype,request.getRemoteUser());
88 try {
89 response.sendRedirect("/");
90 } catch (IOException e) {
91 throw new RuntimeException(e);
92 }
93 }
94 @GetMapping("/repairs/addReview/{id}")
95 public String addReviewFor(@PathVariable Integer id, Model model){
96 model.addAttribute("bodyContent","addReviewPage");
97 model.addAttribute("repair", repairService.findById(id));
98 return "master-template";
99 }
100 @PostMapping("/repairs/addReview/{id}")
101 public void addReview(@PathVariable Integer id, @RequestParam Integer rating, @RequestParam(required = false) String comment,
102 HttpServletResponse response, HttpServletRequest request){
103 Client client = (Client) userService.findByUsername(request.getRemoteUser());
104 Repair repair = repairService.findById(id);
105 reviewRepository.save(new Review(rating,
106 comment==null ? "" : comment, repair,client));
107 try {
108 response.sendRedirect("/repairs");
109 } catch (IOException e) {
110 throw new RuntimeException(e);
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.