Change CustomCatalogEntryRepository to extend CrudRepository

This commit is contained in:
Erik Hohlfeld 2023-11-24 19:14:52 +01:00 committed by Mathis
parent f50fbb5ac4
commit ddf4d7f645
5 changed files with 70 additions and 58 deletions

View file

@ -1,22 +1,24 @@
package catering.orderCatalog;
import catering.order.OrderType;
import org.salespointframework.catalog.Product;
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.MonetaryAmount;
import java.util.*;
@Controller
public class CatalogController {
private CustomCatalogEntryRepository catalogEntryRepository;
private Set<CustomProduct> inventory;
private final CustomCatalogEntryRepository catalogEntryRepository;
private Set<Product> productSet;
private CustomCatalogEntry formCatalogEntry;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) {
inventory = new HashSet<>();
productSet = new HashSet<>();
formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<String, Integer>(),
@ -27,13 +29,13 @@ public class CatalogController {
@GetMapping("/catalog")
public String catalog(Model model) {
model.addAttribute("catalogEntries", catalogEntryRepository.getCatalogEntries());
model.addAttribute("catalogEntries", catalogEntryRepository.findAll());
return "catalog";
}
@GetMapping("/catalog_editor")
public String catalog_configuration(Model model) {
model.addAttribute("inventory", inventory);
model.addAttribute("inventory", productSet);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "catalog_editor";
}
@ -45,11 +47,12 @@ public class CatalogController {
Model model) {
Map<String, Integer> products = new HashMap<String, Integer>();
for (CustomProduct product : inventory) {
products.put(product.getName(), product.getAmount());
for (Product product : productSet) {
// TODO: Map each product to its quantity (Quantity)
products.put(product.getName(), 0);
}
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(
catalogEntryRepository.save(new CustomCatalogEntry(
eventType,
products,
minimumTimePeriod,
@ -59,39 +62,46 @@ public class CatalogController {
new HashMap<String, Integer>(),
0,
0);
inventory.clear();
model.addAttribute("inventory", inventory);
productSet.clear();
model.addAttribute("inventory", productSet);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog";
}
// Removes a catalog entry
/* TODO: reimplement this
@PostMapping("/catalog/remove")
public String removeEntry(@RequestParam int catalogEntryID) {
catalogEntryRepository.removeCatalogEntry(catalogEntryID);
return "redirect:/catalog";
}
*/
@PostMapping("/catalog_editor/product_add")
public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam double cost) {
inventory.add(new CustomProduct(name, amount, cost));
public String product_add(@RequestParam String name, @RequestParam int amount, @RequestParam MonetaryAmount price) {
productSet.add(new Product(name, price));
formCatalogEntry.addProduct(name, amount);
return "redirect:/catalog_editor";
}
// Removes a product from the catalog_editor
/* TODO: reimplement this function based on Product
@PostMapping("/catalog_editor/remove_product")
public String removeProduct(@RequestParam String id, Model model) {
Iterator<CustomProduct> iterator = inventory.iterator();
Iterator<Product> iterator = inventory.iterator();
while (iterator.hasNext()) {
CustomProduct currentProduct = iterator.next();
Product currentProduct = iterator.next();
if (currentProduct.getItemId() == Integer.parseInt(id)) {
// TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use inventory.remove()
if (currentProduct.getId() == Integer.parseInt(id)) {
iterator.remove();
inventory.remove(currentProduct.getId());
currentProduct.getId();
}
}
return "redirect:/catalog_editor";
}
*/
@PostMapping("/catalog_editor/add_time")
public String add_time(@RequestParam int minimumTimePeriod,

View file

@ -1,13 +1,21 @@
package catering.orderCatalog;
import catering.order.OrderType;
import jakarta.persistence.*;
import java.util.Map;
@Entity
//@Table(name = "CatalogEntries")
public class CustomCatalogEntry {
static int idCounter = 0;
private int id;
@Id
@Column(name = "id")
private long id;
private OrderType eventType;
@ElementCollection
private Map<String, Integer> products;
private int minimumTimePeriod; // Declared as int for simplification of the prototype. Only whole hours.
private int totalCost; // Should actually accumulate the price of all items.
@ -21,7 +29,11 @@ public class CustomCatalogEntry {
this.totalCost = totalCost;
}
public int getId() {
public CustomCatalogEntry() {
}
public long getId() {
return id;
}

View file

@ -1,9 +1,12 @@
package catering.orderCatalog;
import catering.order.OrderType;
import org.salespointframework.catalog.Product;
import org.salespointframework.core.DataInitializer;
import org.springframework.stereotype.Component;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import java.util.HashMap;
import java.util.Map;
@ -18,17 +21,27 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer {
@Override
public void initialize() {
Map<String, Integer> products = new HashMap<>();
products.put("Brötchen", 30);
products.put("Kerze", 20);
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.EVENT_CATERING, products, 4, 500));
products = new HashMap<>();
products.put("Kuchen", 3);
products.put("Pizza Funghi", 1);
products.put("Pizza Margherita", 1);
products.put("Pizza Quattro Formaggi", 1);
products.put("Ballon", 20);
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.RENT_A_COOK, products, 2, 1000));
Map<Product, Integer> products = new HashMap<>();
CurrencyUnit currencyUnit = Monetary.getCurrency("EUR");
products.put(new Product("Brötchen", Monetary.getDefaultAmountFactory()
.setCurrency(currencyUnit)
.setNumber(1)
.create()), 30);
products.put(new Product("Kerze", Monetary.getDefaultAmountFactory()
.setCurrency(currencyUnit)
.setNumber(2)
.create()), 30);
// TODO: insert an actual name instead of "new Hashmap<>()"
Map<String, Integer> productsFormatted = new HashMap<String, Integer>();
for (Product product : products.keySet()) {
productsFormatted.put(product.getName(), 5);
}
System.out.println(productsFormatted);
catalogEntryRepository.save(
new CustomCatalogEntry(
OrderType.EVENT_CATERING,
productsFormatted,
4,
500));
}
}

View file

@ -1,32 +1,9 @@
package catering.orderCatalog;
import org.springframework.stereotype.Component;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable;
import java.util.HashSet;
import java.util.Set;
@Component
public class CustomCatalogEntryRepository {
private Set<CustomCatalogEntry> catalogEntries;
public CustomCatalogEntryRepository() {
this.catalogEntries = new HashSet<>();
}
public boolean addCatalogEntry(CustomCatalogEntry catalogEntry) {
return catalogEntries.add(catalogEntry);
}
public boolean removeCatalogEntry(int catalogEntryID) {
for (CustomCatalogEntry catalogEntry : catalogEntries) {
if (catalogEntry.getId() == catalogEntryID) {
return this.catalogEntries.remove(catalogEntry);
}
}
return false;
}
public Set<CustomCatalogEntry> getCatalogEntries() {
return catalogEntries;
}
interface CustomCatalogEntryRepository extends CrudRepository<CustomCatalogEntry, Long> {
@Override
Streamable<CustomCatalogEntry> findAll();
}

View file

@ -25,7 +25,7 @@
<td th:text="${catalogEntry.getMinimumTimePeriod()}">
<td>
<ul th:each="product : ${catalogEntry.getProducts()}">
<li th:text="${product}"/>
<li th:text="${product.getKey().toString()} + ': ' + ${product.getValue().toString()}"/>
</ul>
</td>
<td th:text="${catalogEntry.getTotalCost()}">