Die Ergebnisse werden gespeichert und sind via REST abrufbar
[demos/kafka/training] / src / main / java / de / juplo / kafka / DriverController.java
index ed38080..26a5bc8 100644 (file)
@@ -2,14 +2,14 @@ package de.juplo.kafka;
 
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
 
 
 @RestController
@@ -17,6 +17,8 @@ import java.util.concurrent.ExecutionException;
 public class DriverController
 {
   private final EndlessConsumer consumer;
+  private final ApplicationRecordHandler recordHandler;
+  private final AdderResults results;
 
 
   @PostMapping("start")
@@ -32,10 +34,49 @@ public class DriverController
   }
 
 
-  @GetMapping("seen")
-  public Map<Integer, Map<String, Long>> seen()
+  @GetMapping("state")
+  public Map<Integer, Map<String, AdderResult>> state()
   {
-    return consumer.getSeen();
+    return
+        recordHandler
+            .getState()
+            .entrySet()
+            .stream()
+            .collect(Collectors.toMap(
+                entry -> entry.getKey(),
+                entry -> entry.getValue().getState()));
+  }
+
+  @GetMapping("state/{user}")
+  public ResponseEntity<Long> state(@PathVariable String user)
+  {
+    for (AdderBusinessLogic adder : recordHandler.getState().values())
+    {
+      Optional<Long> sum = adder.getSum(user);
+      if (sum.isPresent())
+        return ResponseEntity.ok(sum.get());
+    }
+
+    return ResponseEntity.notFound().build();
+  }
+
+  @GetMapping("results")
+  public Map<Integer, Map<String, List<AdderResult>>> results()
+  {
+    return results.getState();
+  }
+
+  @GetMapping("results/{user}")
+  public ResponseEntity<List<AdderResult>> results(@PathVariable String user)
+  {
+    for (Map<String, List<AdderResult>> resultsByUser : this.results.getState().values())
+    {
+      List<AdderResult> results = resultsByUser.get(user);
+      if (results != null)
+        return ResponseEntity.ok(results);
+    }
+
+    return ResponseEntity.notFound().build();
   }