From eab8e3fee0c37147cbfeb63af665967772e6ca7e Mon Sep 17 00:00:00 2001 From: Erik Hohlfeld Date: Sat, 25 Nov 2023 15:31:07 +0100 Subject: [PATCH] Add inventory based product selection --- .../orderCatalog/CatalogController.java | 53 +++++++++++-------- .../orderCatalog/CustomCatalogEntry.java | 1 - .../catering/orderCatalog/CustomProduct.java | 33 ------------ src/main/resources/templates/catalog.html | 4 +- .../resources/templates/catalog_editor.html | 46 ++++++++++------ 5 files changed, 63 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/catering/orderCatalog/CustomProduct.java diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index e56ac00..2f7cd2e 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -2,22 +2,28 @@ package catering.orderCatalog; import catering.order.OrderType; import org.salespointframework.catalog.Product; +import org.salespointframework.inventory.UniqueInventory; +import org.salespointframework.inventory.UniqueInventoryItem; +import org.salespointframework.quantity.Quantity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestParam; +import javax.money.Monetary; import javax.money.MonetaryAmount; import java.util.*; +import java.util.stream.Collectors; @Controller public class CatalogController { private final CustomCatalogEntryRepository catalogEntryRepository; private Set productSet; private CustomCatalogEntry formCatalogEntry; + private final UniqueInventory inventory; - public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) { + public CatalogController(CustomCatalogEntryRepository catalogEntryRepository, UniqueInventory inventory) { productSet = new HashSet<>(); formCatalogEntry = new CustomCatalogEntry( OrderType.EVENT_CATERING, @@ -25,6 +31,7 @@ public class CatalogController { 0, 0); this.catalogEntryRepository = catalogEntryRepository; + this.inventory = inventory; } @GetMapping("/catalog") @@ -34,17 +41,18 @@ public class CatalogController { } @GetMapping("/catalog_editor") - public String catalog_configuration(Model model) { - model.addAttribute("inventory", productSet); + public String editCatalog(Model model) { + model.addAttribute("productSet", productSet); model.addAttribute("formCatalogEntry", formCatalogEntry); + model.addAttribute("inventory", inventory.findAll().stream().collect(Collectors.toList())); return "catalog_editor"; } - @PostMapping("/catalog_editor/catalog_add") - public String catalog_add(@RequestParam OrderType eventType, - @RequestParam int minimumTimePeriod, - @RequestParam int totalCost, - Model model) { + @PostMapping("/catalog_editor/addCatalogEntry") + public String addCatalogEntry(@RequestParam OrderType eventType, + @RequestParam int minimumTimePeriod, + @RequestParam int totalCost, + Model model) { Map products = new HashMap(); for (Product product : productSet) { @@ -63,7 +71,7 @@ public class CatalogController { 0, 0); productSet.clear(); - model.addAttribute("inventory", productSet); + model.addAttribute("productSet", productSet); model.addAttribute("formCatalogEntry", formCatalogEntry); return "redirect:/catalog"; } @@ -77,10 +85,13 @@ public class CatalogController { } */ - @PostMapping("/catalog_editor/product_add") - public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam MonetaryAmount price) { - productSet.add(new Product(name, price)); - formCatalogEntry.addProduct(name, amount); + @PostMapping("/catalog_editor/addProduct") + public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number) { + // TODO: This has to get a price + Quantity amount = Quantity.of(number); + productSet.add(new Product(product.getName(), product.getPrice())); + // TODO: formCatalogEntry.addProduct has to use Quantity amount instead of int number + formCatalogEntry.addProduct(product.getName(), number); return "redirect:/catalog_editor"; } @@ -88,14 +99,14 @@ public class CatalogController { /* TODO: reimplement this function based on Product @PostMapping("/catalog_editor/remove_product") public String removeProduct(@RequestParam String id, Model model) { - Iterator iterator = inventory.iterator(); + Iterator iterator = productSet.iterator(); while (iterator.hasNext()) { Product currentProduct = iterator.next(); - // TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use inventory.remove() + // TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove() if (currentProduct.getId() == Integer.parseInt(id)) { iterator.remove(); - inventory.remove(currentProduct.getId()); + productSet.remove(currentProduct.getId()); currentProduct.getId(); } } @@ -103,11 +114,11 @@ public class CatalogController { } */ - @PostMapping("/catalog_editor/add_time") - public String add_time(@RequestParam int minimumTimePeriod, - @RequestParam OrderType eventType, - @RequestParam Map products, - Model model) { + @PostMapping("/catalog_editor/addTime") + public String addTime(@RequestParam int minimumTimePeriod, + @RequestParam OrderType eventType, + @RequestParam Map products, + Model model) { formCatalogEntry.setMinimumTimePeriod(minimumTimePeriod); model.addAttribute("formCatalogEntry", formCatalogEntry); return "redirect:/catalog_editor"; diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java index 2f87eab..ef32246 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java @@ -6,7 +6,6 @@ import jakarta.persistence.*; import java.util.Map; @Entity -//@Table(name = "CatalogEntries") public class CustomCatalogEntry { static int idCounter = 0; diff --git a/src/main/java/catering/orderCatalog/CustomProduct.java b/src/main/java/catering/orderCatalog/CustomProduct.java deleted file mode 100644 index ac0398b..0000000 --- a/src/main/java/catering/orderCatalog/CustomProduct.java +++ /dev/null @@ -1,33 +0,0 @@ -package catering.orderCatalog; - -public class CustomProduct { - private static int itemIdCounter; - private int itemId; - private String name; - private int amount; - private double cost; - - public CustomProduct(String name, int amount, double cost) { - this.itemId = itemIdCounter; - itemIdCounter++; - this.name = name; - this.amount = amount; - this.cost = cost; - } - - public String getName() { - return name; - } - - public int getAmount() { - return amount; - } - - public double getCost() { - return cost; - } - - public int getItemId() { - return itemId; - } -} diff --git a/src/main/resources/templates/catalog.html b/src/main/resources/templates/catalog.html index 899cf66..1766a2a 100644 --- a/src/main/resources/templates/catalog.html +++ b/src/main/resources/templates/catalog.html @@ -22,13 +22,13 @@ **BILD** - +
- +
diff --git a/src/main/resources/templates/catalog_editor.html b/src/main/resources/templates/catalog_editor.html index c2e9ee1..40d6c04 100644 --- a/src/main/resources/templates/catalog_editor.html +++ b/src/main/resources/templates/catalog_editor.html @@ -22,7 +22,7 @@ Eventbasispreis: 500€

Mindestzeitraum

- + @@ -36,34 +36,46 @@ Menge Stückpreis - + - - € + € - +
-
- - - - - - -
+

Produktauswahl

+ + + + + + + + + + + + + +
ProduktnameKostenVerfügbarMenge
NamePreisVerfügbar +
+ + + +
+

Momentane Auswahl

Eventtyp:
- Mindestzeitraum:
- Gesamtpreis: 90 € -
+ Mindestzeitraum:
+ Gesamtpreis + - +