TMP
[demos/kafka/training] / src / main / java / de / juplo / kafka / DriverController.java
index d8068e5..5a09c1b 100644 (file)
@@ -1,9 +1,11 @@
 package de.juplo.kafka;
 
 import lombok.RequiredArgsConstructor;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
 
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
 
 
@@ -12,6 +14,7 @@ import java.util.concurrent.ExecutionException;
 public class DriverController
 {
   private final EndlessConsumer consumer;
+  private final SumRecordHandler wordcount;
 
 
   @PostMapping("start")
@@ -25,4 +28,32 @@ public class DriverController
   {
     consumer.stop();
   }
+
+
+  @GetMapping("seen")
+  public Map<Integer, Map<String, Map<String, Long>>> seen()
+  {
+    return wordcount.getSeen();
+  }
+
+  @GetMapping("seen/{user}")
+  public ResponseEntity<Map<String, Long>> seen(@PathVariable String user)
+  {
+    for (Map<String, Map<String, Long>> users : wordcount.getSeen().values())
+    {
+      Map<String, Long> words = users.get(user);
+      if (words != null)
+        return ResponseEntity.ok(words);
+    }
+
+    return ResponseEntity.notFound().build();
+  }
+
+
+  @ExceptionHandler
+  @ResponseStatus(HttpStatus.BAD_REQUEST)
+  public ErrorResponse illegalStateException(IllegalStateException e)
+  {
+    return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
+  }
 }