Add inventory based product selection

This commit is contained in:
Erik Hohlfeld 2023-11-25 15:31:07 +01:00 committed by Mathis
parent ddf4d7f645
commit eab8e3fee0
5 changed files with 63 additions and 74 deletions

View file

@ -2,22 +2,28 @@ package catering.orderCatalog;
import catering.order.OrderType;
import org.salespointframework.catalog.Product;
import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem;
import org.salespointframework.quantity.Quantity;
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 javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.util.*;
import java.util.stream.Collectors;
@Controller
public class CatalogController {
private final CustomCatalogEntryRepository catalogEntryRepository;
private Set<Product> productSet;
private CustomCatalogEntry formCatalogEntry;
private final UniqueInventory<UniqueInventoryItem> inventory;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) {
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository, UniqueInventory<UniqueInventoryItem> inventory) {
productSet = new HashSet<>();
formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
@ -25,6 +31,7 @@ public class CatalogController {
0,
0);
this.catalogEntryRepository = catalogEntryRepository;
this.inventory = inventory;
}
@GetMapping("/catalog")
@ -34,14 +41,15 @@ public class CatalogController {
}
@GetMapping("/catalog_editor")
public String catalog_configuration(Model model) {
model.addAttribute("inventory", productSet);
public String editCatalog(Model model) {
model.addAttribute("productSet", productSet);
model.addAttribute("formCatalogEntry", formCatalogEntry);
model.addAttribute("inventory", inventory.findAll().stream().collect(Collectors.toList()));
return "catalog_editor";
}
@PostMapping("/catalog_editor/catalog_add")
public String catalog_add(@RequestParam OrderType eventType,
@PostMapping("/catalog_editor/addCatalogEntry")
public String addCatalogEntry(@RequestParam OrderType eventType,
@RequestParam int minimumTimePeriod,
@RequestParam int totalCost,
Model model) {
@ -63,7 +71,7 @@ public class CatalogController {
0,
0);
productSet.clear();
model.addAttribute("inventory", productSet);
model.addAttribute("productSet", productSet);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog";
}
@ -77,10 +85,13 @@ public class CatalogController {
}
*/
@PostMapping("/catalog_editor/product_add")
public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam MonetaryAmount price) {
productSet.add(new Product(name, price));
formCatalogEntry.addProduct(name, amount);
@PostMapping("/catalog_editor/addProduct")
public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number) {
// TODO: This has to get a price
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
formCatalogEntry.addProduct(product.getName(), number);
return "redirect:/catalog_editor";
}
@ -88,14 +99,14 @@ public class CatalogController {
/* TODO: reimplement this function based on Product
@PostMapping("/catalog_editor/remove_product")
public String removeProduct(@RequestParam String id, Model model) {
Iterator<Product> iterator = inventory.iterator();
Iterator<Product> iterator = productSet.iterator();
while (iterator.hasNext()) {
Product currentProduct = iterator.next();
// TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use inventory.remove()
// TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove()
if (currentProduct.getId() == Integer.parseInt(id)) {
iterator.remove();
inventory.remove(currentProduct.getId());
productSet.remove(currentProduct.getId());
currentProduct.getId();
}
}
@ -103,8 +114,8 @@ public class CatalogController {
}
*/
@PostMapping("/catalog_editor/add_time")
public String add_time(@RequestParam int minimumTimePeriod,
@PostMapping("/catalog_editor/addTime")
public String addTime(@RequestParam int minimumTimePeriod,
@RequestParam OrderType eventType,
@RequestParam Map<String, Integer> products,
Model model) {

View file

@ -6,7 +6,6 @@ import jakarta.persistence.*;
import java.util.Map;
@Entity
//@Table(name = "CatalogEntries")
public class CustomCatalogEntry {
static int idCounter = 0;

View file

@ -1,33 +0,0 @@
package catering.orderCatalog;
public class CustomProduct {
private static int itemIdCounter;
private int itemId;
private String name;
private int amount;
private double cost;
public CustomProduct(String name, int amount, double cost) {
this.itemId = itemIdCounter;
itemIdCounter++;
this.name = name;
this.amount = amount;
this.cost = cost;
}
public String getName() {
return name;
}
public int getAmount() {
return amount;
}
public double getCost() {
return cost;
}
public int getItemId() {
return itemId;
}
}

View file

@ -22,13 +22,13 @@
**BILD**
</td>
<td th:text="${catalogEntry.getEventType()}">
<td th:text="${catalogEntry.getMinimumTimePeriod()}">
<td th:text="${catalogEntry.getMinimumTimePeriod()} + ' h'">
<td>
<ul th:each="product : ${catalogEntry.getProducts()}">
<li th:text="${product.getKey().toString()} + ': ' + ${product.getValue().toString()}"/>
</ul>
</td>
<td th:text="${catalogEntry.getTotalCost()}">
<td th:text="${catalogEntry.getTotalCost()} + ' €'">
<td sec:authorize="hasRole('ADMIN')">
<form method="post" th:action="@{/catalog/remove}">
<input type="hidden" name="catalogEntryID" th:value="${catalogEntry.getId()}">

View file

@ -22,7 +22,7 @@
Eventbasispreis: 500€
<h2>Mindestzeitraum</h2>
<form th:object="${formCatalogEntry}" method="post" th:action="@{/catalog_editor/add_time}">
<form th:object="${formCatalogEntry}" method="post" th:action="@{/catalog_editor/addTime}">
<label class="form-label">Mindestzeitraum:</label>
<input type="hidden" th:field="*{eventType}">
<input class="form-control w-auto d-inline-block" th:field="*{minimumTimePeriod}" type="number" min="0" step="1" value="1"/>
@ -36,34 +36,46 @@
<th>Menge</th>
<th>Stückpreis</th>
</tr>
<tr th:each="item : ${inventory}">
<tr th:each="item : ${productSet}">
<td th:text="${item.getName()}">
<td th:text="${item.getAmount()}">
<td th:text="${item.getCost()}">
<td th:text="${item.getPrice()}">
<td>
<form method="post" th:action="@{/catalog_editor/remove_product}">
<input class="form-control w-auto d-inline-block" type="hidden" name="id" th:value="${item.getItemId()}">
<input class="form-control w-auto d-inline-block" type="hidden" name="id" th:value="${item.getId()}">
<button class="btn btn-danger" type="submit">Entfernen</button>
</form>
</td>
</tr>
</table>
<form method="post" th:action="@{/catalog_editor/product_add}">
<label class="form-label" for="name">Produktname</label>
<input class="form-control w-auto d-inline-block" type="text" id="name" name="name" required>
<label class="form-label" for="amount">Menge</label>
<input class="form-control w-auto d-inline-block" type="number" id="amount" name="amount" min="1" required>
<input type="hidden" name="cost" value="50.0">
<button class="btn btn-primary" type="submit">Artikel hinzufügen</button>
<h2>Produktauswahl</h2>
<table class="table">
<tr>
<th>Produktname</th>
<th>Kosten</th>
<th>Verfügbar</th>
<th>Menge</th>
</tr>
<tr th:each="item : ${inventory}">
<td th:text="${item.getProduct().getName()}">Name</td>
<td th:text="${item.getProduct().getPrice()}">Preis</td>
<td th:text="${item.getQuantity()}">Verfügbar</td>
<td>
<form th:action="@{/catalog_editor/addProduct}" method="post">
<input id="number" type="number" name="number" min="1" value="1"/>
<input type="hidden" name="pid" th:value="${item.getProduct().getId()}"/>
<input class="btn btn-primary" type="submit" th:value="Hinzufügen"/>
</form>
</td>
</tr>
</table>
<br>
<h3>Momentane Auswahl</h3>
Eventtyp: <span th:text="${formCatalogEntry.getEventType()}"></span><br>
Mindestzeitraum: <span th:text="${formCatalogEntry.getMinimumTimePeriod()}"></span><br>
<span>Gesamtpreis: 90 €</span>
<form method="post" th:action="@{/catalog_editor/catalog_add}" th:object="${formCatalogEntry}">
Mindestzeitraum: <span th:text="${formCatalogEntry.getMinimumTimePeriod()} + ' h'"></span><br>
<span th:text="'Gesamtpreis: ' + ${formCatalogEntry.getTotalCost()} + ' €'">Gesamtpreis</span>
<form method="post" th:action="@{/catalog_editor/addCatalogEntry}" th:object="${formCatalogEntry}">
<input type="hidden" name="eventType" th:value="${formCatalogEntry.getEventType()}">
<input type="hidden" name="products" th:value="${inventory}">
<input type="hidden" name="products" th:value="${productSet}">
<input type="hidden" name="minimumTimePeriod" th:value="${formCatalogEntry.getMinimumTimePeriod}">
<input type="hidden" name="totalCost" th:value="${formCatalogEntry.getTotalCost()}">
<button class="btn btn-primary" type="submit">Zum Katalog hinzufügen</button>