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.OrderTableContainsProductModel;
|
---|
9 | import tech.techharbor.Model.OrderTableModel;
|
---|
10 | import tech.techharbor.Model.ProductModel;
|
---|
11 | import tech.techharbor.Model.UserTableModel;
|
---|
12 | import tech.techharbor.Service.OrderTableContainsProductService;
|
---|
13 | import tech.techharbor.Service.OrderTableService;
|
---|
14 | import tech.techharbor.Service.ProductService;
|
---|
15 |
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Objects;
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | public class UserOrdersController {
|
---|
22 |
|
---|
23 | private final OrderTableService orderTableService;
|
---|
24 |
|
---|
25 | private final ProductService productService;
|
---|
26 |
|
---|
27 | private final OrderTableContainsProductService orderTableContainsProductService;
|
---|
28 |
|
---|
29 | public UserOrdersController(OrderTableService orderTableService, ProductService productService, OrderTableContainsProductService orderTableContainsProductService) {
|
---|
30 | this.orderTableService = orderTableService;
|
---|
31 | this.productService = productService;
|
---|
32 | this.orderTableContainsProductService = orderTableContainsProductService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping("/orders/{id}")
|
---|
36 | public String showUserOrders(@PathVariable Integer id, Model model, HttpSession session) {
|
---|
37 |
|
---|
38 | UserTableModel user = (UserTableModel) session.getAttribute("user");
|
---|
39 | model.addAttribute("user", user);
|
---|
40 | List<OrderTableModel> orders = orderTableService.findByCustomerId(user.getUserId());
|
---|
41 | List<OrderTableContainsProductModel> ordersWithProducts = orderTableContainsProductService.listAll();
|
---|
42 |
|
---|
43 | List<OrderTableContainsProductModel> finalOrderList = new ArrayList<>();
|
---|
44 | List<OrderTableModel> allOrders = new ArrayList<>();
|
---|
45 | List<ProductModel> products = new ArrayList<>();
|
---|
46 |
|
---|
47 | for (OrderTableModel order : orders) {
|
---|
48 | for (OrderTableContainsProductModel orderProducts : ordersWithProducts) {
|
---|
49 | if (Objects.equals(order.getOrderId(), orderProducts.getOrderTableContainsProductClass().getOrderId())) {
|
---|
50 | allOrders.add(order);
|
---|
51 | finalOrderList.add(orderProducts);
|
---|
52 | products.add(productService.findById(orderProducts.getOrderTableContainsProductClass().getProductId()));
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | if (!finalOrderList.isEmpty() || !products.isEmpty()) {
|
---|
59 | model.addAttribute("orderList", finalOrderList);
|
---|
60 | model.addAttribute("products", products);
|
---|
61 | model.addAttribute("orders", allOrders);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | return "myOrders";
|
---|
66 | }
|
---|
67 | }
|
---|