[f4b4afa] | 1 | package tech.techharbor.Web;
|
---|
| 2 |
|
---|
| 3 | import jakarta.servlet.http.HttpSession;
|
---|
| 4 | import org.springframework.stereotype.Controller;
|
---|
| 5 | import org.springframework.ui.Model;
|
---|
| 6 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 7 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
| 8 | import tech.techharbor.Model.ProductModel;
|
---|
| 9 | import tech.techharbor.Model.ReviewModel;
|
---|
| 10 | import tech.techharbor.Model.UserTableModel;
|
---|
| 11 | import tech.techharbor.Service.ProductService;
|
---|
| 12 | import tech.techharbor.Service.ReviewService;
|
---|
| 13 |
|
---|
| 14 | import java.util.ArrayList;
|
---|
| 15 | import java.util.List;
|
---|
| 16 |
|
---|
| 17 | @Controller
|
---|
| 18 | public class UserReviewController {
|
---|
| 19 | private final ReviewService reviewService;
|
---|
| 20 | private final ProductService productService;
|
---|
| 21 |
|
---|
| 22 | public UserReviewController(ReviewService reviewService, ProductService productService) {
|
---|
| 23 | this.reviewService = reviewService;
|
---|
| 24 | this.productService = productService;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | @GetMapping("/reviews/{id}")
|
---|
| 28 | public String showUserReviews(@PathVariable Integer id, Model model, HttpSession session) {
|
---|
| 29 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
| 30 | model.addAttribute("user", user);
|
---|
| 31 | List<ReviewModel> allReviews = reviewService.listReviews();
|
---|
| 32 | List<ReviewModel> reviews = new ArrayList<>();
|
---|
| 33 | List<ProductModel> products = new ArrayList<>();
|
---|
| 34 | for (ReviewModel review : allReviews) {
|
---|
| 35 |
|
---|
| 36 | if (review.getCustomerId().equals(user.getUserId())) {
|
---|
| 37 | reviews.add(review);
|
---|
| 38 | ProductModel product = productService.findById(review.getProductId());
|
---|
| 39 | products.add(product);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | model.addAttribute("reviews", reviews);
|
---|
| 43 | model.addAttribute("products", products);
|
---|
| 44 | model.addAttribute("index", 0);
|
---|
| 45 | return "myReviews";
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | }
|
---|