1 | package com.example.fooddeliverysystem.web;
|
---|
2 |
|
---|
3 | import com.example.fooddeliverysystem.exceptions.FoodItemNotFoundException;
|
---|
4 | import com.example.fooddeliverysystem.exceptions.SalePlaceNotFoundException;
|
---|
5 | import com.example.fooddeliverysystem.model.*;
|
---|
6 | import com.example.fooddeliverysystem.model.objects.FoodItemsWithQuantity;
|
---|
7 | import com.example.fooddeliverysystem.repository.PriceRepository;
|
---|
8 | import com.example.fooddeliverysystem.repository.storedprocedure.OrderCostCalcualte;
|
---|
9 | import com.example.fooddeliverysystem.service.*;
|
---|
10 | import jakarta.servlet.http.HttpServletRequest;
|
---|
11 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
12 | import org.springframework.stereotype.Controller;
|
---|
13 | import org.springframework.ui.Model;
|
---|
14 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
15 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
16 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
17 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
18 |
|
---|
19 | import java.util.*;
|
---|
20 | import java.util.stream.Collectors;
|
---|
21 |
|
---|
22 | @Controller
|
---|
23 | public class SalePlaceController {
|
---|
24 |
|
---|
25 | private final OrderCostCalcualte orderCostCalcualte;
|
---|
26 |
|
---|
27 | private final SalePlaceService salePlaceService;
|
---|
28 | private final PriceService priceService;
|
---|
29 | private final OrderService orderService;
|
---|
30 | private final HasFoodService hasFoodService;
|
---|
31 | private final FoodItemService foodItemService;
|
---|
32 |
|
---|
33 |
|
---|
34 | public SalePlaceController(SalePlaceService salePlaceService, PriceService priceService, OrderService orderService, HasFoodService hasFoodService,
|
---|
35 | FoodItemService foodItemService, OrderCostCalcualte orderCostCalcualte) {
|
---|
36 | this.salePlaceService = salePlaceService;
|
---|
37 | this.priceService = priceService;
|
---|
38 | this.orderService = orderService;
|
---|
39 | this.hasFoodService = hasFoodService;
|
---|
40 | this.foodItemService = foodItemService;
|
---|
41 | this.orderCostCalcualte = orderCostCalcualte;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @GetMapping("/salePlaces")
|
---|
45 | public String showSalePlaces(Model model) {
|
---|
46 | List<SalePlace> salePlaceList = this.salePlaceService.findAll();
|
---|
47 | model.addAttribute("salePlaces", salePlaceList);
|
---|
48 |
|
---|
49 | return "saleplaces";
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping("/salePlace/{id}")
|
---|
53 | public String showSalePlaceFooItems(@PathVariable Long id, Model model) {
|
---|
54 | try {
|
---|
55 | model.addAttribute("foodItems", this.salePlaceService.findSalePlaceServiceById(id).getFoodItemList());
|
---|
56 | model.addAttribute("salePlaceId", id);
|
---|
57 | List<Price> prices = new ArrayList<>();
|
---|
58 | this.salePlaceService.findSalePlaceServiceById(id)
|
---|
59 | .getFoodItemList()
|
---|
60 | .forEach(foodItem -> {
|
---|
61 | try {
|
---|
62 | prices.add(this.priceService.findCurrentPriceForFoodItem(foodItem));
|
---|
63 | } catch (FoodItemNotFoundException e) {
|
---|
64 | model.addAttribute("foodItemError", e.getMessage());
|
---|
65 | }
|
---|
66 | });
|
---|
67 | System.out.println(prices);
|
---|
68 |
|
---|
69 | model.addAttribute("prices", prices);
|
---|
70 | } catch (SalePlaceNotFoundException salePlaceNotFoundException) {
|
---|
71 | model.addAttribute("error", salePlaceNotFoundException.getMessage());
|
---|
72 | }
|
---|
73 | return "saleplacefooditems";
|
---|
74 | }
|
---|
75 |
|
---|
76 | @PostMapping("/salePlace/{id}")
|
---|
77 | public String createOrderInSalePlace(@PathVariable Long id,
|
---|
78 | @RequestParam List<Long> foodIds,
|
---|
79 | @RequestParam List<Integer> foodPrice,
|
---|
80 | @RequestParam List<Integer> quantity,
|
---|
81 | @RequestParam String typeOfPayment,
|
---|
82 | HttpServletRequest httpServletRequest) {
|
---|
83 | try {
|
---|
84 | this.orderService.placeOrder(typeOfPayment, id, foodIds, foodPrice, quantity, httpServletRequest.getRemoteUser());
|
---|
85 |
|
---|
86 | } catch (SalePlaceNotFoundException e) {
|
---|
87 | throw new RuntimeException(e);
|
---|
88 | }
|
---|
89 | return "redirect:/checkOrderStatus";
|
---|
90 | }
|
---|
91 |
|
---|
92 | @GetMapping("/salePlace/Orders")
|
---|
93 | public String showSalePlaceOrders(Model model, HttpServletRequest httpServletRequest) {
|
---|
94 |
|
---|
95 | Map<Long, List<FoodItem>> map = new HashMap<>();
|
---|
96 | String username = httpServletRequest.getRemoteUser();
|
---|
97 | try {
|
---|
98 | List<Order> orders = this.salePlaceService.findAllCreatedOrders(username).stream().sorted(Comparator.comparingLong(Order::getOrderId)).collect(Collectors.toList());
|
---|
99 | List<OrderHasFood> orderHasFoodList = new ArrayList<>();
|
---|
100 | model.addAttribute("orders", orders);
|
---|
101 | List<List<OrderHasFood>> outer = new ArrayList<>();
|
---|
102 | for (Order order : orders) {
|
---|
103 | List<OrderHasFood> inner = this.hasFoodService.findAllFoodsInOrder(order.getOrderId());
|
---|
104 | outer.add(inner);
|
---|
105 | List<FoodItem> items = new ArrayList<>();
|
---|
106 | for (OrderHasFood orderHasFood : inner) {
|
---|
107 | FoodItem foodItems = this.salePlaceService.findSalePlaceServiceById(this.salePlaceService.findSalePlaceForUser(username).getSalePalceId())
|
---|
108 | .getFoodItemList()
|
---|
109 | .stream().filter(foodItem -> foodItem.getFoodItemId().equals(orderHasFood.getOrderHasFoodKey().getFoodItemId()))
|
---|
110 | .findFirst().get();
|
---|
111 | items.add(foodItems);
|
---|
112 |
|
---|
113 | }
|
---|
114 | map.put(order.getOrderId(), items);
|
---|
115 | model.addAttribute("quantity", outer);
|
---|
116 | }
|
---|
117 |
|
---|
118 | model.addAttribute("orderHasFoods", map);
|
---|
119 |
|
---|
120 | } catch (SalePlaceNotFoundException e) {
|
---|
121 | model.addAttribute("error", "sale place not found");
|
---|
122 | }
|
---|
123 | return "saleplaceorders";
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | @GetMapping("/changeOrderStatus/{id}")
|
---|
128 | public String changeOrderStauts(@PathVariable Long id) {
|
---|
129 | this.orderService.changeOrderStatus(id, "spremna");
|
---|
130 | return "redirect:/salePlace/Orders";
|
---|
131 | }
|
---|
132 |
|
---|
133 | @GetMapping("/checkOrderStatus")
|
---|
134 | public String showOrderStatusToUser(HttpServletRequest httpServletRequest, Model model) {
|
---|
135 | String username = httpServletRequest.getRemoteUser();
|
---|
136 | List<Order> orders = this.orderService.findAllOrdersForCustomer(username);
|
---|
137 | model.addAttribute("orders", orders.stream().sorted((a,b) -> Long.compare(b.getOrderId(), a.getOrderId())).collect(Collectors.toList()));
|
---|
138 | Map<Long, List<FoodItemsWithQuantity>> foodNamesInOrder = new HashMap<>();
|
---|
139 | for(Order order: orders){
|
---|
140 | foodNamesInOrder.put(order.getOrderId(), this.hasFoodService.findAllFoodnamesInOrder(order.getOrderId()));
|
---|
141 | }
|
---|
142 |
|
---|
143 | model.addAttribute("foodNames", foodNamesInOrder);
|
---|
144 | return "showOrderStatusCustomer";
|
---|
145 | }
|
---|
146 |
|
---|
147 | @GetMapping("/home")
|
---|
148 | public String showHomePage() {
|
---|
149 |
|
---|
150 | return "home";
|
---|
151 | }
|
---|
152 | @GetMapping("/")
|
---|
153 | public String showHome() {
|
---|
154 | return "home";
|
---|
155 | }
|
---|
156 | }
|
---|