Add forms to the catalog editor to submit its data

This commit is contained in:
Erik Hohlfeld 2023-11-10 18:35:12 +01:00 committed by Simon Bruder
parent 94a03e2266
commit 89eed0755d
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 65 additions and 29 deletions

View file

@ -2,14 +2,23 @@ package catering.orderCatalog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Controller
public class CatalogController {
private CustomCatalogEntryRepository catalogEntryRepository;
private Set<CustomProduct> inventory;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) {
inventory = new HashSet<>();
this.catalogEntryRepository = catalogEntryRepository;
}
@ -20,13 +29,44 @@ public class CatalogController {
}
@GetMapping("/catalog_editor")
public String event_configuration() {
public String catalog_configuration(Model model) {
model.addAttribute("inventory", inventory);
model.addAttribute("formCatalogEntry", new CustomCatalogEntry());
return "catalog_editor";
}
// Removes a catalog entry
@PostMapping("/catalog/remove")
public String removeItem() {
public String removeEntry(@RequestParam int catalogEntryID) {
catalogEntryRepository.removeCatalogEntry(catalogEntryID);
return "catalog_editor";
}
@PostMapping("/catalog_editor/product_add")
public String product_add(Model model) {
inventory.add(new CustomProduct("Test product", 5, 50));
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);
return "redirect:/catalog_editor";
}
@PostMapping("/catalog_editor/add_time")
public String add_time(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) {
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog_editor";
}
}

View file

@ -3,8 +3,7 @@
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Basiseventkonfiguration</title>
</head>
<title>Basisevent-Konfiguration</title></head>
<body>
<nav>
<div class="topnav"> <!-- später für css -->
@ -16,6 +15,7 @@
</div>
</nav>
<h1>Basisevent konfigurieren</h1>
<input type="hidden" name="addCatalog">
<div class="content">
<h2>Dienstleistung</h2>
<label for="dienstleistungen"></label>
@ -28,9 +28,14 @@
Eventbasispreis: 500€
<h2>Mindestzeitraum</h2>
<label>Mindestzeitraum:
<input type="number" name="amount" min="1" step="1" value="1"/>
</label>h
<form th:object="${formCatalogEntry}" method="post" th:action="@{/catalog_editor/add_time}">
<label>Mindestzeitraum:
<input th:field="*{minimumTimePeriod}" type="number" name="minimumTimePeriod" min="1" step="1" value="1"/>
<button type="submit">Zum Model hinzufügen</button>
</label>
</form>
<span th:text="${formCatalogEntry.minimumTimePeriod}"></span>
<h2>Basisausstattung</h2>
<table>
@ -39,39 +44,30 @@
<th>Menge</th>
<th>Stückpreis</th>
</tr>
<tr>
<td>Brötchen</td>
<tr th:each="item : ${inventory}">
<td th:text="${item.getName()}">
<td th:text="${item.getAmount()}">
<td th:text="${item.getCost()}">
<td>
<input type="number" name="amount" min="1" step="1" value="1"/>
</td>
<td>1,00 €</td>
<td>
<form method="post" th:action="@{/catalog/remove}">
<input type="hidden" name="itemID" value="0"/>
<input type="submit" value="remove" th:value="Entfernen"/>
<form method="post" th:action="@{/catalog_editor/remove_product}">
<input type="hidden" th:action="${inventory.remove(item.getItemId())}" name="productId" th:value="${item.getItemId()}">
<button type="submit">Entfernen</button>
</form>
</td>
</tr>
<tr>
<td>Kerze</td>
<td>
<input type="number" name="amount" min="1" step="1" value="1"/>
</td>
<td>3.00 €</td>
<td>
<form method="post" th:action="@{/catalog/remove}">
<input type="hidden" name="itemID" value="0"/>
<input type="submit" value="remove" th:value="Entfernen"/>
<form method="post" th:action="@{/catalog_editor/product_add}">
<input type="hidden" name="addProduct">
<button type="submit">Artikel hinzufügen</button>
</form>
</td>
</tr>
<tr>
<td>[Artikel Hinzufügen]</td>
</tr>
</table>
<br>
Gesamtpreis: 90 €<br>
<button>Zum Katalog hinzufügen</button>
<form method="post" th:action="@{/catalog_editor/catalog_add}" th:object="${formCatalogEntry}">
<button type="submit">Zum Katalog hinzufügen</button>
</form>
</div>
</body>
</html>