diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index 2ea4789..03d018f 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -157,23 +157,27 @@ public class OrderController { @PostMapping("/event/addEmployee") @PreAuthorize("hasRole('CUSTOMER')") String addEmployeeToCart(@RequestParam("sid") long employeeId, @ModelAttribute("event") CustomCart cart) { - Employee employee = staffManagement.findById(employeeId).get(); + Optional employee = staffManagement.findById(employeeId); - if (cart.getStaff().contains(employee)) { + return employee.map(e -> { + if (cart.getStaff().contains(e)) { + return "redirect:/event"; + } + + cart.addEmployee(e); return "redirect:/event"; - } - - cart.addEmployee(employee); - return "redirect:/event"; + }).orElse("order-error"); } @PostMapping("/event/removeEmployee") @PreAuthorize("hasRole('CUSTOMER')") String removeEmployeeFromCart(@RequestParam("sid") long employeeId, @ModelAttribute("event") CustomCart cart) { - Employee employee = staffManagement.findById(employeeId).get(); - cart.removeEmployee(employee); + Optional employee = staffManagement.findById(employeeId); - return "redirect:/event"; + return employee.map(e -> { + cart.removeEmployee(e); + return "redirect:/event"; + }).orElse("order-error"); } @PostMapping("/allOrders/remove") @@ -192,37 +196,40 @@ public class OrderController { @PostMapping("/event/addProduct") @PreAuthorize("hasRole('CUSTOMER')") - public String addProduct(@RequestParam("pid") Product product, + public String addProduct(@RequestParam("pid") Optional product, @RequestParam("number") int number, @ModelAttribute("event") CustomCart cart) { - // check if product is suitable - if (product.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) { + + return product.map(p -> { + // check if product is suitable + if (p.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) { + return "redirect:/event"; + } + + Quantity amount = p.createQuantity(number > 0 ? number : 1); + Quantity cartQuantity = cart.getQuantity(p); + Quantity available; + + if (p instanceof Rentable rentable) { + available = findFreeAmountInInterval( + rentable, + cart.getStart(), + cart.getFinish(), + inventory, + customOrderRepository); + } else { + available = inventory.findByProduct(p).get().getQuantity(); + } + + // check for possible miss-inputs + if (amount.add(cartQuantity).isGreaterThan(available)) { + cart.addOrUpdateItem(p, cartQuantity.negate().add(available)); + } else { + cart.addOrUpdateItem(p, amount); + } + return "redirect:/event"; - } - - Quantity amount = product.createQuantity(number > 0 ? number : 1); - Quantity cartQuantity = cart.getQuantity(product); - Quantity available; - - if (product instanceof Rentable rentable) { - available = findFreeAmountInInterval( - rentable, - cart.getStart(), - cart.getFinish(), - inventory, - customOrderRepository); - } else { - available = inventory.findByProduct(product).get().getQuantity(); - } - - // check for possible miss-inputs - if (amount.add(cartQuantity).isGreaterThan(available)) { - cart.addOrUpdateItem(product, cartQuantity.negate().add(available)); - } else { - cart.addOrUpdateItem(product, amount); - } - - return "redirect:/event"; + }).orElse("order-error"); } @PostMapping("/event/removeProduct") @@ -264,6 +271,44 @@ public class OrderController { } return userAccount.map(account -> { + for (CartItem item : cart) { + // check if items in cart are still in inventory + if (inventory.findByProduct(item.getProduct()).isEmpty()) { + cart.clear(); + return "order-error"; + } else { + // check if there are still enough items left + Quantity invQuantity = inventory.findByProduct(item.getProduct()).get().getQuantity(); + if (item.getQuantity().isGreaterThan(invQuantity)) { + cart.clear(); + return "order-error"; + } + + // check availability for rentable + if (item.getProduct() instanceof Rentable rentable) { + Quantity available = findFreeAmountInInterval( + rentable, + cart.getStart(), + cart.getFinish(), + inventory, + customOrderRepository); + + if (item.getQuantity().isGreaterThan(available)) { + cart.clear(); + return "order-error"; + } + } + } + } + + // check if employees are still available + for (Employee employee : cart.getStaff()) { + if (staffManagement.findById(employee.getId()).isEmpty()) { + cart.clear(); + return "order-error"; + } + } + CustomOrder myOrder = new CustomOrder(account.getId(), cart); cart.addItemsTo(myOrder); cart.addStaffTo(myOrder); diff --git a/src/main/resources/templates/order-error.html b/src/main/resources/templates/order-error.html new file mode 100644 index 0000000..61802c3 --- /dev/null +++ b/src/main/resources/templates/order-error.html @@ -0,0 +1,19 @@ + +SPDX-License-Identifier: Apache-2.0 AND AGPL-3.0-or-later +SPDX-FileCopyrightText: 2024 swt23w23 + + + + + +
+

+ Es tut uns seeeeeeehr leid. Leider ist bei ihrer Bestellung etwas schief gelaufen. + Bitte probieren Sie es später erneut. +

+
+ +