swt23w23/src/main/java/catering/order/OrderController.java
2023-11-21 18:00:25 +01:00

92 lines
2.5 KiB
Java

package catering.order;
import org.salespointframework.quantity.Quantity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;
@Controller
public class OrderController {
private final CustomOrderRepository orderRepository;
private final CustomCart cart;
public OrderController(CustomOrderRepository orderRepository, CustomCart cart) {
this.orderRepository = orderRepository;
this.cart = cart;
}
@GetMapping("/orders")
public String orders(Model model) {
model.addAttribute("orders", orderRepository.getOrders());
model.addAttribute("total", orderRepository.getOrders().size());
return "orders";
}
@GetMapping("/event")
public String event(Model model) {
model.addAttribute("orderType", cart.getOrderType());
model.addAttribute("items", cart.getProucts());
model.addAttribute("productForm", new ProductForm());
return "event";
}
@PostMapping("/orders/remove")
public String removeOrder(@RequestParam int orderID) {
orderRepository.removeOrder(orderID);
return "redirect:/orders";
}
@PostMapping("/event/addProduct")
public String addProduct(@ModelAttribute ProductForm productForm, Model model) {
cart.addProduct(productForm.getProduct(), productForm.getNumber());
model.addAttribute("orderType", cart.getOrderType());
model.addAttribute("items", cart.getProucts());
model.addAttribute("productForm", new ProductForm());
return "redirect:/event";
}
@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) {
String orderType = optionalOrderType.orElse("FOO");
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";
}
}