Add filteredByDay to OrderController

This commit is contained in:
Mathis Kral 2023-11-14 20:31:12 +01:00 committed by Simon Bruder
parent d29c1b4553
commit b3d93685c3
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 23 additions and 0 deletions

View file

@ -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<CustomOrder> getOrders() {
return new ArrayList<>(this.orders);
}
// For Theo: returns all orders that happen on this day
public Collection<CustomOrder> 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());
}
}

View file

@ -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<CustomOrder> 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());