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

View file

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

View file

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

View file

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