Options can be removed from the map
[demos/spring-boot] / src / main / java / de / juplo / demo / DemoController.java
index 3f8d0dd..a9391db 100644 (file)
@@ -1,10 +1,13 @@
 package de.juplo.demo;
 
 
+import java.util.stream.Collectors;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.thymeleaf.util.StringUtils;
 
 
 /**
@@ -18,7 +21,38 @@ public class DemoController
   @RequestMapping("/")
   public String display(@ModelAttribute Form form)
   {
-    log.info("option={}, inner={}", form.option, form.inner.option);
+    log.info(
+        "option={}, inner={}{}",
+        form.option,
+        form.inner.option,
+        form.map
+            .entrySet()
+            .stream()
+            .map(entry -> entry.getKey() + "=" + entry.getValue())
+            .collect(Collectors.joining(", ", ", ", "")));
     return "form";
   }
+
+  @RequestMapping(path = "/", params = "add")
+  public String add(@ModelAttribute Form form, @RequestParam String name)
+  {
+    if (!StringUtils.isEmptyOrWhitespace(name))
+    {
+      form.map.put(name.trim(), Boolean.FALSE);
+      log.info("Added option \"{}\" to the map", name.trim());
+    }
+    else
+    {
+      log.info("Ignoring empty option-name");
+    }
+    return display(form);
+  }
+
+  @RequestMapping(path = "/", params = "remove")
+  public String remove(@ModelAttribute Form form, @RequestParam String remove)
+  {
+    Boolean value = form.map.remove(remove);
+    log.info("Removed option \"{}\" with value {} from the map", remove, value);
+    return display(form);
+  }
 }