import de.juplo.kafka.chat.backend.domain.ChatHome;
import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
+import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Clock;
import java.time.LocalDateTime;
-import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
{
private final ChatHome chatHome;
private final Clock clock;
+ private final StorageStrategy storageStrategy;
@PostMapping("create")
return chatHome.list().map(chatroom -> ChatroomTo.from(chatroom));
}
+ @GetMapping("list/{chatroomId}")
+ public Flux<MessageTo> list(@PathVariable UUID chatroomId)
+ {
+ return chatHome
+ .getChatroom(chatroomId)
+ .map(chatroom -> chatroom
+ .getMessages()
+ .map(MessageTo::from))
+ .get();
+ }
+
@GetMapping("get/{chatroomId}")
public Optional<ChatroomTo> get(@PathVariable UUID chatroomId)
{
.map(message -> MessageTo.from(message));
}
- @GetMapping(
- path = "listen/{chatroomId}",
- produces = MediaType.TEXT_EVENT_STREAM_VALUE)
- public Flux<MessageTo> listen(@PathVariable UUID chatroomId)
+ @GetMapping(path = "listen/{chatroomId}")
+ public Flux<ServerSentEvent<MessageTo>> listen(@PathVariable UUID chatroomId)
{
return chatHome
.getChatroom(chatroomId)
.orElseThrow(() -> new UnknownChatroomException(chatroomId));
}
- private Flux<MessageTo> listen(Chatroom chatroom)
+ private Flux<ServerSentEvent<MessageTo>> listen(Chatroom chatroom)
{
return chatroom
.listen()
.log()
- .map(message -> MessageTo.from(message));
+ .map(message -> MessageTo.from(message))
+ .map(messageTo ->
+ ServerSentEvent
+ .builder(messageTo)
+ .id(messageTo.getSerial().toString())
+ .event("message")
+ .build());
+ }
+
+ @PostMapping("/store")
+ public void store()
+ {
+ storageStrategy.writeChatrooms(Flux.fromStream(chatHome.list()));
}
}