2023-11-05 10:39:39 +01:00
|
|
|
package catering.order;
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
import org.salespointframework.catalog.Product;
|
|
|
|
import org.salespointframework.inventory.UniqueInventory;
|
|
|
|
import org.salespointframework.inventory.UniqueInventoryItem;
|
|
|
|
import org.salespointframework.order.Cart;
|
|
|
|
import org.salespointframework.order.Order;
|
|
|
|
import org.salespointframework.order.OrderManagement;
|
|
|
|
import org.salespointframework.quantity.Quantity;
|
|
|
|
import org.salespointframework.time.Interval;
|
|
|
|
import org.salespointframework.useraccount.Role;
|
|
|
|
import org.salespointframework.useraccount.UserAccount;
|
|
|
|
import org.salespointframework.useraccount.web.LoggedIn;
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2023-11-05 10:39:39 +01:00
|
|
|
import org.springframework.stereotype.Controller;
|
2023-11-05 12:33:43 +01:00
|
|
|
import org.springframework.ui.Model;
|
2023-11-07 20:53:31 +01:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2023-11-05 10:39:39 +01:00
|
|
|
|
2023-11-07 22:23:48 +01:00
|
|
|
import java.time.LocalDateTime;
|
2023-11-12 14:42:07 +01:00
|
|
|
import java.time.LocalDate;
|
2023-11-15 00:31:09 +01:00
|
|
|
import java.time.temporal.ChronoUnit;
|
2023-11-15 00:34:16 +01:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2023-11-05 15:36:46 +01:00
|
|
|
import java.util.ArrayList;
|
2023-11-14 20:31:12 +01:00
|
|
|
import java.util.Collection;
|
2023-11-19 13:00:21 +01:00
|
|
|
import java.util.List;
|
2023-11-07 22:23:48 +01:00
|
|
|
import java.util.Optional;
|
2023-11-19 13:00:21 +01:00
|
|
|
import java.util.stream.Collectors;
|
2023-11-05 15:36:46 +01:00
|
|
|
|
2023-11-12 14:42:07 +01:00
|
|
|
|
2023-11-05 10:39:39 +01:00
|
|
|
@Controller
|
2023-11-19 13:00:21 +01:00
|
|
|
@PreAuthorize("isAuthenticated()")
|
|
|
|
@SessionAttributes("event")
|
2023-11-05 10:39:39 +01:00
|
|
|
public class OrderController {
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
private final OrderManagement<CustomOrder> orderManagement;
|
|
|
|
private final UniqueInventory<UniqueInventoryItem> inventory;
|
2023-11-05 12:33:43 +01:00
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public OrderController(OrderManagement<CustomOrder> orderManagement, UniqueInventory<UniqueInventoryItem> inventory) {
|
|
|
|
this.orderManagement = orderManagement;
|
|
|
|
this.inventory = inventory;
|
2023-11-05 12:33:43 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
@GetMapping("/myOrders")
|
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String orders(Model model, @LoggedIn Optional<UserAccount> userAccount) {
|
|
|
|
List<CustomOrder> myOrders = orderManagement.findBy(userAccount.get()).stream().collect(Collectors.toList()); // to be changed
|
|
|
|
|
|
|
|
model.addAttribute("orders", myOrders);
|
|
|
|
model.addAttribute("total", myOrders.size());
|
|
|
|
return "orders";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/allOrders")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
2023-11-05 12:33:43 +01:00
|
|
|
public String orders(Model model) {
|
2023-11-19 13:00:21 +01:00
|
|
|
List<CustomOrder> myOrders = orderManagement.findAll(Pageable.unpaged()).stream().collect(Collectors.toList());
|
|
|
|
|
|
|
|
model.addAttribute("orders", myOrders);
|
|
|
|
model.addAttribute("total", myOrders.size());
|
2023-11-05 12:33:43 +01:00
|
|
|
return "orders";
|
|
|
|
}
|
2023-11-05 16:41:22 +01:00
|
|
|
|
2023-11-14 20:31:12 +01:00
|
|
|
// For Theo: filters orders by day
|
2023-11-19 13:00:21 +01:00
|
|
|
@GetMapping("/allOrders/{day}")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
2023-11-14 20:31:12 +01:00
|
|
|
public String orders(@PathVariable String day, Model model) {
|
|
|
|
// Obtains an instance of LocalDate from a text string such as 2007-12-03. (https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)
|
|
|
|
LocalDate date = LocalDate.parse(day);
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
List<CustomOrder> myOrders = orderManagement.findAll(Pageable.unpaged()).stream().filter(
|
|
|
|
order ->
|
|
|
|
(order.getStart().toLocalDate().isBefore(date) || order.getStart().toLocalDate().isEqual(date))
|
|
|
|
&& (order.getFinish().toLocalDate().isAfter(date) || order.getFinish().toLocalDate().isEqual(date))
|
|
|
|
).collect(Collectors.toList());
|
|
|
|
|
2023-11-14 20:31:12 +01:00
|
|
|
model.addAttribute("orders", myOrders);
|
|
|
|
model.addAttribute("total", myOrders.size());
|
|
|
|
return "orders";
|
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
@ModelAttribute("event")
|
|
|
|
CustomCart initializeCart() {
|
|
|
|
return new CustomCart(OrderType.SOMETHING_ELSE, LocalDateTime.now(), LocalDateTime.now());
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:50:29 +01:00
|
|
|
@GetMapping("/event")
|
2023-11-19 13:00:21 +01:00
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String event(Model model, @ModelAttribute("event") CustomCart cart) {
|
|
|
|
model.addAttribute("items", cart.stream().collect(Collectors.toList()));
|
|
|
|
model.addAttribute("totalPrice", cart.getPrice());
|
|
|
|
model.addAttribute("invItems", inventory.findAll().stream().collect(Collectors.toList()));
|
2023-11-07 20:53:31 +01:00
|
|
|
return "event";
|
2023-11-07 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
@PostMapping("/allOrders/remove")
|
|
|
|
public String removeOrder(@RequestParam Order.OrderIdentifier orderID, @LoggedIn Optional<UserAccount> userAccount) {
|
|
|
|
return userAccount.map(account -> {
|
|
|
|
if (account.hasRole(Role.of("ADMIN"))) {
|
|
|
|
CustomOrder myOrder = orderManagement.get(orderID).get(); // FIXME
|
|
|
|
orderManagement.cancelOrder(myOrder, "I have my own reasons.");
|
|
|
|
return "redirect:/allOrders";
|
|
|
|
}
|
|
|
|
return "redirect:/";
|
|
|
|
}).orElse("redirect:/");
|
2023-11-05 16:41:22 +01:00
|
|
|
}
|
2023-11-07 20:53:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/event/addProduct")
|
2023-11-19 13:00:21 +01:00
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number, @ModelAttribute("event") CustomCart cart) {
|
|
|
|
|
|
|
|
Quantity amount = Quantity.of(number);
|
|
|
|
Quantity invAmount = inventory.findByProduct(product).get().getQuantity(); // TODO ERROR HANDLING
|
|
|
|
Quantity cartQuantity = cart.getQuantity(product);
|
|
|
|
|
|
|
|
// check for possible miss-inputs
|
|
|
|
if (amount.add(cartQuantity).isGreaterThan(invAmount)) {
|
|
|
|
cart.addOrUpdateItem(product, cartQuantity.negate().add(invAmount));
|
|
|
|
} else if (amount.add(cartQuantity).isLessThan(Quantity.of(0))) {
|
|
|
|
cart.addOrUpdateItem(product, cartQuantity.negate());
|
|
|
|
} else {
|
|
|
|
cart.addOrUpdateItem(product, amount);
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:53:31 +01:00
|
|
|
return "redirect:/event";
|
|
|
|
}
|
2023-11-07 22:23:48 +01:00
|
|
|
|
|
|
|
@PostMapping("/event/checkout")
|
2023-11-19 13:00:21 +01:00
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String checkout(@ModelAttribute("event") CustomCart cart, @LoggedIn Optional<UserAccount> userAccount, Model model) {
|
|
|
|
|
|
|
|
return userAccount.map(account -> {
|
|
|
|
CustomOrder myOrder = new CustomOrder(account.getId(), cart);
|
|
|
|
cart.addItemsTo(myOrder);
|
|
|
|
orderManagement.payOrder(myOrder); // TODO: change this later
|
|
|
|
orderManagement.completeOrder(myOrder);
|
|
|
|
cart.clear();
|
|
|
|
|
|
|
|
List<CustomOrder> myOrders = orderManagement.findBy(account).stream().collect(Collectors.toList());
|
|
|
|
|
|
|
|
return "redirect:/myOrders";
|
|
|
|
}).orElse("redirect:/event");
|
2023-11-07 22:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/event/changeOrderType")
|
2023-11-19 13:00:21 +01:00
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String changeOrderType(@RequestParam(name = "type") Optional<String> optionalOrderType, @ModelAttribute("event") CustomCart cart) {
|
2023-11-09 22:17:47 +01:00
|
|
|
String orderType = optionalOrderType.orElse("FOO");
|
2023-11-07 22:23:48 +01:00
|
|
|
switch (orderType) {
|
|
|
|
case "RaK":
|
2023-11-19 13:00:21 +01:00
|
|
|
cart.setOrderType(OrderType.RENT_A_COOK);
|
2023-11-07 22:23:48 +01:00
|
|
|
break;
|
|
|
|
case "EK":
|
2023-11-19 13:00:21 +01:00
|
|
|
cart.setOrderType(OrderType.EVENT_CATERING);
|
2023-11-07 22:23:48 +01:00
|
|
|
break;
|
|
|
|
case "SN":
|
2023-11-19 13:00:21 +01:00
|
|
|
cart.setOrderType(OrderType.SUSHI_NIGHT);
|
|
|
|
break;
|
|
|
|
case "MB":
|
|
|
|
cart.setOrderType(OrderType.MOBILE_BREAKFAST);
|
2023-11-07 22:23:48 +01:00
|
|
|
break;
|
|
|
|
default:
|
2023-11-19 13:00:21 +01:00
|
|
|
cart.setOrderType(OrderType.SOMETHING_ELSE);
|
2023-11-07 22:23:48 +01:00
|
|
|
}
|
|
|
|
return "redirect:/event";
|
|
|
|
}
|
2023-11-12 14:42:07 +01:00
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
@PostMapping("/event/changeOrderTime")
|
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String changeOrderTime(@RequestParam(name = "start") Optional<String> startTime, @RequestParam(name = "finish") Optional<String> finishTime, @ModelAttribute("event") CustomCart cart) {
|
|
|
|
LocalDateTime start = LocalDateTime.parse(startTime.orElse("2024-12-03T10:15:30"));
|
|
|
|
LocalDateTime finish = LocalDateTime.parse(finishTime.orElse("2024-12-03T10:19:30"));
|
2023-11-12 14:42:07 +01:00
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
cart.setStart(start);
|
|
|
|
cart.setFinish(finish);
|
2023-11-13 16:49:35 +01:00
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
return "redirect:/event";
|
2023-11-12 14:42:07 +01:00
|
|
|
}
|
2023-11-05 10:39:39 +01:00
|
|
|
}
|