2023-11-05 10:39:39 +01:00
|
|
|
package catering.order;
|
|
|
|
|
2023-11-13 16:49:35 +01:00
|
|
|
import org.springframework.data.util.Streamable;
|
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-05 15:36:46 +01:00
|
|
|
import java.util.ArrayList;
|
2023-11-13 16:49:35 +01:00
|
|
|
import java.util.Comparator;
|
2023-11-07 22:23:48 +01:00
|
|
|
import java.util.Optional;
|
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
|
|
|
|
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-12 14:42:07 +01:00
|
|
|
|
|
|
|
@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()));
|
|
|
|
week_to_add_to_month.add(x);
|
|
|
|
}
|
|
|
|
//datesOfTheWeek.clear();
|
|
|
|
weeksOfMonth.add(week_to_add_to_month);
|
|
|
|
}
|
|
|
|
|
2023-11-13 16:49:35 +01:00
|
|
|
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(order.getStart().toLocalDate().compareTo(startDate),0);
|
|
|
|
int end_index_exclusive = Math.min(order.getFinish().toLocalDate().compareTo(startDate), 27) + 1;
|
|
|
|
for (int i = start_index_inclusive; i < end_index_exclusive; i++) {
|
|
|
|
weeksOfMonth.get((int) 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
|
2023-11-12 14:42:07 +01:00
|
|
|
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";
|
|
|
|
}
|
2023-11-05 10:39:39 +01:00
|
|
|
}
|