Do not use MonetaryAmount in inventory mutate form

While this creates duplicate code in the controller, it will vastly
simplify handling the form values.
This commit is contained in:
Simon Bruder 2023-11-21 22:42:23 +01:00
parent f7d859b25d
commit 695bc1d821
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 25 additions and 19 deletions

View file

@ -16,6 +16,8 @@
*/ */
package catering.inventory; package catering.inventory;
import static org.salespointframework.core.Currencies.EURO;
import org.javamoney.moneta.Money;
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;
@ -72,9 +74,9 @@ class InventoryController {
product.setName(form.getName()); product.setName(form.getName());
product.setType(form.getType()); product.setType(form.getType());
product.setPrice(form.getRetailPrice()); product.setPrice(Money.of(form.getRetailPrice(), EURO));
product.setWholesalePrice(form.getWholesalePrice()); product.setWholesalePrice(Money.of(form.getWholesalePrice(), EURO));
product.setPromotionPrice(form.getPromotionPrice().orElse(null)); product.setPromotionPrice(form.getPromotionPrice().map(price -> Money.of(price, EURO)).orElse(null));
product = cateringCatalog.save(product); product = cateringCatalog.save(product);
@ -86,7 +88,10 @@ class InventoryController {
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@GetMapping("/inventory/add") @GetMapping("/inventory/add")
String add() { String add(Model model) {
model.addAttribute("actionIsAdd", true);
model.addAttribute("form", InventoryMutateForm.empty());
return "inventory-mutate"; return "inventory-mutate";
} }
@ -97,8 +102,10 @@ class InventoryController {
return "inventory-mutate"; return "inventory-mutate";
} }
inventory.save(new UniqueInventoryItem( inventory.save(new UniqueInventoryItem(
cateringCatalog.save(new CatalogDummy(form.getName(), form.getType(), form.getRetailPrice(), cateringCatalog
form.getWholesalePrice(), form.getPromotionPrice().orElse(null))), .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())); form.getQuantity()));
return "redirect:/inventory"; return "redirect:/inventory";
} }

View file

@ -16,13 +16,8 @@
*/ */
package catering.inventory; package catering.inventory;
import static org.salespointframework.core.Currencies.EURO;
import java.util.Optional; import java.util.Optional;
import javax.money.MonetaryAmount;
import org.javamoney.moneta.Money;
import org.salespointframework.quantity.Quantity; import org.salespointframework.quantity.Quantity;
import catering.catalog.CatalogDummyType; import catering.catalog.CatalogDummyType;
@ -34,8 +29,8 @@ class InventoryMutateForm {
private final @NotNull CatalogDummyType type; private final @NotNull CatalogDummyType type;
private final @NotEmpty String name; private final @NotEmpty String name;
private final @NotNull Quantity quantity; private final @NotNull Quantity quantity;
private final @NotNull MonetaryAmount wholesalePrice, retailPrice; private final @NotNull double wholesalePrice, retailPrice;
private final @NotNull Optional<MonetaryAmount> promotionPrice; private final @NotNull Optional<Double> promotionPrice;
public InventoryMutateForm(@NotNull CatalogDummyType type, @NotEmpty String name, public InventoryMutateForm(@NotNull CatalogDummyType type, @NotEmpty String name,
@NotNull Quantity quantity, @PositiveOrZero double wholesalePrice, @PositiveOrZero double retailPrice, @NotNull Quantity quantity, @PositiveOrZero double wholesalePrice, @PositiveOrZero double retailPrice,
@ -43,9 +38,13 @@ class InventoryMutateForm {
this.type = type; this.type = type;
this.name = name; this.name = name;
this.quantity = quantity; this.quantity = quantity;
this.wholesalePrice = Money.of(wholesalePrice, EURO); this.wholesalePrice = wholesalePrice;
this.retailPrice = Money.of(retailPrice, EURO); this.retailPrice = retailPrice;
this.promotionPrice = promotionPrice.map(price -> (MonetaryAmount) Money.of(price, EURO)); this.promotionPrice = promotionPrice;
}
public static InventoryMutateForm empty() {
return new InventoryMutateForm(null, "", Quantity.of(0), 0, 0, Optional.empty());
} }
public CatalogDummyType getType() { public CatalogDummyType getType() {
@ -60,15 +59,15 @@ class InventoryMutateForm {
return quantity; return quantity;
} }
public MonetaryAmount getWholesalePrice() { public double getWholesalePrice() {
return wholesalePrice; return wholesalePrice;
} }
public MonetaryAmount getRetailPrice() { public double getRetailPrice() {
return retailPrice; return retailPrice;
} }
public Optional<MonetaryAmount> getPromotionPrice() { public Optional<Double> getPromotionPrice() {
return promotionPrice; return promotionPrice;
} }
} }