mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
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:
parent
f21b9ba3af
commit
8929868895
|
@ -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(employee);
|
||||
cart.addEmployee(e);
|
||||
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 employee.map(e -> {
|
||||
cart.removeEmployee(e);
|
||||
return "redirect:/event";
|
||||
}).orElse("order-error");
|
||||
}
|
||||
|
||||
@PostMapping("/allOrders/remove")
|
||||
|
@ -192,19 +196,21 @@ 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) {
|
||||
|
||||
return product.map(p -> {
|
||||
// check if product is suitable
|
||||
if (product.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) {
|
||||
if (p.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) {
|
||||
return "redirect:/event";
|
||||
}
|
||||
|
||||
Quantity amount = product.createQuantity(number > 0 ? number : 1);
|
||||
Quantity cartQuantity = cart.getQuantity(product);
|
||||
Quantity amount = p.createQuantity(number > 0 ? number : 1);
|
||||
Quantity cartQuantity = cart.getQuantity(p);
|
||||
Quantity available;
|
||||
|
||||
if (product instanceof Rentable rentable) {
|
||||
if (p instanceof Rentable rentable) {
|
||||
available = findFreeAmountInInterval(
|
||||
rentable,
|
||||
cart.getStart(),
|
||||
|
@ -212,17 +218,18 @@ public class OrderController {
|
|||
inventory,
|
||||
customOrderRepository);
|
||||
} else {
|
||||
available = inventory.findByProduct(product).get().getQuantity();
|
||||
available = inventory.findByProduct(p).get().getQuantity();
|
||||
}
|
||||
|
||||
// check for possible miss-inputs
|
||||
if (amount.add(cartQuantity).isGreaterThan(available)) {
|
||||
cart.addOrUpdateItem(product, cartQuantity.negate().add(available));
|
||||
cart.addOrUpdateItem(p, cartQuantity.negate().add(available));
|
||||
} else {
|
||||
cart.addOrUpdateItem(product, amount);
|
||||
cart.addOrUpdateItem(p, 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);
|
||||
|
|
19
src/main/resources/templates/order-error.html
Normal file
19
src/main/resources/templates/order-error.html
Normal 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>
|
Loading…
Reference in a new issue