source: src/main/java/com/example/fooddeliverysystem/web/DeliveryController.java@ 8d11f8c

Last change on this file since 8d11f8c was 8d11f8c, checked in by jovanmanchev <jovanmanchev3003@…>, 18 months ago

code added, trial 2

  • Property mode set to 100644
File size: 3.1 KB
Line 
1package com.example.fooddeliverysystem.web;
2
3
4import com.example.fooddeliverysystem.exceptions.FoodItemNotFoundException;
5import com.example.fooddeliverysystem.exceptions.OrderNotFoundException;
6import com.example.fooddeliverysystem.model.Order;
7import com.example.fooddeliverysystem.service.OrderPaymentService;
8import com.example.fooddeliverysystem.service.OrderService;
9import jakarta.servlet.http.HttpServletRequest;
10import org.springframework.stereotype.Component;
11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.GetMapping;
14import org.springframework.web.bind.annotation.PathVariable;
15
16import java.util.ArrayList;
17import java.util.List;
18
19@Controller
20public 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}
Note: See TracBrowser for help on using the repository browser.