Add functionality for adding products to an event

This commit is contained in:
Mathis Kral 2023-11-07 20:53:31 +01:00 committed by Simon Bruder
parent 15f47a04b3
commit 8eb8281e5c
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
6 changed files with 104 additions and 18 deletions

View file

@ -0,0 +1,39 @@
package catering.order;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class CustomCart {
private CustomOrder.OrderType orderType;
private Map<String, Integer> products;
public CustomCart() {
this.orderType = CustomOrder.OrderType.SOMETHING_ELSE;
this.products = new HashMap<>();
}
public CustomOrder.OrderType getOrderType() {
return orderType;
}
public void setOrderType(CustomOrder.OrderType type) {
this.orderType = type;
return;
}
public Map<String, Integer> getProucts() {
return products;
}
public void addProduct(String product, int number) {
products.put(product, number);
}
public boolean removeProduct(String product) {
return products.remove(product) != null;
}
}

View file

@ -32,7 +32,7 @@ public class CustomOrder {
this.products = products;
this.invoiceAvailable = invoiceAvailable;
this.totalCost = totalCost;
this.formatter = DateTimeFormatter.ofPattern("MM.dd.yyy, HH:mm 'Uhr'");
this.formatter = DateTimeFormatter.ofPattern("dd.MM.yyy, HH:mm 'Uhr'");
}
public int getId() {

View file

@ -2,9 +2,7 @@ package catering.order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@ -12,9 +10,11 @@ import java.util.ArrayList;
public class OrderController {
private final CustomOrderRepository orderRepository;
private final CustomCart cart;
public OrderController(CustomOrderRepository orderRepository) {
public OrderController(CustomOrderRepository orderRepository, CustomCart cart) {
this.orderRepository = orderRepository;
this.cart = cart;
}
@GetMapping("/orders")
@ -25,8 +25,11 @@ public class OrderController {
}
@GetMapping("/event")
public String event() {
return "eventPlanner";
public String event(Model model) {
model.addAttribute("items", cart.getProucts());
model.addAttribute("type", cart.getOrderType());
model.addAttribute("productForm", new ProductForm());
return "event";
}
@PostMapping("/orders/remove")
@ -34,4 +37,14 @@ public class OrderController {
orderRepository.removeOrder(orderID);
return "redirect:/orders";
}
@PostMapping("/event/addProduct")
public String addProduct(@ModelAttribute ProductForm product, Model model) {
cart.addProduct(product.getProduct(), product.getNumber());
model.addAttribute("items", cart.getProucts());
model.addAttribute("type", cart.getOrderType());
model.addAttribute("productForm", new ProductForm());
return "redirect:/event";
}
}

View file

@ -0,0 +1,24 @@
package catering.order;
import org.springframework.web.bind.annotation.ModelAttribute;
public class ProductForm {
private String product;
private int number;
public String getProduct() {
return product;
}
public int getNumber() {
return number;
}
public void setProduct(String product) {
this.product = product;
}
public void setNumber(int number) {
this.number = number;
}
}

View file

@ -31,8 +31,4 @@ public class WelcomeController {
return "catalog";
}
@GetMapping("/event_configuration")
public String event_configuration() {
return "event_configuration";
}
}

View file

@ -18,21 +18,35 @@
<h1>Eventplaner (planen Sie Ihr Event)</h1>
<span th:text="'Typ: ' + ${type}"/>
<table style="width:100%; text-align:left">
<tr>
<td>Eventtyp</td>
<td>SISHI_NIGHT</td>
</tr>
<tr>
<th>Produkt</th>
<th>Anzahl</th>
</tr>
<tr>
<td>Sake Nigiri</td>
<td>200</td>
<tr th:each="key : ${items.keySet()}">
<td th:text="${key}">Sake Nigiri</td>
<td th:text="${items.get(key)}">200</td>
</tr>
</table>
<h4>Product hinzufügen</h4>
<form th:action="@{/event/addProduct}" th:object="${productForm}" method="post">
<div>
<label>Produkt</label>
<input type="text" th:field="*{product}" placeholder="Produktname" required/>
</div>
<div>
<label>Anzahl</label>
<input type="number" th:field="*{number}" placeholder="Anzahl" min="1" required/>
</div>
<input type="submit" value="Submit"/>
</form>
</body>
<footer style="bottom: 0; position: absolute;">