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.RequestParam;
|
---|
9 | import tech.techharbor.Model.OrderTableModel;
|
---|
10 | import tech.techharbor.Model.ProductModel;
|
---|
11 | import tech.techharbor.Model.UserTableModel;
|
---|
12 | import tech.techharbor.Service.DeliveryService;
|
---|
13 | import tech.techharbor.Service.OrderTableContainsProductService;
|
---|
14 | import tech.techharbor.Service.OrderTableService;
|
---|
15 | import tech.techharbor.Service.ProductService;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Date;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 |
|
---|
22 | @Controller
|
---|
23 | public class ShoppingCartController {
|
---|
24 |
|
---|
25 | private final ProductService productService;
|
---|
26 | private final OrderTableService orderTableService;
|
---|
27 | private final OrderTableContainsProductService orderTableContainsProductService;
|
---|
28 |
|
---|
29 | private final DeliveryService deliveryService;
|
---|
30 |
|
---|
31 | public ShoppingCartController(ProductService productService, OrderTableService orderTableService, OrderTableContainsProductService orderTableContainsProductService, DeliveryService deliveryService) {
|
---|
32 | this.productService = productService;
|
---|
33 | this.orderTableService = orderTableService;
|
---|
34 | this.orderTableContainsProductService = orderTableContainsProductService;
|
---|
35 | this.deliveryService = deliveryService;
|
---|
36 | }
|
---|
37 |
|
---|
38 | List<ProductModel> shoppingCartList = new ArrayList<>();
|
---|
39 |
|
---|
40 | @GetMapping("/shoppingCart")
|
---|
41 | public String getShoppingCart(HttpSession session, Model model) {
|
---|
42 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
43 | Integer totalPrice = 0;
|
---|
44 |
|
---|
45 | for (ProductModel product : shoppingCartList) {
|
---|
46 | totalPrice += product.getProductPrice();
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | model.addAttribute("user", user);
|
---|
51 | model.addAttribute("grandTotal", totalPrice);
|
---|
52 | model.addAttribute("cartItems", shoppingCartList);
|
---|
53 |
|
---|
54 | return "shoppingCart";
|
---|
55 | }
|
---|
56 |
|
---|
57 | @GetMapping("/removeFromShoppingCart/{id}")
|
---|
58 | public String removeFromShoppingCart(@PathVariable Integer id,
|
---|
59 | HttpSession session,
|
---|
60 | Model model) {
|
---|
61 | ProductModel productModel = shoppingCartList.stream().filter(product -> product.getProductId().equals(id)).findFirst().orElse(null);
|
---|
62 | shoppingCartList.remove(productModel);
|
---|
63 |
|
---|
64 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
65 | Integer totalPrice = 0;
|
---|
66 |
|
---|
67 | for (ProductModel product : shoppingCartList) {
|
---|
68 | totalPrice += product.getProductPrice();
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | model.addAttribute("user", user);
|
---|
73 | model.addAttribute("grandTotal", totalPrice);
|
---|
74 | model.addAttribute("cartItems", shoppingCartList);
|
---|
75 |
|
---|
76 | return "shoppingCart";
|
---|
77 | }
|
---|
78 |
|
---|
79 | @GetMapping("/shoppingCart/{id}")
|
---|
80 | public String getProduct(@PathVariable Integer id) {
|
---|
81 | ProductModel product = productService.findById(id);
|
---|
82 | shoppingCartList.add(product);
|
---|
83 |
|
---|
84 | return "redirect:/";
|
---|
85 | }
|
---|
86 |
|
---|
87 | @GetMapping("/checkout")
|
---|
88 | public String checkoutPage(Model model, HttpSession session) {
|
---|
89 |
|
---|
90 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
91 |
|
---|
92 | Integer grandTotal = 0;
|
---|
93 |
|
---|
94 | for (ProductModel product : shoppingCartList) {
|
---|
95 | grandTotal += product.getProductPrice();
|
---|
96 | }
|
---|
97 |
|
---|
98 | model.addAttribute("user", user);
|
---|
99 | model.addAttribute("grandTotal", grandTotal);
|
---|
100 | model.addAttribute("cartItems", shoppingCartList);
|
---|
101 | return "Checkout";
|
---|
102 | }
|
---|
103 |
|
---|
104 | @GetMapping("/finalizeOrder")
|
---|
105 | public String getOrders(HttpSession session,
|
---|
106 | @RequestParam String address) {
|
---|
107 |
|
---|
108 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
109 |
|
---|
110 | java.util.Date utilDate = new Date();
|
---|
111 | OrderTableModel order = orderTableService.create("Created", new java.sql.Date(utilDate.getTime()), user.getUserId());
|
---|
112 | shoppingCartList.forEach(product -> orderTableContainsProductService.create(order.getOrderId(), product.getProductId(), 1));
|
---|
113 | deliveryService.create("Pending", address, 37, order.getOrderId());
|
---|
114 | shoppingCartList = new ArrayList<>();
|
---|
115 | return "redirect:/orders/" + user.getUserId();
|
---|
116 |
|
---|
117 | }
|
---|
118 | }
|
---|