Add functionality to add to the catalog with the catalog_editor

This commit is contained in:
Erik Hohlfeld 2023-11-12 00:46:47 +01:00 committed by Simon Bruder
parent e7a722b6f8
commit 24e4ce63dc
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
4 changed files with 99 additions and 44 deletions

View file

@ -2,23 +2,25 @@ package catering.orderCatalog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.*;
@Controller
public class CatalogController {
private CustomCatalogEntryRepository catalogEntryRepository;
private Set<CustomProduct> inventory;
private CustomCatalogEntry formCatalogEntry;
public CatalogController(CustomCatalogEntryRepository catalogEntryRepository) {
inventory = new HashSet<>();
formCatalogEntry = new CustomCatalogEntry(
CustomCatalogEntry.EventType.EVENT_CATERING,
new HashMap<String, Integer>(),
0,
0);
this.catalogEntryRepository = catalogEntryRepository;
}
@ -31,23 +33,35 @@ public class CatalogController {
@GetMapping("/catalog_editor")
public String catalog_configuration(Model model) {
model.addAttribute("inventory", inventory);
model.addAttribute("formCatalogEntry", new CustomCatalogEntry(
CustomCatalogEntry.EventType.EVENT_CATERING,
new HashMap<String, Integer>(),
0,
0));
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "catalog_editor";
}
@PostMapping("/catalog_editor/catalog_add")
public String catalog_add(Model model) {
public String catalog_add(@RequestParam CustomCatalogEntry.EventType eventType,
@RequestParam int minimumTimePeriod,
@RequestParam int totalCost,
Model model) {
Map<String, Integer> products = new HashMap<String, Integer>();
for (CustomProduct product : inventory) {
products.put(product.getName(), product.getAmount());
}
System.out.println("Products looks like this:" + products);
catalogEntryRepository.addCatalogEntry(new CustomCatalogEntry(
// TODO: Fill with formCatalogEntry's data instead of dummy data
eventType,
products,
minimumTimePeriod,
totalCost));
this.formCatalogEntry = new CustomCatalogEntry(
CustomCatalogEntry.EventType.EVENT_CATERING,
new HashMap<String, Integer>(),
0,
0
));
0);
inventory.clear();
model.addAttribute("inventory", inventory);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog";
}
@ -61,6 +75,7 @@ public class CatalogController {
@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));
formCatalogEntry.addProduct(name, amount);
return "redirect:/catalog_editor";
}
@ -79,11 +94,35 @@ public class CatalogController {
}
@PostMapping("/catalog_editor/add_time")
public String add_time(@RequestParam int minimumTimePeriod, @ModelAttribute CustomCatalogEntry formCatalogEntry, Model model) {
System.out.println(minimumTimePeriod);
/*System.out.println(formCatalogEntry.getMinimumTimePeriod());
model.addAttribute("formCatalogEntry", formCatalogEntry);*/
// TODO: Might not work because on every GetMapping of /catalog_editor a new formCatalogEntry is created
public String add_time(@RequestParam int minimumTimePeriod,
@RequestParam CustomCatalogEntry.EventType eventType,
@RequestParam Map<String, Integer> products,
Model model) {
formCatalogEntry.setMinimumTimePeriod(minimumTimePeriod);
model.addAttribute("formCatalogEntry", formCatalogEntry);
return "redirect:/catalog_editor";
}
@PostMapping("/catalog_editor/chooseEvent")
public String chooseEvent(String events) {
switch (events) {
case "event_catering":
formCatalogEntry.setEventType(CustomCatalogEntry.EventType.EVENT_CATERING);
break;
case "party_service":
formCatalogEntry.setEventType(CustomCatalogEntry.EventType.PARTY_SERVICE);
break;
case "mobile_breakfast":
formCatalogEntry.setEventType(CustomCatalogEntry.EventType.MOBILE_BREAKFAST);
break;
case "rent_a_cook":
formCatalogEntry.setEventType(CustomCatalogEntry.EventType.RENT_A_COOK);
break;
default:
formCatalogEntry.setEventType(CustomCatalogEntry.EventType.EVENT_CATERING);
break;
}
return "redirect:/catalog_editor";
}

View file

@ -1,6 +1,5 @@
package catering.orderCatalog;
import java.util.HashMap;
import java.util.Map;
public class CustomCatalogEntry {
@ -36,14 +35,18 @@ public class CustomCatalogEntry {
return minimumTimePeriod;
}
public String getTotalCost() {
return totalCost + "";
public int getTotalCost() {
return totalCost;
}
public void setEventType(EventType eventType) {
this.eventType = eventType;
}
public void addProduct(String name, Integer count) {
this.products.put(name, count);
}
public void setProducts(Map<String, Integer> products) {
this.products = products;
}

View file

@ -33,7 +33,6 @@
<td>
**BILD**
</td>
<td th:text="${catalogEntry.getId()}">
<td th:text="${catalogEntry.getEventType()}">
<td th:text="${catalogEntry.getMinimumTimePeriod()}">h
<td>
@ -70,6 +69,12 @@
</form>
</div>
<div>
<form th:action="@{/catalog_editor}">
<button type="submit">Hinzufügen</button>
</form>
</div>
<div>
<button type="button">Hinzufügen</button>
</div>

View file

@ -19,25 +19,26 @@
<input type="hidden" name="addCatalog">
<div class="content">
<h2>Dienstleistung</h2>
<label for="dienstleistungen"></label>
<select name="dienstleistungen" id="dienstleistungen">
<option value="eventcatering">Eventcatering</option>
<option value="partyservice">Partyservice</option>
<label for="events"></label>
<form th:object="${formCatalogEntry}" method="post" th:action="@{/catalog_editor/chooseEvent}">
<select name="events" id="events">
<option value="event_catering">Eventcatering</option>
<option value="party_service">Partyservice</option>
<option value="mobile_breakfast">Mobile Breakfast</option>
<option value="rent-a-cook">Rent-a-Cook</option>
<option value="rent_a_cook">Rent-a-Cook</option>
</select>
<button type="submit">Event auswählen</button>
</form>
Eventbasispreis: 500€
<h2>Mindestzeitraum</h2>
<form th:object="${formCatalogEntry}" method="post" th:action="@{/catalog_editor/add_time}">
<label>Mindestzeitraum:</label>
<input type="hidden" th:field="*{eventType}">
<input th:field="*{minimumTimePeriod}" type="number" min="0" step="1" value="1"/>
<button type="submit">Zum Model hinzufügen</button>
</form>
<span th:text="${formCatalogEntry.minimumTimePeriod}"></span>
<span th:text="${formCatalogEntry}"></span>
<h2>Basisausstattung</h2>
<table>
<tr>
@ -65,10 +66,17 @@
<input type="number" id="amount" name="amount" min="1" required>
<input type="hidden" name="cost" value="50.0">
<button type="submit">Artikel hinzufügen</button>
</form>
</form>hinzufügen
<br>
Gesamtpreis: 90 €<br>
<h3>Momentane Auswahl</h3>
Eventtyp: <span th:text="${formCatalogEntry.getEventType()}"></span><br>
Mindestzeitraum: <span th:text="${formCatalogEntry.getMinimumTimePeriod()}"></span><br>
<span>Gesamtpreis: 90 €</span>
<form method="post" th:action="@{/catalog_editor/catalog_add}" th:object="${formCatalogEntry}">
<input type="hidden" name="eventType" th:value="${formCatalogEntry.getEventType()}">
<input type="hidden" name="products" th:value="${inventory}">
<input type="hidden" name="minimumTimePeriod" th:value="${formCatalogEntry.getMinimumTimePeriod}">
<input type="hidden" name="totalCost" th:value="${formCatalogEntry.getTotalCost()}">
<button type="submit">Zum Katalog hinzufügen</button>
</form>
</div>