From ea78ec93b7f5ffba23f4999ed567878c2aeba925 Mon Sep 17 00:00:00 2001 From: Theo Reichert Date: Thu, 14 Dec 2023 12:04:35 +0100 Subject: [PATCH] Add working filtration of orders for admin --- .../catering/order/CustomOrderRepository.java | 7 +---- .../java/catering/order/OrderController.java | 16 +++++++---- .../java/catering/order/OrderQueryForm.java | 28 +++++++++++++++++++ src/main/resources/templates/orders.html | 9 ++++++ .../OrderControllerIntegrationTests.java | 6 ++++ .../order/OrderControllerUnitTests.java | 12 +++++++- 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/main/java/catering/order/OrderQueryForm.java diff --git a/src/main/java/catering/order/CustomOrderRepository.java b/src/main/java/catering/order/CustomOrderRepository.java index d55327e..25f0b56 100644 --- a/src/main/java/catering/order/CustomOrderRepository.java +++ b/src/main/java/catering/order/CustomOrderRepository.java @@ -1,16 +1,11 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -// SPDX-FileCopyrightText: 2023 swt23w23 +// SPDX-FileCopyrightText: 2023-2024 swt23w23 package catering.order; -import catering.catalog.Rentable; -import org.salespointframework.inventory.UniqueInventory; -import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.Order; -import org.salespointframework.quantity.Quantity; import org.springframework.data.jpa.repository.Query; import org.springframework.data.util.Streamable; import org.springframework.data.repository.Repository; - import java.time.LocalDateTime; public interface CustomOrderRepository extends Repository{ diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index b33107f..077570b 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -8,6 +8,7 @@ import catering.catalog.Rentable; import catering.staff.Employee; import catering.staff.JobType; import catering.staff.StaffManagement; +import jakarta.validation.Valid; import org.salespointframework.catalog.Product; import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; @@ -60,25 +61,29 @@ public class OrderController { model.addAttribute("total", myOrders.size()); model.addAttribute("cancelled", OrderStatus.CANCELED); model.addAttribute("completed", OrderStatus.COMPLETED); + model.addAttribute("form", new OrderQueryForm()); return "orders"; } @GetMapping("/allOrders") @PreAuthorize("hasRole('ADMIN')") - public String orders(Model model) { - List myOrders = orderManagement.findAll(Pageable.unpaged()).stream().collect(Collectors.toList()); - + public String ordersForAdmin(Model model, @Valid @ModelAttribute("form") OrderQueryForm form) { + List myOrders; + myOrders = form.getOrderStatus() + .map(orderManagement::findBy) + .orElse(orderManagement.findAll(Pageable.unpaged())) + .stream().collect(Collectors.toList()); model.addAttribute("orders", myOrders); model.addAttribute("total", myOrders.size()); model.addAttribute("cancelled", OrderStatus.CANCELED); model.addAttribute("completed", OrderStatus.COMPLETED); + model.addAttribute("form", form); return "orders"; } - // For Theo: filters orders by day @GetMapping("/allOrders/{day}") @PreAuthorize("hasRole('ADMIN')") - public String orders(@PathVariable String day, Model model) { + public String ordersForAdmin(@PathVariable String day, Model model) { // Obtains an instance of LocalDate from a text string such as 2007-12-03. LocalDate date = LocalDate.parse(day); @@ -88,6 +93,7 @@ public class OrderController { model.addAttribute("orders", myOrders); model.addAttribute("total", myOrders.size()); + model.addAttribute("form", new OrderQueryForm()); return "orders"; } diff --git a/src/main/java/catering/order/OrderQueryForm.java b/src/main/java/catering/order/OrderQueryForm.java new file mode 100644 index 0000000..1494b0b --- /dev/null +++ b/src/main/java/catering/order/OrderQueryForm.java @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +// SPDX-FileCopyrightText: 2023-2024 swt23w23 +package catering.order; + +import jakarta.validation.constraints.NotNull; +import org.salespointframework.order.OrderStatus; + +import java.util.Optional; + + +class OrderQueryForm { + private @NotNull Optional orderStatus = Optional.empty(); + + public Optional getOrderStatus() { + return orderStatus; + } + + public void setOrderStatus(Optional orderStatus) { + this.orderStatus = orderStatus; + } + + public static OrderQueryForm of(CustomOrder order) { + OrderQueryForm form = new OrderQueryForm(); + form.setOrderStatus(Optional.of(order.getOrderStatus())); + return form; + + } +} diff --git a/src/main/resources/templates/orders.html b/src/main/resources/templates/orders.html index 8537339..a83eb4d 100644 --- a/src/main/resources/templates/orders.html +++ b/src/main/resources/templates/orders.html @@ -10,6 +10,15 @@ SPDX-FileCopyrightText: 2023 swt23w23
+
+ + +
Ungültiger Buchungsstatus.
+ +
diff --git a/src/test/java/catering/order/OrderControllerIntegrationTests.java b/src/test/java/catering/order/OrderControllerIntegrationTests.java index 8e943bf..93e6bc8 100644 --- a/src/test/java/catering/order/OrderControllerIntegrationTests.java +++ b/src/test/java/catering/order/OrderControllerIntegrationTests.java @@ -14,6 +14,7 @@ import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.CartItem; import org.salespointframework.order.OrderManagement; +import org.salespointframework.order.OrderStatus; import org.salespointframework.quantity.Quantity; import org.salespointframework.useraccount.Password; import org.salespointframework.useraccount.Role; @@ -103,6 +104,11 @@ public class OrderControllerIntegrationTests { .andExpect(content().string(containsString("Menge"))) .andExpect(model().attributeExists("orders")); + mvc.perform(get("/myOrders").param("orderStatus", OrderStatus.COMPLETED.toString()).with(user("andi").roles("CUSTOMER"))) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Menge"))) + .andExpect(model().attributeExists("orders")); + mvc.perform(get("/myOrders").with(user("admin").roles("CUSTOMER"))) // WTF SPRING!11!!!! .andExpect(status().isOk()) .andExpect(content().string(containsString("0"))); diff --git a/src/test/java/catering/order/OrderControllerUnitTests.java b/src/test/java/catering/order/OrderControllerUnitTests.java index 2dcc40b..ba08ddc 100644 --- a/src/test/java/catering/order/OrderControllerUnitTests.java +++ b/src/test/java/catering/order/OrderControllerUnitTests.java @@ -1,5 +1,5 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -// SPDX-FileCopyrightText: 2023 swt23w23 +// SPDX-FileCopyrightText: 2023-2024 swt23w23 package catering.order; import catering.catalog.Rentable; @@ -7,6 +7,7 @@ import org.junit.jupiter.api.*; import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.OrderManagement; +import org.salespointframework.order.OrderStatus; import org.salespointframework.quantity.Quantity; import org.salespointframework.useraccount.UserAccount; import org.springframework.beans.factory.annotation.Autowired; @@ -111,4 +112,13 @@ public class OrderControllerUnitTests { customOrderRepository) ).isEqualTo(Quantity.of(7)); } + + @Test + @Order(5) + void ordersByOrderStatus() { + assertThat(orderManagement.findBy(OrderStatus.PAID).stream().toList()).hasSize(0); + assertThat(orderManagement.findBy(OrderStatus.OPEN).stream().toList()).hasSize(0); + assertThat(orderManagement.findBy(OrderStatus.COMPLETED).stream().toList()).hasSize(2); + assertThat(orderManagement.findBy(OrderStatus.CANCELED).stream().toList()).hasSize(0); + } }
Von