1 | package com.example.fooddeliverysystem.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.fooddeliverysystem.exceptions.FoodItemNotFoundException;
|
---|
5 | import com.example.fooddeliverysystem.exceptions.OrderNotFoundException;
|
---|
6 | import com.example.fooddeliverysystem.model.Order;
|
---|
7 | import com.example.fooddeliverysystem.service.OrderPaymentService;
|
---|
8 | import com.example.fooddeliverysystem.service.OrderService;
|
---|
9 | import jakarta.servlet.http.HttpServletRequest;
|
---|
10 | import org.springframework.stereotype.Component;
|
---|
11 | import org.springframework.stereotype.Controller;
|
---|
12 | import org.springframework.ui.Model;
|
---|
13 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
14 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
15 |
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | public class DeliveryController {
|
---|
21 |
|
---|
22 | private final OrderService orderService;
|
---|
23 |
|
---|
24 | private final OrderPaymentService orderPaymentService;
|
---|
25 | public DeliveryController(OrderService orderService, OrderPaymentService orderPaymentService) {
|
---|
26 | this.orderService = orderService;
|
---|
27 | this.orderPaymentService = orderPaymentService;
|
---|
28 | }
|
---|
29 |
|
---|
30 | @GetMapping("/deliveryOrders")
|
---|
31 | public String showOrdersForDelivery(Model model){
|
---|
32 | List<Integer> costs = new ArrayList<>();
|
---|
33 | List<Order> orders = this.orderService.findAllOrdersReadyToBeDelivered();
|
---|
34 | for(Order order: orders){
|
---|
35 | try {
|
---|
36 | costs.add(this.orderService.calculateCostOfOrder(order.getOrderId()));
|
---|
37 | } catch (FoodItemNotFoundException e) {
|
---|
38 | System.out.println(e.getMessage());
|
---|
39 | }
|
---|
40 | }
|
---|
41 | model.addAttribute("orders", orders);
|
---|
42 | model.addAttribute("costs", costs);
|
---|
43 | return "delivery";
|
---|
44 | }
|
---|
45 |
|
---|
46 | @GetMapping("/takeOrder/{id}")
|
---|
47 | public String deliverTakesOrder(@PathVariable Long id, HttpServletRequest httpServletRequest){
|
---|
48 | this.orderService.changeOrderStatus(id, "prevzemena");
|
---|
49 | this.orderService.updateOrderDeliver(httpServletRequest.getRemoteUser(), id);
|
---|
50 | return "redirect:/showOrderDeliverer";
|
---|
51 | }
|
---|
52 | @GetMapping("/showOrderDeliverer")
|
---|
53 | public String showDelivererAllOrders(Model model, HttpServletRequest httpServletRequest){
|
---|
54 | List<Integer> costs = new ArrayList<>();
|
---|
55 | List<Order> orders = this.orderService.findAllOrdersForDeliver(httpServletRequest.getRemoteUser());
|
---|
56 | System.out.println(orders);
|
---|
57 | for(Order order: orders){
|
---|
58 | try {
|
---|
59 | costs.add(this.orderService.calculateCostOfOrder(order.getOrderId()));
|
---|
60 | } catch (FoodItemNotFoundException e) {
|
---|
61 | throw new RuntimeException(e);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | model.addAttribute("orders", orders);
|
---|
65 | model.addAttribute("costs", costs);
|
---|
66 | return "ordersfordeliver";
|
---|
67 | }
|
---|
68 | @GetMapping("/orderPayment/{id}")
|
---|
69 | public String markOrderAsPayed(@PathVariable Long id){
|
---|
70 | try {
|
---|
71 | this.orderPaymentService.createOrderPayment(id);
|
---|
72 | } catch (OrderNotFoundException e) {
|
---|
73 | System.out.println(e.getMessage());
|
---|
74 | } catch (FoodItemNotFoundException e) {
|
---|
75 | System.out.println(e.getMessage());
|
---|
76 | }
|
---|
77 | return "redirect:/showOrderDeliverer";
|
---|
78 | }
|
---|
79 | }
|
---|