From 695bc1d82184bade6b829f007004a07bbc86cecb Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 21 Nov 2023 22:42:23 +0100 Subject: [PATCH] Do not use MonetaryAmount in inventory mutate form While this creates duplicate code in the controller, it will vastly simplify handling the form values. --- .../inventory/InventoryController.java | 19 +++++++++----- .../inventory/InventoryMutateForm.java | 25 +++++++++---------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/catering/inventory/InventoryController.java b/src/main/java/catering/inventory/InventoryController.java index 53feb6c..be5cb59 100644 --- a/src/main/java/catering/inventory/InventoryController.java +++ b/src/main/java/catering/inventory/InventoryController.java @@ -16,6 +16,8 @@ */ package catering.inventory; +import static org.salespointframework.core.Currencies.EURO; +import org.javamoney.moneta.Money; import org.salespointframework.catalog.Product; import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; @@ -72,9 +74,9 @@ class InventoryController { product.setName(form.getName()); product.setType(form.getType()); - product.setPrice(form.getRetailPrice()); - product.setWholesalePrice(form.getWholesalePrice()); - product.setPromotionPrice(form.getPromotionPrice().orElse(null)); + product.setPrice(Money.of(form.getRetailPrice(), EURO)); + product.setWholesalePrice(Money.of(form.getWholesalePrice(), EURO)); + product.setPromotionPrice(form.getPromotionPrice().map(price -> Money.of(price, EURO)).orElse(null)); product = cateringCatalog.save(product); @@ -86,7 +88,10 @@ class InventoryController { @PreAuthorize("hasRole('ADMIN')") @GetMapping("/inventory/add") - String add() { + String add(Model model) { + model.addAttribute("actionIsAdd", true); + model.addAttribute("form", InventoryMutateForm.empty()); + return "inventory-mutate"; } @@ -97,8 +102,10 @@ class InventoryController { return "inventory-mutate"; } inventory.save(new UniqueInventoryItem( - cateringCatalog.save(new CatalogDummy(form.getName(), form.getType(), form.getRetailPrice(), - form.getWholesalePrice(), form.getPromotionPrice().orElse(null))), + cateringCatalog + .save(new CatalogDummy(form.getName(), form.getType(), Money.of(form.getRetailPrice(), EURO), + Money.of(form.getWholesalePrice(), EURO), + form.getPromotionPrice().map(price -> Money.of(price, EURO)).orElse(null))), form.getQuantity())); return "redirect:/inventory"; } diff --git a/src/main/java/catering/inventory/InventoryMutateForm.java b/src/main/java/catering/inventory/InventoryMutateForm.java index dcdf99f..3bff024 100644 --- a/src/main/java/catering/inventory/InventoryMutateForm.java +++ b/src/main/java/catering/inventory/InventoryMutateForm.java @@ -16,13 +16,8 @@ */ package catering.inventory; -import static org.salespointframework.core.Currencies.EURO; - import java.util.Optional; -import javax.money.MonetaryAmount; - -import org.javamoney.moneta.Money; import org.salespointframework.quantity.Quantity; import catering.catalog.CatalogDummyType; @@ -34,8 +29,8 @@ class InventoryMutateForm { private final @NotNull CatalogDummyType type; private final @NotEmpty String name; private final @NotNull Quantity quantity; - private final @NotNull MonetaryAmount wholesalePrice, retailPrice; - private final @NotNull Optional promotionPrice; + private final @NotNull double wholesalePrice, retailPrice; + private final @NotNull Optional promotionPrice; public InventoryMutateForm(@NotNull CatalogDummyType type, @NotEmpty String name, @NotNull Quantity quantity, @PositiveOrZero double wholesalePrice, @PositiveOrZero double retailPrice, @@ -43,9 +38,13 @@ class InventoryMutateForm { this.type = type; this.name = name; this.quantity = quantity; - this.wholesalePrice = Money.of(wholesalePrice, EURO); - this.retailPrice = Money.of(retailPrice, EURO); - this.promotionPrice = promotionPrice.map(price -> (MonetaryAmount) Money.of(price, EURO)); + this.wholesalePrice = wholesalePrice; + this.retailPrice = retailPrice; + this.promotionPrice = promotionPrice; + } + + public static InventoryMutateForm empty() { + return new InventoryMutateForm(null, "", Quantity.of(0), 0, 0, Optional.empty()); } public CatalogDummyType getType() { @@ -60,15 +59,15 @@ class InventoryMutateForm { return quantity; } - public MonetaryAmount getWholesalePrice() { + public double getWholesalePrice() { return wholesalePrice; } - public MonetaryAmount getRetailPrice() { + public double getRetailPrice() { return retailPrice; } - public Optional getPromotionPrice() { + public Optional getPromotionPrice() { return promotionPrice; } }