Add adding and removing of CustomProducts inside catalog_editor

This commit is contained in:
Erik Hohlfeld 2023-11-11 20:40:28 +01:00 committed by Simon Bruder
parent 5925ede2f8
commit 580529ab2f
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
3 changed files with 41 additions and 28 deletions

View file

@ -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<CustomProduct> 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";
}

View file

@ -32,8 +32,8 @@ public class CustomCatalogEntry {
return products;
}
public String getMinimumTimePeriod() {
return minimumTimePeriod + " h";
public int getMinimumTimePeriod() {
return minimumTimePeriod;
}
public String getTotalCost() {

View file

@ -3,7 +3,8 @@
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Basisevent-Konfiguration</title></head>
<title>Basisevent-Konfiguration</title>
</head>
<body>
<nav>
<div class="topnav"> <!-- später für css -->
@ -29,13 +30,13 @@
<h2>Mindestzeitraum</h2>
<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"/>
<label>Mindestzeitraum:</label>
<input th:field="*{minimumTimePeriod}" type="number" min="1" step="1" value="1"/>
<button type="submit">Zum Model hinzufügen</button>
</label>
</form>
<span th:text="${formCatalogEntry.minimumTimePeriod}"></span>
<span th:text="${formCatalogEntry}"></span>
<h2>Basisausstattung</h2>
<table>
@ -45,24 +46,26 @@
<th>Stückpreis</th>
</tr>
<tr th:each="item : ${inventory}">
<td th:text="${item}">
<td th:text="${item.getItemId()}">
<td th:text="${item.getName()}">
<td th:text="${item.getAmount()}">
<td th:text="${item.getCost()}">
<td>
<form method="post" th:action="@{/catalog_editor/remove_product}">
<input type="hidden" th:action="${inventory.remove(item.getItemId())}" name="productId" th:value="${item.getItemId()}">
<input type="hidden" name="id" th:value="${item.getItemId()}">
<button type="submit">Entfernen</button>
</form>
</td>
<tr>
<td>
</table>
<form method="post" th:action="@{/catalog_editor/product_add}">
<input type="hidden" name="addProduct">
<label for="name">Produktname</label>
<input type="text" id="name" name="name" required>
<label for="amount">Menge</label>
<input type="number" id="amount" name="amount" min="1" required>
<input type="hidden" name="cost" value="50.0">
<button type="submit">Artikel hinzufügen</button>
</form>
</td>
</tr>
</table>
<br>
Gesamtpreis: 90 €<br>
<form method="post" th:action="@{/catalog_editor/catalog_add}" th:object="${formCatalogEntry}">