Add error handling for inventory

This commit is contained in:
Paul Heimer 2023-11-20 23:26:47 +01:00 committed by Simon Bruder
parent 6bd1c23e2d
commit f1f9539aae
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 22 additions and 12 deletions

View file

@ -33,6 +33,7 @@ import org.springframework.ui.Model;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -85,9 +86,9 @@ class InventoryController {
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@PostMapping("/inventory/edit/{pid}") @PostMapping("/inventory/edit/{pid}")
String edit(@Valid InventoryMutateForm form, Errors result, @PathVariable Product pid) { String edit(@Valid @ModelAttribute("form") InventoryMutateForm form, Errors result, @PathVariable Product pid, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "redirect:/inventory/edit/" + pid.getId(); return edit(model, pid, form);
} }
CatalogDummy product = (CatalogDummy) pid; CatalogDummy product = (CatalogDummy) pid;
UniqueInventoryItem item = inventory.findByProduct(pid).get(); UniqueInventoryItem item = inventory.findByProduct(pid).get();
@ -109,17 +110,20 @@ class InventoryController {
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@GetMapping("/inventory/add") @GetMapping("/inventory/add")
String add(Model model) { String add(Model model) {
model.addAttribute("actionIsAdd", true); return add(model, InventoryMutateForm.empty());
model.addAttribute("form", InventoryMutateForm.empty()); }
String add(Model model, InventoryMutateForm form) {
model.addAttribute("actionIsAdd", true);
model.addAttribute("form", form);
return "inventory-mutate"; return "inventory-mutate";
} }
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@PostMapping("/inventory/add") @PostMapping("/inventory/add")
String add(@Valid InventoryMutateForm form, Errors result) { String add(@Valid @ModelAttribute("form") InventoryMutateForm form, Errors result, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "inventory-mutate"; return add(model, form);
} }
inventory.save(new UniqueInventoryItem( inventory.save(new UniqueInventoryItem(
cateringCatalog cateringCatalog

View file

@ -10,30 +10,36 @@
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="type">Typ</label> <label class="form-label" for="type">Typ</label>
<div class="form-check form-check-inline" th:each="type : ${T(catering.catalog.CatalogDummyType).values()}"> <div class="form-check form-check-inline" th:each="type : ${T(catering.catalog.CatalogDummyType).values()}">
<input class="form-check-input" type="radio" th:field="*{type}" th:value="${type}" th:text="${type}" required/> <input class="form-check-input" type="radio" th:field="*{type}" th:value="${type}" th:text="${type}" th:errorclass="is-invalid" required/>
<div th:if="${#fields.hasErrors('type')}" class="invalid-feedback">Ungültiger Typ.</div>
</div> </div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="name">Produktname</label> <label class="form-label" for="name">Produktname</label>
<input class="form-control" type="text" th:field="*{name}" required/> <input class="form-control" type="text" th:field="*{name}" th:errorclass="is-invalid" required/>
<div th:if="${#fields.hasErrors('name')}" class="invalid-feedback">Ungültiger Name.</div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="quantity">Menge im Bestand</label> <label class="form-label" for="quantity">Menge im Bestand</label>
<input class="form-control" type="number" th:field="*{quantity}" required/> <input class="form-control" type="number" th:field="*{quantity}" th:errorclass="is-invalid" required/>
<div th:if="${#fields.hasErrors('quantity')}" class="invalid-feedback">Ungültige Menge.</div>
</div> </div>
<!-- the prices arent bound with th:field as they need special formatting --> <!-- the prices arent bound with th:field as they need special formatting -->
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="wholesalePrice">Einkaufspreis</label> <label class="form-label" for="wholesalePrice">Einkaufspreis</label>
<input class="form-control" type="number" th:field="*{wholesalePrice}" step="0.01" min="0" required/> <input class="form-control" type="number" name="wholesalePrice" th:value="${#numbers.formatDecimal(form.wholesalePrice, 1, 2)}" th:errorclass="is-invalid" step="0.01" min="0" required/>
<div th:if="${#fields.hasErrors('wholesalePrice')}" class="invalid-feedback">Ungültiger Einkaufspreis.</div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="retailPrice">UVP</label> <label class="form-label" for="retailPrice">UVP</label>
<input class="form-control" type="number" th:field="*{retailPrice}" step="0.01" min="0" required/> <input class="form-control" type="number" name="retailPrice" th:value="${#numbers.formatDecimal(form.retailPrice, 1, 2)}" th:errorclass="is-invalid" step="0.01" min="0" required/>
<div th:if="${#fields.hasErrors('retailPrice')}" class="invalid-feedback">Ungültiger Verkaufspreis.</div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<!-- FIXME darf nur bei angeboten als teil von partyservice angezeigt werden --> <!-- FIXME darf nur bei angeboten als teil von partyservice angezeigt werden -->
<label class="form-label" for="promotionPrice">Aktionspreis</label> <label class="form-label" for="promotionPrice">Aktionspreis</label>
<input class="form-control" type="number" th:field="*{promotionPrice}" step="0.01" min="0"/> <input class="form-control" type="number" name="promotionPrice" th:value="${#numbers.formatDecimal(form.promotionPrice.orElse(null), 1, 2)}" th:errorclass="is-invalid" step="0.01" min="0"/>
<div th:if="${#fields.hasErrors('promotionPrice')}" class="invalid-feedback">Ungültiger Aktionspreis.</div>
</div> </div>
<button class="btn btn-primary" type="submit" th:text="${actionIsAdd ? 'Hinzufügen' : 'Bearbeiten'}"></button> <button class="btn btn-primary" type="submit" th:text="${actionIsAdd ? 'Hinzufügen' : 'Bearbeiten'}"></button>
<!-- KANN: Bild und Beschreibungstext --> <!-- KANN: Bild und Beschreibungstext -->