From 580529ab2fd075fec88ab75bc7e55d0543774ef8 Mon Sep 17 00:00:00 2001 From: Erik Hohlfeld Date: Sat, 11 Nov 2023 20:40:28 +0100 Subject: [PATCH] Add adding and removing of CustomProducts inside catalog_editor --- .../orderCatalog/CatalogController.java | 32 +++++++++++------- .../orderCatalog/CustomCatalogEntry.java | 4 +-- .../resources/templates/catalog_editor.html | 33 ++++++++++--------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index 3ffa89f..f33ce2f 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -8,6 +8,7 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestParam; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; @Controller @@ -33,6 +34,14 @@ public class CatalogController { return "catalog_editor"; } + @PostMapping("/catalog_editor/catalog_add") + public String catalog_add(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) { + catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry( + // TODO: Fill with formCatalogEntry's data + )); + return "redirect:/catalog"; + } + // Removes a catalog entry @PostMapping("/catalog/remove") public String removeEntry(@RequestParam int catalogEntryID) { @@ -41,29 +50,30 @@ public class CatalogController { } @PostMapping("/catalog_editor/product_add") - public String product_add(Model model) { - inventory.add(new CustomProduct("Test product", 5, 50)); + public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam double cost) { + inventory.add(new CustomProduct(name, amount, cost)); return "redirect:/catalog_editor"; } - @PostMapping("/catalog_editor/catalog_add") - public String catalog_add(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) { - catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry()); - return "redirect:/catalog"; - } - // Removes a product from the catalog_editor @PostMapping("/catalog_editor/remove_product") - public String removeProduct(@RequestParam int productId, Model model) { - inventory.remove(productId); - model.addAttribute("inventory", inventory); + public String removeProduct(@RequestParam String id, Model model) { + Iterator iterator = inventory.iterator(); + while (iterator.hasNext()) { + CustomProduct currentProduct = iterator.next(); + if (currentProduct.getItemId() == Integer.parseInt(id)) { + iterator.remove(); + } + } return "redirect:/catalog_editor"; } @PostMapping("/catalog_editor/add_time") public String add_time(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) { + System.out.println(formCatalogEntry.getMinimumTimePeriod()); model.addAttribute("formCatalogEntry", formCatalogEntry); + // Might not work because on every GetMapping of /catalog_editor a new formCatalogEntry is created return "redirect:/catalog_editor"; } diff --git a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java index 4c6c1d1..58bc624 100644 --- a/src/main/java/catering/orderCatalog/CustomCatalogEntry.java +++ b/src/main/java/catering/orderCatalog/CustomCatalogEntry.java @@ -32,8 +32,8 @@ public class CustomCatalogEntry { return products; } - public String getMinimumTimePeriod() { - return minimumTimePeriod + " h"; + public int getMinimumTimePeriod() { + return minimumTimePeriod; } public String getTotalCost() { diff --git a/src/main/resources/templates/catalog_editor.html b/src/main/resources/templates/catalog_editor.html index aa202d1..fdbd39b 100644 --- a/src/main/resources/templates/catalog_editor.html +++ b/src/main/resources/templates/catalog_editor.html @@ -3,7 +3,8 @@ xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> - Basisevent-Konfiguration + Basisevent-Konfiguration +