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

View file

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

View file

@ -1,9 +1,12 @@
package catering.orderCatalog; package catering.orderCatalog;
import catering.order.OrderType; import catering.order.OrderType;
import org.salespointframework.catalog.Product;
import org.salespointframework.core.DataInitializer; import org.salespointframework.core.DataInitializer;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -18,17 +21,27 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer {
@Override @Override
public void initialize() { public void initialize() {
Map<String, Integer> products = new HashMap<>(); Map<Product, Integer> products = new HashMap<>();
products.put("Brötchen", 30); CurrencyUnit currencyUnit = Monetary.getCurrency("EUR");
products.put("Kerze", 20); products.put(new Product("Brötchen", Monetary.getDefaultAmountFactory()
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.EVENT_CATERING, products, 4, 500)); .setCurrency(currencyUnit)
.setNumber(1)
products = new HashMap<>(); .create()), 30);
products.put("Kuchen", 3); products.put(new Product("Kerze", Monetary.getDefaultAmountFactory()
products.put("Pizza Funghi", 1); .setCurrency(currencyUnit)
products.put("Pizza Margherita", 1); .setNumber(2)
products.put("Pizza Quattro Formaggi", 1); .create()), 30);
products.put("Ballon", 20); // TODO: insert an actual name instead of "new Hashmap<>()"
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(OrderType.RENT_A_COOK, products, 2, 1000)); 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; package catering.orderCatalog;
import org.springframework.stereotype.Component; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable;
import java.util.HashSet; interface CustomCatalogEntryRepository extends CrudRepository<CustomCatalogEntry, Long> {
import java.util.Set; @Override
Streamable<CustomCatalogEntry> findAll();
@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;
}
} }

View file

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