swt23w23/src/main/java/catering/orderCatalog/CatalogController.java
2024-01-19 19:00:51 +01:00

157 lines
5.4 KiB
Java

// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.orderCatalog;
import catering.order.OrderType;
import catering.staff.Employee;
import catering.staff.StaffManagement;
import org.javamoney.moneta.Money;
import org.salespointframework.catalog.Product;
import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem;
import org.salespointframework.quantity.Quantity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.*;
import java.util.stream.Collectors;
@Controller
public class CatalogController {
private final CustomCatalogEntryRepository catalogEntryRepository;
private Map<Product, Quantity> productMap;
private CustomCatalogEntry formCatalogEntry;
private final UniqueInventory<UniqueInventoryItem> inventory;
@Autowired
private CartService cartService;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository,
UniqueInventory<UniqueInventoryItem> inventory,
CartService cartService) {
productMap = new HashMap<>();
formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<Product, Quantity>(),
Money.of(0, "EUR"));
this.catalogEntryRepository = catalogEntryRepository;
this.inventory = inventory;
this.cartService = cartService;
}
@GetMapping("/catalog")
public String catalog(Model model) {
model.addAttribute("catalogEntries", catalogEntryRepository.findAll());
return "catalog";
}
@GetMapping("/catalog_editor")
public String editCatalog(Model model) {
model.addAttribute("productMap", productMap);
model.addAttribute("formCatalogEntry", formCatalogEntry);
model.addAttribute("inventory", inventory.findAll().stream()
.filter(i -> i.getProduct()
.getCategories()
.stream().anyMatch(c -> c.equals(formCatalogEntry.getEventType().toString())))
.collect(Collectors.toList()));
return "catalog_editor";
}
@PostMapping("/catalog_editor/addCatalogEntry")
public String addCatalogEntry(@RequestParam OrderType eventType,
@RequestParam double totalCost,
Model model) {
Map<Product, Quantity> products = new HashMap<Product, Quantity>();
for (Product product : productMap.keySet()) {
products.put(product, productMap.get(product));
}
catalogEntryRepository.save(new CustomCatalogEntry(
eventType,
products,
Money.of(totalCost, "EUR")));
this.formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<Product, Quantity>(),
Money.of(0, "EUR"));
productMap.clear();
model.addAttribute("productMap", productMap);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog";
}
// Removes a catalog entry
@PostMapping("/catalog/remove")
public String removeEntry(@RequestParam long catalogEntryID) {
Optional<CustomCatalogEntry> entry = catalogEntryRepository.findById(catalogEntryID);
catalogEntryRepository.delete(catalogEntryRepository.findById(catalogEntryID).get());
return "redirect:/catalog";
}
@PostMapping("/catalog_editor/addProduct")
public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number) {
Quantity amount = product.createQuantity(number);
productMap.compute(product,
(key, existingQuantity) -> (existingQuantity == null) ? amount : existingQuantity.add(amount));
formCatalogEntry.addProduct(product, amount);
return "redirect:/catalog_editor";
}
// Removes a product from the catalog_editor
@PostMapping("/catalog_editor/removeProduct")
public String removeProduct(@RequestParam String id, Model model) {
Iterator<Map.Entry<Product, Quantity>> iterator = productMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Product, Quantity> currentProduct = iterator.next();
if (currentProduct.getKey().getId().toString().equals(id)) {
iterator.remove();
}
}
return "redirect:/catalog_editor";
}
@PostMapping("/catalog_editor/chooseEvent")
public String chooseEvent(String eventType) {
try {
formCatalogEntry.setEventType(OrderType.valueOf(eventType));
} catch (IllegalArgumentException e) {
formCatalogEntry.setEventType(OrderType.EVENT_CATERING);
}
productMap.clear();
return "redirect:/catalog_editor";
}
@PostMapping("/catalog/addToCart")
public String addToCart(@RequestParam long catalogEntryID) {
if (cartService.getCart() == null) {
return "redirect:/event";
}
CustomCatalogEntry entry = catalogEntryRepository.findById(catalogEntryID).get();
for (Product product : entry.getProducts().keySet()) {
// Checks if inventory has sufficient Quantity of a Product
Quantity cartQuantity = cartService.getCart().getQuantity(product);
if (entry.getProducts().get(product).add(cartQuantity).isGreaterThan(
inventory.findByProduct(product).get().getQuantity())) {
cartService.getCart().addOrUpdateItem(
product,
inventory.findByProduct(product).get().getQuantity().subtract(cartQuantity));
} else {
cartService.getCart().addOrUpdateItem(
product,
entry.getProducts().get(product));
}
}
cartService.getCart().setOrderType(entry.getEventType());
return "redirect:/event";
}
}