diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index ee45af6..e56ac00 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -1,22 +1,24 @@ package catering.orderCatalog; import catering.order.OrderType; +import org.salespointframework.catalog.Product; 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.MonetaryAmount; import java.util.*; @Controller public class CatalogController { - private CustomCatalogEntryRepository catalogEntryRepository; - private Set inventory; + private final CustomCatalogEntryRepository catalogEntryRepository; + private Set productSet; private CustomCatalogEntry formCatalogEntry; public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) { - inventory = new HashSet<>(); + productSet = new HashSet<>(); formCatalogEntry = new CustomCatalogEntry( OrderType.EVENT_CATERING, new HashMap(), @@ -27,13 +29,13 @@ public class CatalogController { @GetMapping("/catalog") public String catalog(Model model) { - model.addAttribute("catalogEntries", catalogEntryRepository.getCatalogEntries()); + model.addAttribute("catalogEntries", catalogEntryRepository.findAll()); return "catalog"; } @GetMapping("/catalog_editor") public String catalog_configuration(Model model) { - model.addAttribute("inventory", inventory); + model.addAttribute("inventory", productSet); model.addAttribute("formCatalogEntry", formCatalogEntry); return "catalog_editor"; } @@ -45,11 +47,12 @@ public class CatalogController { Model model) { Map products = new HashMap(); - for (CustomProduct product : inventory) { - products.put(product.getName(), product.getAmount()); + for (Product product : productSet) { + // TODO: Map each product to its quantity (Quantity) + products.put(product.getName(), 0); } - catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry( + catalogEntryRepository.save(new CustomCatalogEntry( eventType, products, minimumTimePeriod, @@ -59,39 +62,46 @@ public class CatalogController { new HashMap(), 0, 0); - inventory.clear(); - model.addAttribute("inventory", inventory); + productSet.clear(); + model.addAttribute("inventory", productSet); model.addAttribute("formCatalogEntry", formCatalogEntry); return "redirect:/catalog"; } // Removes a catalog entry + /* TODO: reimplement this @PostMapping("/catalog/remove") public String removeEntry(@RequestParam int catalogEntryID) { catalogEntryRepository.removeCatalogEntry(catalogEntryID); return "redirect:/catalog"; } + */ @PostMapping("/catalog_editor/product_add") - public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam double cost) { - inventory.add(new CustomProduct(name, amount, cost)); + public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam MonetaryAmount price) { + productSet.add(new Product(name, price)); formCatalogEntry.addProduct(name, amount); return "redirect:/catalog_editor"; } // Removes a product from the catalog_editor + /* 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 = inventory.iterator(); while (iterator.hasNext()) { - CustomProduct currentProduct = iterator.next(); + Product currentProduct = iterator.next(); - if (currentProduct.getItemId() == Integer.parseInt(id)) { + // TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use inventory.remove() + if (currentProduct.getId() == Integer.parseInt(id)) { iterator.remove(); + inventory.remove(currentProduct.getId()); + currentProduct.getId(); } } return "redirect:/catalog_editor"; } + */ @PostMapping("/catalog_editor/add_time") public String add_time(@RequestParam int minimumTimePeriod, diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java index 0b5bd6b..2f87eab 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java @@ -1,13 +1,21 @@ package catering.orderCatalog; import catering.order.OrderType; +import jakarta.persistence.*; import java.util.Map; +@Entity +//@Table(name = "CatalogEntries") public class CustomCatalogEntry { static int idCounter = 0; - private int id; + + @Id + @Column(name = "id") + private long id; private OrderType eventType; + + @ElementCollection private Map products; private int minimumTimePeriod; // Declared as int for simplification of the prototype. Only whole hours. private int totalCost; // Should actually accumulate the price of all items. @@ -21,7 +29,11 @@ public class CustomCatalogEntry { this.totalCost = totalCost; } - public int getId() { + public CustomCatalogEntry() { + + } + + public long getId() { return id; } diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java b/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java index dc8d47d..44dd345 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java @@ -1,9 +1,12 @@ package catering.orderCatalog; import catering.order.OrderType; +import org.salespointframework.catalog.Product; import org.salespointframework.core.DataInitializer; import org.springframework.stereotype.Component; +import javax.money.CurrencyUnit; +import javax.money.Monetary; import java.util.HashMap; import java.util.Map; @@ -18,17 +21,27 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer { @Override public void initialize() { - Map products = new HashMap<>(); - products.put("Brötchen", 30); - products.put("Kerze", 20); - catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.EVENT_CATERING, products, 4, 500)); - - products = new HashMap<>(); - products.put("Kuchen", 3); - products.put("Pizza Funghi", 1); - products.put("Pizza Margherita", 1); - products.put("Pizza Quattro Formaggi", 1); - products.put("Ballon", 20); - catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.RENT_A_COOK, products, 2, 1000)); + Map products = new HashMap<>(); + CurrencyUnit currencyUnit = Monetary.getCurrency("EUR"); + products.put(new Product("Brötchen", Monetary.getDefaultAmountFactory() + .setCurrency(currencyUnit) + .setNumber(1) + .create()), 30); + products.put(new Product("Kerze", Monetary.getDefaultAmountFactory() + .setCurrency(currencyUnit) + .setNumber(2) + .create()), 30); + // TODO: insert an actual name instead of "new Hashmap<>()" + Map productsFormatted = new HashMap(); + for (Product product : products.keySet()) { + productsFormatted.put(product.getName(), 5); + } + System.out.println(productsFormatted); + catalogEntryRepository.save( + new CustomCatalogEntry( + OrderType.EVENT_CATERING, + productsFormatted, + 4, + 500)); } } diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntryRepository.java b/src/main/java/catering/orderCatalog/CustomCatalogEntryRepository.java index 53ac2b5..36c46d1 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntryRepository.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntryRepository.java @@ -1,32 +1,9 @@ package catering.orderCatalog; -import org.springframework.stereotype.Component; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.util.Streamable; -import java.util.HashSet; -import java.util.Set; - -@Component -public class CustomCatalogEntryRepository { - private Set catalogEntries; - - public CustomCatalogEntryRepository() { - this.catalogEntries = new HashSet<>(); - } - - public boolean addCatalogEntry(CustomCatalogEntry catalogEntry) { - return catalogEntries.add(catalogEntry); - } - - public boolean removeCatalogEntry(int catalogEntryID) { - for (CustomCatalogEntry catalogEntry : catalogEntries) { - if (catalogEntry.getId() == catalogEntryID) { - return this.catalogEntries.remove(catalogEntry); - } - } - return false; - } - - public Set getCatalogEntries() { - return catalogEntries; - } +interface CustomCatalogEntryRepository extends CrudRepository { + @Override + Streamable findAll(); } diff --git a/src/main/resources/templates/catalog.html b/src/main/resources/templates/catalog.html index a75b70c..899cf66 100644 --- a/src/main/resources/templates/catalog.html +++ b/src/main/resources/templates/catalog.html @@ -25,7 +25,7 @@
    -
  • +