2023-11-05 10:39:39 +01:00
|
|
|
package catering.order;
|
|
|
|
|
2023-11-07 22:23:48 +01:00
|
|
|
import org.salespointframework.quantity.Quantity;
|
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-05 15:36:46 +01:00
|
|
|
import java.util.ArrayList;
|
2023-11-07 22:23:48 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
2023-11-05 15:36:46 +01:00
|
|
|
|
2023-11-05 10:39:39 +01:00
|
|
|
@Controller
|
|
|
|
public class OrderController {
|
|
|
|
|
2023-11-05 12:33:43 +01:00
|
|
|
private final CustomOrderRepository orderRepository;
|
2023-11-07 20:53:31 +01:00
|
|
|
private final CustomCart cart;
|
2023-11-05 12:33:43 +01:00
|
|
|
|
2023-11-07 20:53:31 +01:00
|
|
|
public OrderController(CustomOrderRepository orderRepository, CustomCart cart) {
|
2023-11-05 12:33:43 +01:00
|
|
|
this.orderRepository = orderRepository;
|
2023-11-07 20:53:31 +01:00
|
|
|
this.cart = cart;
|
2023-11-05 12:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/orders")
|
|
|
|
public String orders(Model model) {
|
2023-11-05 15:36:46 +01:00
|
|
|
model.addAttribute("orders", orderRepository.getOrders());
|
2023-11-05 12:33:43 +01:00
|
|
|
model.addAttribute("total", orderRepository.getOrders().size());
|
|
|
|
return "orders";
|
|
|
|
}
|
2023-11-05 16:41:22 +01:00
|
|
|
|
2023-11-07 17:50:29 +01:00
|
|
|
@GetMapping("/event")
|
2023-11-07 20:53:31 +01:00
|
|
|
public String event(Model model) {
|
2023-11-07 22:23:48 +01:00
|
|
|
model.addAttribute("orderType", cart.getOrderType());
|
2023-11-07 20:53:31 +01:00
|
|
|
model.addAttribute("items", cart.getProucts());
|
|
|
|
model.addAttribute("productForm", new ProductForm());
|
|
|
|
return "event";
|
2023-11-07 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
2023-11-05 16:41:22 +01:00
|
|
|
@PostMapping("/orders/remove")
|
|
|
|
public String removeOrder(@RequestParam int orderID) {
|
|
|
|
orderRepository.removeOrder(orderID);
|
|
|
|
return "redirect:/orders";
|
|
|
|
}
|
2023-11-07 20:53:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/event/addProduct")
|
2023-11-07 22:23:48 +01:00
|
|
|
public String addProduct(@ModelAttribute ProductForm productForm, Model model) {
|
|
|
|
cart.addProduct(productForm.getProduct(), productForm.getNumber());
|
|
|
|
model.addAttribute("orderType", cart.getOrderType());
|
2023-11-07 20:53:31 +01:00
|
|
|
model.addAttribute("items", cart.getProucts());
|
|
|
|
model.addAttribute("productForm", new ProductForm());
|
|
|
|
return "redirect:/event";
|
|
|
|
}
|
2023-11-07 22:23:48 +01:00
|
|
|
|
|
|
|
@PostMapping("/event/checkout")
|
|
|
|
public String checkout(Model model) {
|
|
|
|
CustomOrder myOrder = new CustomOrder(
|
|
|
|
cart.getOrderType(),
|
|
|
|
LocalDateTime.MIN,
|
|
|
|
LocalDateTime.MAX,
|
|
|
|
cart.getProucts(),
|
|
|
|
false,
|
|
|
|
Double.MAX_VALUE
|
|
|
|
);
|
|
|
|
|
|
|
|
orderRepository.addOrder(myOrder);
|
|
|
|
model.addAttribute("orders", orderRepository.getOrders());
|
|
|
|
model.addAttribute("total", orderRepository.getOrders().size());
|
|
|
|
cart.resetCart();
|
|
|
|
return "redirect:/orders";
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/event/changeOrderType")
|
|
|
|
public String changeOrderType(@RequestParam(name = "orderType") Optional<String> optionalOrderType, Model model) {
|
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":
|
|
|
|
cart.setOrderType(CustomOrder.OrderType.RENT_A_COOK);
|
|
|
|
break;
|
|
|
|
case "EK":
|
|
|
|
cart.setOrderType(CustomOrder.OrderType.EVENT_CATERING);
|
|
|
|
break;
|
|
|
|
case "SN":
|
|
|
|
cart.setOrderType(CustomOrder.OrderType.SUSHI_NIGHT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return "redirect:/event";
|
|
|
|
}
|
2023-11-05 10:39:39 +01:00
|
|
|
}
|