From 8cc44c8121cd8bf132def9a2ffa1759650e81a9f Mon Sep 17 00:00:00 2001 From: Erik Hohlfeld Date: Sat, 25 Nov 2023 21:20:11 +0100 Subject: [PATCH] Fix the remove methods to work again --- .../orderCatalog/CatalogController.java | 30 ++++++++----------- .../orderCatalog/CustomCatalogEntry.java | 18 +++++------ .../CustomCatalogEntryDataInitializer.java | 7 +++-- .../resources/templates/catalog_editor.html | 2 +- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index 2f7cd2e..dc026d4 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -11,8 +11,6 @@ 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; @@ -27,7 +25,7 @@ public class CatalogController { productSet = new HashSet<>(); formCatalogEntry = new CustomCatalogEntry( OrderType.EVENT_CATERING, - new HashMap(), + new HashMap(), 0, 0); this.catalogEntryRepository = catalogEntryRepository; @@ -53,11 +51,11 @@ public class CatalogController { @RequestParam int minimumTimePeriod, @RequestParam int totalCost, Model model) { - Map products = new HashMap(); + Map products = new HashMap(); for (Product product : productSet) { // TODO: Map each product to its quantity (Quantity) - products.put(product.getName(), 0); + products.put(product.getName(), Quantity.of(3)); } catalogEntryRepository.save(new CustomCatalogEntry( @@ -67,7 +65,7 @@ public class CatalogController { totalCost)); this.formCatalogEntry = new CustomCatalogEntry( OrderType.EVENT_CATERING, - new HashMap(), + new HashMap(), 0, 0); productSet.clear(); @@ -77,42 +75,38 @@ public class CatalogController { } // Removes a catalog entry - /* TODO: reimplement this @PostMapping("/catalog/remove") - public String removeEntry(@RequestParam int catalogEntryID) { - catalogEntryRepository.removeCatalogEntry(catalogEntryID); + public String removeEntry(@RequestParam long catalogEntryID) { + Optional entry = catalogEntryRepository.findById(catalogEntryID); + catalogEntryRepository.delete(catalogEntryRepository.findById(catalogEntryID).get()); + System.out.println(catalogEntryID); + // TODO: use catalogEntryRepository.findById(); to find the object return "redirect:/catalog"; } - */ @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); + formCatalogEntry.addProduct(product.getName(), amount); return "redirect:/catalog_editor"; } // Removes a product from the catalog_editor - /* TODO: reimplement this function based on Product - @PostMapping("/catalog_editor/remove_product") + @PostMapping("/catalog_editor/removeProduct") public String removeProduct(@RequestParam String id, Model model) { Iterator iterator = productSet.iterator(); while (iterator.hasNext()) { Product currentProduct = iterator.next(); // TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove() - if (currentProduct.getId() == Integer.parseInt(id)) { + if (currentProduct.getId().toString().equals(id)) { iterator.remove(); - productSet.remove(currentProduct.getId()); - currentProduct.getId(); } } return "redirect:/catalog_editor"; } - */ @PostMapping("/catalog_editor/addTime") public String addTime(@RequestParam int minimumTimePeriod, diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java index ef32246..af5ebee 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java @@ -2,26 +2,24 @@ package catering.orderCatalog; import catering.order.OrderType; import jakarta.persistence.*; +import org.salespointframework.quantity.Quantity; import java.util.Map; @Entity public class CustomCatalogEntry { - static int idCounter = 0; - @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") - private long id; + private Long id; private OrderType eventType; @ElementCollection - private Map products; + 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. - public CustomCatalogEntry(OrderType eventType, Map products, int minimumTimePeriod, int totalCost) { - this.id = idCounter; - idCounter++; + public CustomCatalogEntry(OrderType eventType, Map products, int minimumTimePeriod, int totalCost) { this.eventType = eventType; this.products = products; this.minimumTimePeriod = minimumTimePeriod; @@ -40,7 +38,7 @@ public class CustomCatalogEntry { return eventType; } - public Map getProducts() { + public Map getProducts() { return products; } @@ -56,11 +54,11 @@ public class CustomCatalogEntry { this.eventType = eventType; } - public void addProduct(String name, Integer count) { + public void addProduct(String name, Quantity count) { this.products.put(name, count); } - public void setProducts(Map products) { + public void setProducts(Map products) { this.products = products; } public void setMinimumTimePeriod(int timePeriod) { diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java b/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java index 44dd345..c9ed444 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntryDataInitializer.java @@ -3,6 +3,7 @@ package catering.orderCatalog; import catering.order.OrderType; import org.salespointframework.catalog.Product; import org.salespointframework.core.DataInitializer; +import org.salespointframework.quantity.Quantity; import org.springframework.stereotype.Component; import javax.money.CurrencyUnit; @@ -31,10 +32,10 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer { .setCurrency(currencyUnit) .setNumber(2) .create()), 30); - // TODO: insert an actual name instead of "new Hashmap<>()" - Map productsFormatted = new HashMap(); + + Map productsFormatted = new HashMap(); for (Product product : products.keySet()) { - productsFormatted.put(product.getName(), 5); + productsFormatted.put(product.getName(), Quantity.of(5)); } System.out.println(productsFormatted); catalogEntryRepository.save( diff --git a/src/main/resources/templates/catalog_editor.html b/src/main/resources/templates/catalog_editor.html index 40d6c04..fed0a98 100644 --- a/src/main/resources/templates/catalog_editor.html +++ b/src/main/resources/templates/catalog_editor.html @@ -40,7 +40,7 @@ € -
+