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 org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
10 | import tech.techharbor.Model.ProductModel;
|
---|
11 | import tech.techharbor.Model.ReviewModel;
|
---|
12 | import tech.techharbor.Model.UserTableModel;
|
---|
13 | import tech.techharbor.Service.CategoryService;
|
---|
14 | import tech.techharbor.Service.ProductService;
|
---|
15 | import tech.techharbor.Service.ReviewService;
|
---|
16 | import tech.techharbor.Service.SubcategoryService;
|
---|
17 |
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | @Controller
|
---|
22 | public class ProductController {
|
---|
23 |
|
---|
24 | private final ProductService productService;
|
---|
25 | private final ReviewService reviewService;
|
---|
26 |
|
---|
27 | private final CategoryService categoryService;
|
---|
28 |
|
---|
29 | private final SubcategoryService subcategoryService;
|
---|
30 |
|
---|
31 |
|
---|
32 | public ProductController(ProductService productService, ReviewService reviewService, CategoryService categoryService, SubcategoryService subcategoryService) {
|
---|
33 | this.productService = productService;
|
---|
34 | this.reviewService = reviewService;
|
---|
35 | this.categoryService = categoryService;
|
---|
36 | this.subcategoryService = subcategoryService;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @GetMapping("/product/{id}")
|
---|
40 | public String showProduct(@PathVariable Integer id,
|
---|
41 | Model model,
|
---|
42 | HttpSession session) {
|
---|
43 | ProductModel product = productService.findById(id);
|
---|
44 | List<ReviewModel> reviews = this.reviewService.getReviewsByProductId(product.getProductId());
|
---|
45 | model.addAttribute("product", product);
|
---|
46 | model.addAttribute("reviews", reviews);
|
---|
47 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
48 | model.addAttribute("user", user);
|
---|
49 | Object deliveryManObject = session.getAttribute("deliveryMan");
|
---|
50 | model.addAttribute("deliveryMan", deliveryManObject);
|
---|
51 | return "productPage";
|
---|
52 | }
|
---|
53 |
|
---|
54 | @PostMapping("/add-review")
|
---|
55 | public String addReview(@RequestParam Integer productId,
|
---|
56 | @RequestParam Integer reviewRating,
|
---|
57 | @RequestParam String reviewDescription,
|
---|
58 | HttpSession session) {
|
---|
59 |
|
---|
60 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
61 |
|
---|
62 | reviewService.create(reviewRating, reviewDescription, user.getUserId(), productId);
|
---|
63 |
|
---|
64 | return "redirect:/product/" + productId;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @PostMapping("/filter")
|
---|
68 | public String filter(@RequestParam String name,
|
---|
69 | Model model,
|
---|
70 | HttpSession session) {
|
---|
71 | List<ProductModel> products = this.productService.listProducts();
|
---|
72 | List<ProductModel> filteredProducts = new ArrayList<>();
|
---|
73 |
|
---|
74 | for (ProductModel productModel : products) {
|
---|
75 | if (productModel.getProductName().toLowerCase().contains(name.toLowerCase())) {
|
---|
76 | filteredProducts.add(productModel);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | model.addAttribute("filteredProducts", filteredProducts);
|
---|
80 | model.addAttribute("categories", categoryService.listCategories());
|
---|
81 | model.addAttribute("subcategories", subcategoryService.listSubcategories());
|
---|
82 | model.addAttribute("products", productService.listProducts());
|
---|
83 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
84 | model.addAttribute("user", user);
|
---|
85 | Object deliveryManObject = session.getAttribute("deliveryMan");
|
---|
86 | model.addAttribute("deliveryMan", deliveryManObject);
|
---|
87 |
|
---|
88 | return "filterPage";
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|