Fix checkout bug where items got deleted

This works on #145.
Before this, if an item got removed out of the inventory by the
admin and the user tried to checkout, the application crashed.
This commit is contained in:
Mathis Kral 2024-01-14 17:04:03 +01:00 committed by Mathis
parent f21b9ba3af
commit 8929868895
2 changed files with 101 additions and 37 deletions

View file

@ -157,23 +157,27 @@ public class OrderController {
@PostMapping("/event/addEmployee")
@PreAuthorize("hasRole('CUSTOMER')")
String addEmployeeToCart(@RequestParam("sid") long employeeId, @ModelAttribute("event") CustomCart cart) {
Employee employee = staffManagement.findById(employeeId).get();
Optional<Employee> employee = staffManagement.findById(employeeId);
if (cart.getStaff().contains(employee)) {
return employee.map(e -> {
if (cart.getStaff().contains(e)) {
return "redirect:/event";
}
cart.addEmployee(e);
return "redirect:/event";
}
cart.addEmployee(employee);
return "redirect:/event";
}).orElse("order-error");
}
@PostMapping("/event/removeEmployee")
@PreAuthorize("hasRole('CUSTOMER')")
String removeEmployeeFromCart(@RequestParam("sid") long employeeId, @ModelAttribute("event") CustomCart cart) {
Employee employee = staffManagement.findById(employeeId).get();
cart.removeEmployee(employee);
Optional<Employee> employee = staffManagement.findById(employeeId);
return "redirect:/event";
return employee.map(e -> {
cart.removeEmployee(e);
return "redirect:/event";
}).orElse("order-error");
}
@PostMapping("/allOrders/remove")
@ -192,37 +196,40 @@ public class OrderController {
@PostMapping("/event/addProduct")
@PreAuthorize("hasRole('CUSTOMER')")
public String addProduct(@RequestParam("pid") Product product,
public String addProduct(@RequestParam("pid") Optional<Product> product,
@RequestParam("number") int number,
@ModelAttribute("event") CustomCart cart) {
// check if product is suitable
if (product.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) {
return product.map(p -> {
// check if product is suitable
if (p.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) {
return "redirect:/event";
}
Quantity amount = p.createQuantity(number > 0 ? number : 1);
Quantity cartQuantity = cart.getQuantity(p);
Quantity available;
if (p instanceof Rentable rentable) {
available = findFreeAmountInInterval(
rentable,
cart.getStart(),
cart.getFinish(),
inventory,
customOrderRepository);
} else {
available = inventory.findByProduct(p).get().getQuantity();
}
// check for possible miss-inputs
if (amount.add(cartQuantity).isGreaterThan(available)) {
cart.addOrUpdateItem(p, cartQuantity.negate().add(available));
} else {
cart.addOrUpdateItem(p, amount);
}
return "redirect:/event";
}
Quantity amount = product.createQuantity(number > 0 ? number : 1);
Quantity cartQuantity = cart.getQuantity(product);
Quantity available;
if (product instanceof Rentable rentable) {
available = findFreeAmountInInterval(
rentable,
cart.getStart(),
cart.getFinish(),
inventory,
customOrderRepository);
} else {
available = inventory.findByProduct(product).get().getQuantity();
}
// check for possible miss-inputs
if (amount.add(cartQuantity).isGreaterThan(available)) {
cart.addOrUpdateItem(product, cartQuantity.negate().add(available));
} else {
cart.addOrUpdateItem(product, amount);
}
return "redirect:/event";
}).orElse("order-error");
}
@PostMapping("/event/removeProduct")
@ -264,6 +271,44 @@ public class OrderController {
}
return userAccount.map(account -> {
for (CartItem item : cart) {
// check if items in cart are still in inventory
if (inventory.findByProduct(item.getProduct()).isEmpty()) {
cart.clear();
return "order-error";
} else {
// check if there are still enough items left
Quantity invQuantity = inventory.findByProduct(item.getProduct()).get().getQuantity();
if (item.getQuantity().isGreaterThan(invQuantity)) {
cart.clear();
return "order-error";
}
// check availability for rentable
if (item.getProduct() instanceof Rentable rentable) {
Quantity available = findFreeAmountInInterval(
rentable,
cart.getStart(),
cart.getFinish(),
inventory,
customOrderRepository);
if (item.getQuantity().isGreaterThan(available)) {
cart.clear();
return "order-error";
}
}
}
}
// check if employees are still available
for (Employee employee : cart.getStaff()) {
if (staffManagement.findById(employee.getId()).isEmpty()) {
cart.clear();
return "order-error";
}
}
CustomOrder myOrder = new CustomOrder(account.getId(), cart);
cart.addItemsTo(myOrder);
cart.addStaffTo(myOrder);

View file

@ -0,0 +1,19 @@
<!--/*-->
SPDX-License-Identifier: Apache-2.0 AND AGPL-3.0-or-later
SPDX-FileCopyrightText: 2024 swt23w23
<!--*/-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout.html(title='Es ist ein Fehler aufgetreten')}">
<body>
<div layout:fragment="content">
<p>
Es tut uns seeeeeeehr leid. Leider ist bei ihrer Bestellung etwas schief gelaufen.
Bitte probieren Sie es später erneut.
</p>
</div>
</body>
</html>