1 package de.juplo.kafka.chatroom.api;
3 import de.juplo.kafka.chatroom.domain.Chatroom;
4 import de.juplo.kafka.chatroom.domain.Message;
5 import lombok.RequiredArgsConstructor;
6 import org.springframework.http.MediaType;
7 import org.springframework.web.bind.annotation.*;
8 import reactor.core.publisher.Flux;
9 import reactor.core.publisher.Mono;
11 import java.time.Clock;
12 import java.time.Duration;
13 import java.time.LocalDateTime;
14 import java.time.LocalTime;
15 import java.util.Collection;
16 import java.util.HashMap;
18 import java.util.UUID;
22 @RequiredArgsConstructor
23 public class ChatroomController
25 private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
26 private final Clock clock;
29 @PostMapping("create")
30 public Chatroom create(@RequestBody String name)
32 Chatroom chatroom = new Chatroom(UUID.randomUUID(), name);
33 chatrooms.put(chatroom.getId(), chatroom);
38 public Collection<Chatroom> list()
40 return chatrooms.values();
43 @PutMapping("post/{chatroomId}/{username}/{messageId}")
44 public Mono<MessageTo> post(
45 @PathVariable UUID chatroomId,
46 @PathVariable String username,
47 @PathVariable UUID messageId,
48 @RequestBody String text)
55 LocalDateTime.now(clock),
58 .map(message -> MessageTo.from(message));
62 path = "listen/{chatroomId}",
63 produces = MediaType.TEXT_EVENT_STREAM_VALUE)
64 public Flux<Message> listen(@PathVariable UUID chatroomId)
66 return chatrooms.get(chatroomId).listen();