diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index d294d95..4a8445c 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -88,6 +88,7 @@ public class OrderController { @PreAuthorize("hasRole('CUSTOMER')") public String event(Model model, @ModelAttribute("event") CustomCart cart) { model.addAttribute("items", cart.stream().collect(Collectors.toList())); + model.addAttribute("totalPrice", cart.getPrice()); model.addAttribute("invItems", inventory.findAll().stream().collect(Collectors.toList())); return "event"; } @@ -108,7 +109,20 @@ public class OrderController { @PostMapping("/event/addProduct") @PreAuthorize("hasRole('CUSTOMER')") public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number, @ModelAttribute("event") CustomCart cart) { - cart.addOrUpdateItem(product, Quantity.of(number)); + + Quantity amount = Quantity.of(number); + Quantity invAmount = inventory.findByProduct(product).get().getQuantity(); // TODO ERROR HANDLING + Quantity cartQuantity = cart.getQuantity(product); + + // check for possible miss-inputs + if (amount.add(cartQuantity).isGreaterThan(invAmount)) { + cart.addOrUpdateItem(product, cartQuantity.negate().add(invAmount)); + } else if (amount.add(cartQuantity).isLessThan(Quantity.of(0))) { + cart.addOrUpdateItem(product, cartQuantity.negate()); + } else { + cart.addOrUpdateItem(product, amount); + } + return "redirect:/event"; } @@ -122,7 +136,6 @@ public class OrderController { cart.clear(); List myOrders = orderManagement.findBy(account).stream().collect(Collectors.toList()); - System.out.println(myOrders); return "redirect:/myOrders"; }).orElse("redirect:/event"); diff --git a/src/main/resources/templates/event.html b/src/main/resources/templates/event.html index 44701a3..3eba2eb 100644 --- a/src/main/resources/templates/event.html +++ b/src/main/resources/templates/event.html @@ -33,14 +33,37 @@ -

Product hinzufügen

- - - -
+ Price
+ + +
+
+ +

Produkt hinzufügen

+ + + + + + + + + + + + + + +
NamePreis/StückVerfügbarMenge
NamePreisVerfügbar +
+ + + +
+