source: src/main/java/tech/techharbor/Web/ProductController.java

main
Last change on this file was f4b4afa, checked in by Nikola Todoroski <nikola.todoroski@…>, 6 months ago

Pushed whole project, original project location on github:https://github.com/hehxd/Tech-Harbor

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package tech.techharbor.Web;
2
3import jakarta.servlet.http.HttpSession;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.Model;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.PathVariable;
8import org.springframework.web.bind.annotation.PostMapping;
9import org.springframework.web.bind.annotation.RequestParam;
10import tech.techharbor.Model.ProductModel;
11import tech.techharbor.Model.ReviewModel;
12import tech.techharbor.Model.UserTableModel;
13import tech.techharbor.Service.CategoryService;
14import tech.techharbor.Service.ProductService;
15import tech.techharbor.Service.ReviewService;
16import tech.techharbor.Service.SubcategoryService;
17
18import java.util.ArrayList;
19import java.util.List;
20
21@Controller
22public 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}
Note: See TracBrowser for help on using the repository browser.