Base orderCatalog fully on salespoint

This commit is contained in:
Erik Hohlfeld 2023-11-25 22:56:05 +01:00 committed by Mathis
parent 8cc44c8121
commit 795ccc31d2
4 changed files with 33 additions and 30 deletions

View file

@ -17,12 +17,12 @@ import java.util.stream.Collectors;
@Controller
public class CatalogController {
private final CustomCatalogEntryRepository catalogEntryRepository;
private Set<Product> productSet;
private Map<Product, Quantity> productMap;
private CustomCatalogEntry formCatalogEntry;
private final UniqueInventory<UniqueInventoryItem> inventory;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository, UniqueInventory<UniqueInventoryItem> inventory) {
productSet = new HashSet<>();
productMap = new HashMap<>();
formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<String, Quantity>(),
@ -39,8 +39,8 @@ public class CatalogController {
}
@GetMapping("/catalog_editor")
public String editCatalog(Model model) {
model.addAttribute("productSet", productSet);
public String editCatalog(Model model) {;
model.addAttribute("productMap", productMap);
model.addAttribute("formCatalogEntry", formCatalogEntry);
model.addAttribute("inventory", inventory.findAll().stream().collect(Collectors.toList()));
return "catalog_editor";
@ -48,14 +48,13 @@ public class CatalogController {
@PostMapping("/catalog_editor/addCatalogEntry")
public String addCatalogEntry(@RequestParam OrderType eventType,
@RequestParam int minimumTimePeriod,
@RequestParam int totalCost,
Model model) {
@RequestParam int minimumTimePeriod,
@RequestParam int totalCost,
Model model) {
Map<String, Quantity> products = new HashMap<String, Quantity>();
for (Product product : productSet) {
// TODO: Map each product to its quantity (Quantity)
products.put(product.getName(), Quantity.of(3));
for (Product product : productMap.keySet()) {
products.put(product.getName(), productMap.get(product));
}
catalogEntryRepository.save(new CustomCatalogEntry(
@ -68,8 +67,8 @@ public class CatalogController {
new HashMap<String, Quantity>(),
0,
0);
productSet.clear();
model.addAttribute("productSet", productSet);
productMap.clear();
model.addAttribute("productMap", productMap);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog";
}
@ -79,16 +78,14 @@ public class CatalogController {
public String removeEntry(@RequestParam long catalogEntryID) {
Optional<CustomCatalogEntry> entry = catalogEntryRepository.findById(catalogEntryID);
catalogEntryRepository.delete(catalogEntryRepository.findById(catalogEntryID).get());
System.out.println(catalogEntryID);
// TODO: use catalogEntryRepository.findById(); to find the object
return "redirect:/catalog";
}
@PostMapping("/catalog_editor/addProduct")
public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number) {
Quantity amount = Quantity.of(number);
productSet.add(new Product(product.getName(), product.getPrice()));
// TODO: formCatalogEntry.addProduct has to use Quantity amount instead of int number
productMap.compute(product, (key, existingQuantity) ->
(existingQuantity == null) ? amount : existingQuantity.add(amount));
formCatalogEntry.addProduct(product.getName(), amount);
return "redirect:/catalog_editor";
}
@ -96,12 +93,11 @@ public class CatalogController {
// Removes a product from the catalog_editor
@PostMapping("/catalog_editor/removeProduct")
public String removeProduct(@RequestParam String id, Model model) {
Iterator<Product> iterator = productSet.iterator();
while (iterator.hasNext()) {
Product currentProduct = iterator.next();
// TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove()
if (currentProduct.getId().toString().equals(id)) {
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();
}
}
@ -110,9 +106,9 @@ public class CatalogController {
@PostMapping("/catalog_editor/addTime")
public String addTime(@RequestParam int minimumTimePeriod,
@RequestParam OrderType eventType,
@RequestParam Map<String, Integer> products,
Model model) {
@RequestParam OrderType eventType,
@RequestParam Map<String, Integer> products,
Model model) {
formCatalogEntry.setMinimumTimePeriod(minimumTimePeriod);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog_editor";

View file

@ -4,6 +4,7 @@ import catering.order.OrderType;
import jakarta.persistence.*;
import org.salespointframework.quantity.Quantity;
import java.util.Iterator;
import java.util.Map;
@Entity
@ -47,6 +48,12 @@ public class CustomCatalogEntry {
}
public int getTotalCost() {
totalCost = 0;
Iterator<Quantity> iterator = products.values().iterator();
while (iterator.hasNext()) {
Quantity currentQuantity = iterator.next();
totalCost += currentQuantity.getAmount().intValue();
}
return totalCost;
}

View file

@ -22,22 +22,21 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer {
@Override
public void initialize() {
Map<Product, Integer> products = new HashMap<>();
Map<Product, Quantity> products = new HashMap<>();
CurrencyUnit currencyUnit = Monetary.getCurrency("EUR");
products.put(new Product("Brötchen", Monetary.getDefaultAmountFactory()
.setCurrency(currencyUnit)
.setNumber(1)
.create()), 30);
.create()), Quantity.of(30));
products.put(new Product("Kerze", Monetary.getDefaultAmountFactory()
.setCurrency(currencyUnit)
.setNumber(2)
.create()), 30);
.create()), Quantity.of(20));
Map<String, Quantity> productsFormatted = new HashMap<String, Quantity>();
for (Product product : products.keySet()) {
productsFormatted.put(product.getName(), Quantity.of(5));
productsFormatted.put(product.getName(), products.get(product));
}
System.out.println(productsFormatted);
catalogEntryRepository.save(
new CustomCatalogEntry(
OrderType.EVENT_CATERING,

View file

@ -36,8 +36,9 @@
<th>Menge</th>
<th>Stückpreis</th>
</tr>
<tr th:each="item : ${productSet}">
<tr th:each="item : ${productMap.keySet()}">
<td th:text="${item.getName()}">
<td th:text="${productMap.get(item)}">
<td th:text="${item.getPrice()}">
<td>
<form method="post" th:action="@{/catalog_editor/removeProduct}">