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.web.bind.annotation.RequestParam;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.util.*;
import java.util.stream.Collectors;
@ -27,7 +25,7 @@ public class CatalogController {
productSet = new HashSet<>();
formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<String, Integer>(),
new HashMap<String, Quantity>(),
0,
0);
this.catalogEntryRepository = catalogEntryRepository;
@ -53,11 +51,11 @@ public class CatalogController {
@RequestParam int minimumTimePeriod,
@RequestParam int totalCost,
Model model) {
Map<String, Integer> products = new HashMap<String, Integer>();
Map<String, Quantity> products = new HashMap<String, Quantity>();
for (Product product : productSet) {
// TODO: Map each product to its quantity (Quantity)
products.put(product.getName(), 0);
products.put(product.getName(), Quantity.of(3));
}
catalogEntryRepository.save(new CustomCatalogEntry(
@ -67,7 +65,7 @@ public class CatalogController {
totalCost));
this.formCatalogEntry = new CustomCatalogEntry(
OrderType.EVENT_CATERING,
new HashMap<String, Integer>(),
new HashMap<String, Quantity>(),
0,
0);
productSet.clear();
@ -77,42 +75,38 @@ public class CatalogController {
}
// Removes a catalog entry
/* TODO: reimplement this
@PostMapping("/catalog/remove")
public String removeEntry(@RequestParam int catalogEntryID) {
catalogEntryRepository.removeCatalogEntry(catalogEntryID);
public String removeEntry(@RequestParam long 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";
}
*/
@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);
formCatalogEntry.addProduct(product.getName(), amount);
return "redirect:/catalog_editor";
}
// Removes a product from the catalog_editor
/* TODO: reimplement this function based on Product
@PostMapping("/catalog_editor/remove_product")
@PostMapping("/catalog_editor/removeProduct")
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()
if (currentProduct.getId() == Integer.parseInt(id)) {
if (currentProduct.getId().toString().equals(id)) {
iterator.remove();
productSet.remove(currentProduct.getId());
currentProduct.getId();
}
}
return "redirect:/catalog_editor";
}
*/
@PostMapping("/catalog_editor/addTime")
public String addTime(@RequestParam int minimumTimePeriod,

View file

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

View file

@ -3,6 +3,7 @@ package catering.orderCatalog;
import catering.order.OrderType;
import org.salespointframework.catalog.Product;
import org.salespointframework.core.DataInitializer;
import org.salespointframework.quantity.Quantity;
import org.springframework.stereotype.Component;
import javax.money.CurrencyUnit;
@ -31,10 +32,10 @@ public class CustomCatalogEntryDataInitializer implements DataInitializer {
.setCurrency(currencyUnit)
.setNumber(2)
.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()) {
productsFormatted.put(product.getName(), 5);
productsFormatted.put(product.getName(), Quantity.of(5));
}
System.out.println(productsFormatted);
catalogEntryRepository.save(

View file

@ -40,7 +40,7 @@
<td th:text="${item.getName()}">
<td th:text="${item.getPrice()}">
<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()}">
<button class="btn btn-danger" type="submit">Entfernen</button>
</form>