X-Git-Url: https://juplo.de/gitweb/?p=demos%2Fspring-boot;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fdemo%2FDemoController.java;h=88ca230c0a9ece86319c2ab5bebf3a3391212d74;hp=ddf4e06a51469b5e297108c3bbeb5043dc736ea2;hb=5a73cdfb59ce1ddc84bf07e80f565ec6066a9821;hpb=ee976412729ec4022d0c2c0e0cf0971d8cd4b579 diff --git a/src/main/java/de/juplo/demo/DemoController.java b/src/main/java/de/juplo/demo/DemoController.java index ddf4e06..88ca230 100644 --- a/src/main/java/de/juplo/demo/DemoController.java +++ b/src/main/java/de/juplo/demo/DemoController.java @@ -2,6 +2,7 @@ package de.juplo.demo; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,56 +23,82 @@ public class DemoController LoggerFactory.getLogger(DemoController.class); - @RequestMapping("/") - public String display( - @ModelAttribute("form") Map> form) + @RequestMapping(path = "/", params = { "!card", "!add", "!remove" }) + public String display(@ModelAttribute Form form) { - for (Integer id : form.keySet()) - for (String entry : form.get(id).keySet()) - LOG.info("{} - {}: {}", id, entry, form.get(id).get(entry)); + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); return "form"; } - @RequestMapping(path = "/", params = "row") - public String addRow( - @ModelAttribute("form") Map> form, - @RequestParam Integer row) + @RequestMapping(path = "/", params = { "card=add", "!add", "!remove" }) + public String addCard(@ModelAttribute Form form) { - LOG.info("Adding row #{}", row); - form.put(row, new HashMap<>()); + Integer next = + form.cards + .keySet() + .stream() + .reduce(0, (a, b) -> a > b ? a : b) + 1; + + Map content = new LinkedHashMap<>(); + content.put(form.names.get(0), Boolean.FALSE); + LOG.info("Adding new card #{} with content: {}", next, content); + form.cards.put(next, content); + + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); + return "form"; } - @RequestMapping(path = "/", params = "row") - public String removeRow( - @ModelAttribute("form") Map> form, - @RequestParam Integer row) + @RequestMapping(path = "/", params = { "card", "!add", "!remove" }) + public String removeCard(@ModelAttribute Form form, @RequestParam Integer card) { - Map content = form.remove(row); - LOG.info("Removed row #{} with content: {}", row, content); + Map content = form.cards.remove(card); + LOG.info("Removed card #{} with content: {}", card, content); + + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); + return "form"; } - @RequestMapping(path = "/", params = { "row", "entry" }) - public String addEntry( - @ModelAttribute("form") Map> form, - @RequestParam Integer row, - @RequestParam String entry) + @RequestMapping(path = "/", params = { "!card", "add", "!remove" }) + public String addRow(@ModelAttribute Form form, @RequestParam Integer add) { - LOG.info("Adding entry {} to row #{}", entry, row); - form.get(row).put(entry, Boolean.FALSE); + LOG.info("Adding row {} to card #{}", form.names.get(add), add); + form.cards.get(add).put(form.names.get(add), Boolean.FALSE); + + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); + return "form"; } - @RequestMapping(path = "/", params = { "row", "entry" }) - public String removeEntry( - @ModelAttribute("form") Map> form, - @RequestParam Integer row, - @RequestParam String entry) + @RequestMapping(path = "/", params = { "!card", "!add", "remove" }) + public String removeRow(@ModelAttribute Form form, @RequestParam String remove) { - Boolean value = form.get(row).remove(entry); - LOG.info("Removed entry {} with value {} from row #{}", entry, value, row); + String[] parts = remove.split(":", 2); + Integer card = Integer.valueOf(parts[0]); + String row = parts[1]; + Boolean value = form.cards.get(card).remove(row); + LOG.info("Removed row {} with value {} from card #{}", row, value, card); + + if (form.cards.get(card).isEmpty()) + { + LOG.info("Card #{} is empty: Removing card!", card); + form.cards.remove(card); + } + + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); + return "form"; } }