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 org.springframework.web.bind.annotation.RequestParam;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
@Controller @Controller
@ -33,6 +34,14 @@ public class CatalogController {
return "catalog_editor"; 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 // Removes a catalog entry
@PostMapping("/catalog/remove") @PostMapping("/catalog/remove")
public String removeEntry(@RequestParam int catalogEntryID) { public String removeEntry(@RequestParam int catalogEntryID) {
@ -41,29 +50,30 @@ public class CatalogController {
} }
@PostMapping("/catalog_editor/product_add") @PostMapping("/catalog_editor/product_add")
public String product_add(Model model) { public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam double cost) {
inventory.add(new CustomProduct("Test product", 5, 50)); inventory.add(new CustomProduct(name, amount, cost));
return "redirect:/catalog_editor"; 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 // Removes a product from the catalog_editor
@PostMapping("/catalog_editor/remove_product") @PostMapping("/catalog_editor/remove_product")
public String removeProduct(@RequestParam int productId, Model model) { public String removeProduct(@RequestParam String id, Model model) {
inventory.remove(productId); Iterator<CustomProduct> iterator = inventory.iterator();
model.addAttribute("inventory", inventory); while (iterator.hasNext()) {
CustomProduct currentProduct = iterator.next();
if (currentProduct.getItemId() == Integer.parseInt(id)) {
iterator.remove();
}
}
return "redirect:/catalog_editor"; return "redirect:/catalog_editor";
} }
@PostMapping("/catalog_editor/add_time") @PostMapping("/catalog_editor/add_time")
public String add_time(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) { public String add_time(@ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) {
System.out.println(formCatalogEntry.getMinimumTimePeriod());
model.addAttribute("formCatalogEntry", formCatalogEntry); model.addAttribute("formCatalogEntry", formCatalogEntry);
// Might not work because on every GetMapping of /catalog_editor a new formCatalogEntry is created
return "redirect:/catalog_editor"; return "redirect:/catalog_editor";
} }

View file

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

View file

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