source: src/main/java/tech/techharbor/Web/UserOrdersController.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: 2.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 tech.techharbor.Model.OrderTableContainsProductModel;
9import tech.techharbor.Model.OrderTableModel;
10import tech.techharbor.Model.ProductModel;
11import tech.techharbor.Model.UserTableModel;
12import tech.techharbor.Service.OrderTableContainsProductService;
13import tech.techharbor.Service.OrderTableService;
14import tech.techharbor.Service.ProductService;
15
16import java.util.ArrayList;
17import java.util.List;
18import java.util.Objects;
19
20@Controller
21public 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}
Note: See TracBrowser for help on using the repository browser.