Add working filtration of orders for admin

This commit is contained in:
Theo Reichert 2023-12-14 12:04:35 +01:00
parent d6abbdcfa2
commit ea78ec93b7
6 changed files with 66 additions and 12 deletions

View file

@ -1,16 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 swt23w23 // SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.order; 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.order.Order;
import org.salespointframework.quantity.Quantity;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.util.Streamable; import org.springframework.data.util.Streamable;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public interface CustomOrderRepository extends Repository<CustomOrder, Order.OrderIdentifier>{ public interface CustomOrderRepository extends Repository<CustomOrder, Order.OrderIdentifier>{

View file

@ -8,6 +8,7 @@ import catering.catalog.Rentable;
import catering.staff.Employee; import catering.staff.Employee;
import catering.staff.JobType; import catering.staff.JobType;
import catering.staff.StaffManagement; import catering.staff.StaffManagement;
import jakarta.validation.Valid;
import org.salespointframework.catalog.Product; import org.salespointframework.catalog.Product;
import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.inventory.UniqueInventoryItem;
@ -60,25 +61,29 @@ public class OrderController {
model.addAttribute("total", myOrders.size()); model.addAttribute("total", myOrders.size());
model.addAttribute("cancelled", OrderStatus.CANCELED); model.addAttribute("cancelled", OrderStatus.CANCELED);
model.addAttribute("completed", OrderStatus.COMPLETED); model.addAttribute("completed", OrderStatus.COMPLETED);
model.addAttribute("form", new OrderQueryForm());
return "orders"; return "orders";
} }
@GetMapping("/allOrders") @GetMapping("/allOrders")
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
public String orders(Model model) { public String ordersForAdmin(Model model, @Valid @ModelAttribute("form") OrderQueryForm form) {
List<CustomOrder> myOrders = orderManagement.findAll(Pageable.unpaged()).stream().collect(Collectors.toList()); List<CustomOrder> myOrders;
myOrders = form.getOrderStatus()
.map(orderManagement::findBy)
.orElse(orderManagement.findAll(Pageable.unpaged()))
.stream().collect(Collectors.toList());
model.addAttribute("orders", myOrders); model.addAttribute("orders", myOrders);
model.addAttribute("total", myOrders.size()); model.addAttribute("total", myOrders.size());
model.addAttribute("cancelled", OrderStatus.CANCELED); model.addAttribute("cancelled", OrderStatus.CANCELED);
model.addAttribute("completed", OrderStatus.COMPLETED); model.addAttribute("completed", OrderStatus.COMPLETED);
model.addAttribute("form", form);
return "orders"; return "orders";
} }
// For Theo: filters orders by day
@GetMapping("/allOrders/{day}") @GetMapping("/allOrders/{day}")
@PreAuthorize("hasRole('ADMIN')") @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. // Obtains an instance of LocalDate from a text string such as 2007-12-03.
LocalDate date = LocalDate.parse(day); LocalDate date = LocalDate.parse(day);
@ -88,6 +93,7 @@ public class OrderController {
model.addAttribute("orders", myOrders); model.addAttribute("orders", myOrders);
model.addAttribute("total", myOrders.size()); model.addAttribute("total", myOrders.size());
model.addAttribute("form", new OrderQueryForm());
return "orders"; return "orders";
} }

View file

@ -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> orderStatus = Optional.empty();
public Optional<OrderStatus> getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(Optional<OrderStatus> orderStatus) {
this.orderStatus = orderStatus;
}
public static OrderQueryForm of(CustomOrder order) {
OrderQueryForm form = new OrderQueryForm();
form.setOrderStatus(Optional.of(order.getOrderStatus()));
return form;
}
}

View file

@ -10,6 +10,15 @@ SPDX-FileCopyrightText: 2023 swt23w23
<body> <body>
<div layout:fragment="content"> <div layout:fragment="content">
<div> <!--th:unless"${cart.empty}"--> <div> <!--th:unless"${cart.empty}"-->
<form sec:authorize="hasRole('ADMIN')" method="get" th:action="@{/allOrders}" th:object="${form}" class="mb-3">
<label class="form-label">Bestellungsstatus</label>
<select class="form-select" for="orderStatus" th:field="*{orderStatus}" th:errorclass="is-invalid">
<option selected name="orderStatus" th:value="${null}" th:text="any"></option>
<option th:each="orderStatus : ${T(org.salespointframework.order.OrderStatus).values()}" name="orderStatus" th:value="${orderStatus}" th:text="${orderStatus.toString().replace('_','-').toLowerCase()}"/>
</select>
<div th:if="${#fields.hasErrors('orderStatus')}" class="invalid-feedback">Ungültiger Buchungsstatus.</div>
<button class="btn btn-primary" type="submit" th:text="${'suchen'}"></button>
</form>
<table class="table"> <table class="table">
<tr> <tr>
<th>Von</th> <th>Von</th>

View file

@ -14,6 +14,7 @@ import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.inventory.UniqueInventoryItem;
import org.salespointframework.order.CartItem; import org.salespointframework.order.CartItem;
import org.salespointframework.order.OrderManagement; import org.salespointframework.order.OrderManagement;
import org.salespointframework.order.OrderStatus;
import org.salespointframework.quantity.Quantity; import org.salespointframework.quantity.Quantity;
import org.salespointframework.useraccount.Password; import org.salespointframework.useraccount.Password;
import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.Role;
@ -103,6 +104,11 @@ public class OrderControllerIntegrationTests {
.andExpect(content().string(containsString("Menge"))) .andExpect(content().string(containsString("Menge")))
.andExpect(model().attributeExists("orders")); .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!!!! mvc.perform(get("/myOrders").with(user("admin").roles("CUSTOMER"))) // WTF SPRING!11!!!!
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().string(containsString("0"))); .andExpect(content().string(containsString("0")));

View file

@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 swt23w23 // SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.order; package catering.order;
import catering.catalog.Rentable; import catering.catalog.Rentable;
@ -7,6 +7,7 @@ import org.junit.jupiter.api.*;
import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.inventory.UniqueInventoryItem;
import org.salespointframework.order.OrderManagement; import org.salespointframework.order.OrderManagement;
import org.salespointframework.order.OrderStatus;
import org.salespointframework.quantity.Quantity; import org.salespointframework.quantity.Quantity;
import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.UserAccount;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -111,4 +112,13 @@ public class OrderControllerUnitTests {
customOrderRepository) customOrderRepository)
).isEqualTo(Quantity.of(7)); ).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);
}
} }