WIP
[demos/spring-boot] / src / main / java / de / juplo / demo / DemoController.java
1 package de.juplo.demo;
2
3
4 import java.util.HashMap;
5 import java.util.Map;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.ModelAttribute;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestParam;
12
13
14 /**
15  *
16  * @author Kai Moritz
17  */
18 @Controller
19 public class DemoController
20 {
21   private static final Logger LOG =
22       LoggerFactory.getLogger(DemoController.class);
23
24
25   @RequestMapping("/")
26   public String display(@ModelAttribute Form form)
27   {
28     for (Integer id : form.cards.keySet())
29       for (String entry : form.cards.get(id).keySet())
30         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
31
32     return "form";
33   }
34
35   @RequestMapping(path = "/", params = "add=card")
36   public String addCard(@ModelAttribute Form form)
37   {
38     Integer next =
39         form.cards
40             .keySet()
41             .stream()
42             .reduce(0, (a, b) -> a > b ? a : b) + 1;
43
44     LOG.info("Adding new card #{}", next);
45     form.cards.put(next, new HashMap<>());
46     return "form";
47   }
48
49   @RequestMapping(path = "/", params = "remove=card")
50   public String removeCard(
51       @ModelAttribute Form form,
52       @RequestParam Integer card)
53   {
54     Map<String, Boolean> content = form.cards.remove(card);
55     LOG.info("Removed card #{} with content: {}", card, content);
56     return "form";
57   }
58
59   @RequestMapping(path = "/", params = "add=row")
60   public String addRow(@ModelAttribute Form form, @RequestParam Integer card)
61   {
62     LOG.info("Adding row {} to card #{}", form.row.get(card), card);
63     form.cards.get(card).put(form.row.get(card), Boolean.FALSE);
64     return "form";
65   }
66
67   @RequestMapping(path = "/", params = "remove!=card")
68   public String removeRow(@ModelAttribute Form form, @RequestParam String remove)
69   {
70     String[] parts = remove.split(":", 2);
71     Integer card = Integer.valueOf(parts[0]);
72     String row = parts[1];
73     Boolean value = form.cards.get(card).remove(row);
74     LOG.info("Removed row {} with value {} from card #{}", row, value, card);
75     return "form";
76   }
77 }