1 package de.juplo.kafka;
3 import lombok.RequiredArgsConstructor;
4 import org.springframework.http.HttpStatus;
5 import org.springframework.http.ResponseEntity;
6 import org.springframework.web.bind.annotation.*;
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.stream.Collectors;
16 @RequiredArgsConstructor
17 public class DriverController
19 private final EndlessConsumer consumer;
20 private final ApplicationRecordHandler recordHandler;
21 private final AdderResults results;
31 public void stop() throws ExecutionException, InterruptedException
38 public Map<Integer, Map<String, AdderResult>> state()
45 .collect(Collectors.toMap(
46 entry -> entry.getKey(),
47 entry -> entry.getValue().getState()));
50 @GetMapping("state/{user}")
51 public ResponseEntity<Long> state(@PathVariable String user)
53 for (AdderBusinessLogic adder : recordHandler.getState().values())
55 Optional<Long> sum = adder.getSum(user);
57 return ResponseEntity.ok(sum.get());
60 return ResponseEntity.notFound().build();
63 @GetMapping("results")
64 public Map<Integer, Map<String, List<AdderResult>>> results()
66 return results.getState();
69 @GetMapping("results/{user}")
70 public ResponseEntity<List<AdderResult>> results(@PathVariable String user)
72 for (Map<String, List<AdderResult>> resultsByUser : this.results.getState().values())
74 List<AdderResult> results = resultsByUser.get(user);
76 return ResponseEntity.ok(results);
79 return ResponseEntity.notFound().build();
84 @ResponseStatus(HttpStatus.BAD_REQUEST)
85 public ErrorResponse illegalStateException(IllegalStateException e)
87 return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());