From 0a24be51be5be5675f0b12a5d5cf53b291a123ff Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Tue, 14 Nov 2023 20:31:12 +0100 Subject: [PATCH] Add filteredByDay to OrderController --- .../java/catering/order/CustomOrderRepository.java | 10 ++++++++++ src/main/java/catering/order/OrderController.java | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/catering/order/CustomOrderRepository.java b/src/main/java/catering/order/CustomOrderRepository.java index 96ed457..8ef1831 100644 --- a/src/main/java/catering/order/CustomOrderRepository.java +++ b/src/main/java/catering/order/CustomOrderRepository.java @@ -2,10 +2,12 @@ package catering.order; import org.springframework.stereotype.Component; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; /** * This class is only used for the prototype to avoid using the bloated Salespoint API @@ -34,4 +36,12 @@ public class CustomOrderRepository { public Collection getOrders() { return new ArrayList<>(this.orders); } + + // For Theo: returns all orders that happen on this day + public Collection getOrdersByDate(LocalDate date) { + return this.orders.stream().filter(order -> + (order.getStart().toLocalDate().isBefore(date) || order.getStart().toLocalDate().isEqual(date)) + && (order.getFinish().toLocalDate().isAfter(date) || order.getFinish().toLocalDate().isEqual(date)) + ).collect(Collectors.toList()); + } } diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index e8fbffd..08c4c48 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; import java.time.LocalDate; import java.util.ArrayList; +import java.util.Collection; import java.util.Comparator; import java.util.Optional; @@ -30,6 +31,18 @@ public class OrderController { return "orders"; } + // For Theo: filters orders by day + @GetMapping("/orders/{day}") + public String orders(@PathVariable String day, Model model) { + // Obtains an instance of LocalDate from a text string such as 2007-12-03. (https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) + LocalDate date = LocalDate.parse(day); + + Collection myOrders = orderRepository.getOrdersByDate(date); + model.addAttribute("orders", myOrders); + model.addAttribute("total", myOrders.size()); + return "orders"; + } + @GetMapping("/event") public String event(Model model) { model.addAttribute("orderType", cart.getOrderType());