- Timestamp:
- 01/06/23 15:26:00 (23 months ago)
- Branches:
- main
- Children:
- 9dcbf44
- Parents:
- f484b14
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/autopartz/controller/UserController.java
rf484b14 rd4b888e 1 1 package com.example.autopartz.controller; 2 2 3 import com.example.autopartz.model.CarSample; 4 import com.example.autopartz.model.Client; 5 import com.example.autopartz.model.ServiceBook; 3 import com.example.autopartz.model.*; 4 import com.example.autopartz.model.views.RepairsForUser; 6 5 import com.example.autopartz.repository.*; 7 6 import com.example.autopartz.service.CarService; 7 import com.example.autopartz.service.RepairService; 8 8 import com.example.autopartz.service.UserService; 9 9 import org.springframework.stereotype.Controller; 10 10 import org.springframework.ui.Model; 11 import org.springframework.web.bind.annotation.GetMapping; 12 import org.springframework.web.bind.annotation.PostMapping; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RequestParam; 11 import org.springframework.web.bind.annotation.*; 15 12 16 13 import javax.servlet.http.HttpServletRequest; 17 14 import javax.servlet.http.HttpServletResponse; 18 15 import java.io.IOException; 16 import java.util.ArrayList; 17 import java.util.List; 19 18 20 19 @Controller … … 28 27 private final CarSampleRepository carSampleRepository; 29 28 private final ServiceBookRepository serviceBookRepository; 30 public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, 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) { 31 37 this.ordersForUserRepository = ordersForUserRepository; 32 38 this.repairsForUserRepository = repairsForUserRepository; … … 36 42 this.carSampleRepository = carSampleRepository; 37 43 this.serviceBookRepository = serviceBookRepository; 44 this.repairService = repairService; 45 this.reviewRepository = reviewRepository; 46 this.repairRepository = repairRepository; 47 this.orderRepository = orderRepository; 38 48 } 39 49 @GetMapping("orders") … … 48 58 Integer id = userService.findByUsername(request.getRemoteUser()).getId(); 49 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); 50 73 model.addAttribute("bodyContent","repairsForUser"); 51 74 return "master-template"; … … 78 101 } 79 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 } 80 122 }
Note:
See TracChangeset
for help on using the changeset viewer.