Fix the remove methods to work again

This commit is contained in:
Erik Hohlfeld 2023-11-25 21:20:11 +01:00 committed by Mathis
parent eab8e3fee0
commit 8cc44c8121
4 changed files with 25 additions and 32 deletions

View file

@ -11,8 +11,6 @@ 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.Monetary;
import javax.money.MonetaryAmount;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -27,7 +25,7 @@ public class CatalogController {
productSet = new HashSet<>(); productSet = new HashSet<>();
formCatalogEntry = new CustomCatalogEntry( formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING, OrderType.EVENT_CATERING,
new HashMap<String, Integer>(), new HashMap<String, Quantity>(),
0, 0,
0); 0);
this.catalogEntryRepository = catalogEntryRepository; this.catalogEntryRepository = catalogEntryRepository;
@ -53,11 +51,11 @@ public class CatalogController {
@RequestParam int minimumTimePeriod, @RequestParam int minimumTimePeriod,
@RequestParam int totalCost, @RequestParam int totalCost,
Model model) { Model model) {
Map<String, Integer> products = new HashMap<String, Integer>(); Map<String, Quantity> products = new HashMap<String, Quantity>();
for (Product product : productSet) { for (Product product : productSet) {
// TODO: Map each product to its quantity (Quantity) // TODO: Map each product to its quantity (Quantity)
products.put(product.getName(), 0); products.put(product.getName(), Quantity.of(3));
} }
catalogEntryRepository.save(new CustomCatalogEntry( catalogEntryRepository.save(new CustomCatalogEntry(
@ -67,7 +65,7 @@ public class CatalogController {
totalCost)); totalCost));
this.formCatalogEntry = new CustomCatalogEntry( this.formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING, OrderType.EVENT_CATERING,
new HashMap<String, Integer>(), new HashMap<String, Quantity>(),
0, 0,
0); 0);
productSet.clear(); productSet.clear();
@ -77,42 +75,38 @@ public class CatalogController {
} }
// 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 long catalogEntryID) {
catalogEntryRepository.removeCatalogEntry(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"; 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) {
// TODO: This has to get a price
Quantity amount = Quantity.of(number); Quantity amount = Quantity.of(number);
productSet.add(new Product(product.getName(), product.getPrice())); productSet.add(new Product(product.getName(), product.getPrice()));
// TODO: formCatalogEntry.addProduct has to use Quantity amount instead of int number // TODO: formCatalogEntry.addProduct has to use Quantity amount instead of int number
formCatalogEntry.addProduct(product.getName(), number); formCatalogEntry.addProduct(product.getName(), 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/removeProduct")
@PostMapping("/catalog_editor/remove_product")
public String removeProduct(@RequestParam String id, Model model) { public String removeProduct(@RequestParam String id, Model model) {
Iterator<Product> iterator = productSet.iterator(); Iterator<Product> iterator = productSet.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Product currentProduct = iterator.next(); Product currentProduct = iterator.next();
// TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove() // TODO: Have a look at salespointframework.catalog.Product.ProductIdentifier, maybe use productSet.remove()
if (currentProduct.getId() == Integer.parseInt(id)) { if (currentProduct.getId().toString().equals(id)) {
iterator.remove(); iterator.remove();
productSet.remove(currentProduct.getId());
currentProduct.getId();
} }
} }
return "redirect:/catalog_editor"; return "redirect:/catalog_editor";
} }
*/
@PostMapping("/catalog_editor/addTime") @PostMapping("/catalog_editor/addTime")
public String addTime(@RequestParam int minimumTimePeriod, public String addTime(@RequestParam int minimumTimePeriod,

View file

@ -2,26 +2,24 @@ package catering.orderCatalog;
import catering.order.OrderType; import catering.order.OrderType;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.salespointframework.quantity.Quantity;
import java.util.Map; import java.util.Map;
@Entity @Entity
public class CustomCatalogEntry { public class CustomCatalogEntry {
static int idCounter = 0;
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") @Column(name = "id")
private long id; private Long id;
private OrderType eventType; private OrderType eventType;
@ElementCollection @ElementCollection
private Map<String, Integer> products; private Map<String, Quantity> 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.
public CustomCatalogEntry(OrderType eventType, Map<String, Integer> products, int minimumTimePeriod, int totalCost) { public CustomCatalogEntry(OrderType eventType, Map<String, Quantity> products, int minimumTimePeriod, int totalCost) {
this.id = idCounter;
idCounter++;
this.eventType = eventType; this.eventType = eventType;
this.products = products; this.products = products;
this.minimumTimePeriod = minimumTimePeriod; this.minimumTimePeriod = minimumTimePeriod;
@ -40,7 +38,7 @@ public class CustomCatalogEntry {
return eventType; return eventType;
} }
public Map<String, Integer> getProducts() { public Map<String, Quantity> getProducts() {
return products; return products;
} }
@ -56,11 +54,11 @@ public class CustomCatalogEntry {
this.eventType = eventType; this.eventType = eventType;
} }
public void addProduct(String name, Integer count) { public void addProduct(String name, Quantity count) {
this.products.put(name, count); this.products.put(name, count);
} }
public void setProducts(Map<String, Integer> products) { public void setProducts(Map<String, Quantity> products) {
this.products = products; this.products = products;
} }
public void setMinimumTimePeriod(int timePeriod) { public void setMinimumTimePeriod(int timePeriod) {

View file

@ -3,6 +3,7 @@ package catering.orderCatalog;
import catering.order.OrderType; import catering.order.OrderType;
import org.salespointframework.catalog.Product; import org.salespointframework.catalog.Product;
import org.salespointframework.core.DataInitializer; import org.salespointframework.core.DataInitializer;
import org.salespointframework.quantity.Quantity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.money.CurrencyUnit; import javax.money.CurrencyUnit;
@ -31,10 +32,10 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer {
.setCurrency(currencyUnit) .setCurrency(currencyUnit)
.setNumber(2) .setNumber(2)
.create()), 30); .create()), 30);
// TODO: insert an actual name instead of "new Hashmap<>()"
Map<String, Integer> productsFormatted = new HashMap<String, Integer>(); Map<String, Quantity> productsFormatted = new HashMap<String, Quantity>();
for (Product product : products.keySet()) { for (Product product : products.keySet()) {
productsFormatted.put(product.getName(), 5); productsFormatted.put(product.getName(), Quantity.of(5));
} }
System.out.println(productsFormatted); System.out.println(productsFormatted);
catalogEntryRepository.save( catalogEntryRepository.save(

View file

@ -40,7 +40,7 @@
<td th:text="${item.getName()}"> <td th:text="${item.getName()}">
<td th:text="${item.getPrice()}"> <td th:text="${item.getPrice()}">
<td> <td>
<form method="post" th:action="@{/catalog_editor/remove_product}"> <form method="post" th:action="@{/catalog_editor/removeProduct}">
<input class="form-control w-auto d-inline-block" type="hidden" name="id" th:value="${item.getId()}"> <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> <button class="btn btn-danger" type="submit">Entfernen</button>
</form> </form>