swt23w23/src/main/java/catering/order/OrderController.java

170 lines
5.9 KiB
Java
Raw Normal View History

package catering.order;
import org.springframework.data.util.Streamable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
2023-11-07 22:23:48 +01:00
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
2023-11-14 20:31:12 +01:00
import java.util.Collection;
import java.util.Comparator;
2023-11-07 22:23:48 +01:00
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";
}
2023-11-05 16:41:22 +01:00
2023-11-14 20:31:12 +01:00
// For Theo: filters orders by day
@GetMapping("/orders/{day}")
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);
Collection<CustomOrder> myOrders = orderRepository.getOrdersByDate(date);
model.addAttribute("orders", myOrders);
model.addAttribute("total", myOrders.size());
return "orders";
}
@GetMapping("/event")
public String event(Model model) {
2023-11-07 22:23:48 +01:00
model.addAttribute("orderType", cart.getOrderType());
model.addAttribute("items", cart.getProucts());
model.addAttribute("productForm", new ProductForm());
return "event";
}
2023-11-05 16:41:22 +01:00
@PostMapping("/orders/remove")
public String removeOrder(@RequestParam int orderID) {
orderRepository.removeOrder(orderID);
return "redirect:/orders";
}
@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());
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) {
2023-11-14 20:43:15 +01:00
LocalDateTime min = LocalDateTime.of(2023, (int) (Math.random() * 11 + 1.0), (int) (Math.random() * 27 + 1.0), (int) (Math.random() * 23 + 1.0), 0, 0);
2023-11-07 22:23:48 +01:00
CustomOrder myOrder = new CustomOrder(
cart.getOrderType(),
2023-11-14 20:43:15 +01:00
min,
min.plusDays((int) (Math.random() * 5)).plusHours((int) (Math.random() * 10)),
2023-11-07 22:23:48 +01:00
cart.getProucts(),
false,
2023-11-14 20:43:15 +01:00
Math.round(Math.random() * 10000)
2023-11-07 22:23:48 +01:00
);
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";
}
@GetMapping("/orders/calender")
public String calender(Model model) {
LocalDate startDate = LocalDate.now();
ArrayList<ArrayList<ArrayList<String>>> weeksOfMonth = new ArrayList<ArrayList<ArrayList<String>>>(4);
for (int i = 0; i < 4; i++) {
ArrayList<LocalDate> datesOfTheWeek = new ArrayList<LocalDate>(7);
LocalDate startOfWeek = startDate.plusDays(7*i);
LocalDate endOfWeekDate = startOfWeek.plusDays(6);
for (LocalDate date = startOfWeek; !date.isAfter(endOfWeekDate); date = date.plusDays(1)) {
datesOfTheWeek.add(date);
}
ArrayList<ArrayList<String>> week_to_add_to_month = new ArrayList<ArrayList<String>>(7);
for (LocalDate date : datesOfTheWeek) {
ArrayList<String> x = new ArrayList<String>(2);
x.add(Integer.toString(date.getDayOfMonth()));
x.add(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
week_to_add_to_month.add(x);
}
//datesOfTheWeek.clear();
weeksOfMonth.add(week_to_add_to_month);
}
LocalDate endDate = startDate.plusDays(27);
// get orders_in_next_month (in future sorted by endDate)
Iterable<CustomOrder> orders_in_next_month = this.orderRepository.getOrders().stream().filter(e ->
!e.getFinish().toLocalDate().isBefore(startDate) && // end is not before today
!e.getStart().toLocalDate().isAfter(endDate)).toList();
for (CustomOrder order : orders_in_next_month) {
int start_index_inclusive = Math.max((int) startDate.until(order.getStart().toLocalDate(), ChronoUnit.DAYS),0);
int end_index_exclusive = Math.min((int) startDate.until(order.getFinish().toLocalDate(), ChronoUnit.DAYS), 27) + 1;
for (int i = start_index_inclusive; i < end_index_exclusive; i++) {
weeksOfMonth.get(Math.floorDiv(i,7)).get(Math.floorMod(i,7)).add(Integer.toString(order.getId()));
}
}
// for (List<List<String>> orders_in_next_week : orders_in_next_month) {
// orders_in_next_weeks.stream().filter(e ->
// e.start.toLocalDate().isEqual(calender_day) || // start day
// e.end.toLocalDate().isEqual(calender_day) || // end day
// e.start.toLocalDate().isBefore(calender_day) && end.toLocalDate().isAfter(calender_day) // day in between start and end
// ).toList().count();
// }
// calendar header with names of week
LocalDate endOfWeekDate = startDate.plusDays(6);
ArrayList<String> dayNames = new ArrayList<String>(7);
for (LocalDate date = startDate; !date.isAfter(endOfWeekDate); date = date.plusDays(1)) {
dayNames.add(date.getDayOfWeek().toString());
}
model.addAttribute("weeksOfMonth", weeksOfMonth);
model.addAttribute("dayNames", dayNames);
return "orders_calender";
}
}