From 97c7e2a16015077b22cdc1cb90cff008b4ec4506 Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Sun, 5 Nov 2023 16:41:22 +0100 Subject: [PATCH] Add order removal --- src/main/java/catering/order/CustomOrder.java | 6 ++++++ .../order/CustomOrderDataInitializer.java | 14 ++++++++++++++ .../catering/order/CustomOrderRepository.java | 9 +++++++-- src/main/java/catering/order/OrderController.java | 8 ++++++++ src/main/resources/templates/orders.html | 15 +++++++++------ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index ab5a865..8c93cd9 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -12,6 +12,7 @@ import java.util.Map; */ public class CustomOrder { + private int id; private OrderType orderType; private LocalDateTime start; private LocalDateTime finish; @@ -21,6 +22,7 @@ public class CustomOrder { private DateTimeFormatter formatter; public CustomOrder(OrderType orderType, LocalDateTime start, LocalDateTime finish, Map products, boolean invoiceAvailable, double totalCost) { + this.id = (int) (Math.random() * Integer.MAX_VALUE); this.orderType = orderType; this.start = start; this.finish = finish; @@ -30,6 +32,10 @@ public class CustomOrder { this.formatter = DateTimeFormatter.ofPattern("MM.dd.yyy, HH:mm 'Uhr'"); } + public int getId() { + return id; + } + public OrderType getOrderType() { return orderType; } diff --git a/src/main/java/catering/order/CustomOrderDataInitializer.java b/src/main/java/catering/order/CustomOrderDataInitializer.java index 44caf7d..ec632a1 100644 --- a/src/main/java/catering/order/CustomOrderDataInitializer.java +++ b/src/main/java/catering/order/CustomOrderDataInitializer.java @@ -45,5 +45,19 @@ public class CustomOrderDataInitializer implements DataInitializer { true, 10000.0 )); + + products = new HashMap<>(); + products.put("Koch", Quantity.of(2)); + products.put("Wiener Schnitzel (vegan)", Quantity.of(1000)); + products.put("Wiener Würstchen", Quantity.of(150)); + products.put("Weißwurst (vegan)", Quantity.of(300)); + orderRepository.addOrder(new CustomOrder( + CustomOrder.OrderType.RENT_A_COOK, + LocalDateTime.of(2023, 12, 24, 11, 0), + LocalDateTime.of(2023, 12, 25, 14, 0), + products, + true, + 4000.0 + )); } } diff --git a/src/main/java/catering/order/CustomOrderRepository.java b/src/main/java/catering/order/CustomOrderRepository.java index 0f39efa..96ed457 100644 --- a/src/main/java/catering/order/CustomOrderRepository.java +++ b/src/main/java/catering/order/CustomOrderRepository.java @@ -22,8 +22,13 @@ public class CustomOrderRepository { return this.orders.add(order); } - public boolean removeOrder(CustomOrder order) { - return this.orders.remove(order); + public boolean removeOrder(int orderID) { + for (CustomOrder order : orders) { + if (order.getId() == orderID) { + return this.orders.remove(order); + } + } + return false; } public Collection getOrders() { diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index b6705c1..02db231 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -3,6 +3,8 @@ package catering.order; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; import java.util.ArrayList; @@ -21,4 +23,10 @@ public class OrderController { model.addAttribute("total", orderRepository.getOrders().size()); return "orders"; } + + @PostMapping("/orders/remove") + public String removeOrder(@RequestParam int orderID) { + orderRepository.removeOrder(orderID); + return "redirect:/orders"; + } } diff --git a/src/main/resources/templates/orders.html b/src/main/resources/templates/orders.html index a49e580..3ad4fe5 100644 --- a/src/main/resources/templates/orders.html +++ b/src/main/resources/templates/orders.html @@ -16,7 +16,7 @@
-

Auftragsliste

+

Auftragsliste

@@ -31,7 +31,7 @@ Preis - + @@ -51,8 +51,11 @@ - - + +
+ + +
@@ -66,8 +69,8 @@