import java.time.Clock;
import java.time.LocalDateTime;
import java.util.Collection;
+import java.util.Optional;
import java.util.UUID;
}
@GetMapping("get/{chatroomId}")
- public Chatroom get(@PathVariable UUID chatroomId)
+ public Optional<Chatroom> get(@PathVariable UUID chatroomId)
{
return chatHome.getChatroom(chatroomId);
}
@PathVariable Long messageId,
@RequestBody String text)
{
- Chatroom chatroom = chatHome.getChatroom(chatroomId);
+ return
+ chatHome
+ .getChatroom(chatroomId)
+ .map(chatroom -> put(chatroom, username, messageId, text))
+ .orElseThrow(() -> new UnknownChatroomException(chatroomId));
+ }
+
+ public Mono<MessageTo> put(
+ Chatroom chatroom,
+ String username,
+ Long messageId,
+ String text)
+ {
return
chatroom
.addMessage(
return
chatHome
.getChatroom(chatroomId)
+ .map(chatroom -> get(chatroom, username, messageId))
+ .orElseThrow(() -> new UnknownChatroomException(chatroomId));
+ }
+
+ private Mono<MessageTo> get(
+ Chatroom chatroom,
+ String username,
+ Long messageId)
+ {
+ return
+ chatroom
.getMessage(username, messageId)
.map(message -> MessageTo.from(message));
}
{
return chatHome
.getChatroom(chatroomId)
+ .map(chatroom -> listen(chatroom))
+ .orElseThrow(() -> new UnknownChatroomException(chatroomId));
+ }
+
+ private Flux<MessageTo> listen(Chatroom chatroom)
+ {
+ return chatroom
.listen()
.log()
.map(message -> MessageTo.from(message));
--- /dev/null
+package de.juplo.kafka.chat.backend.api;
+
+import lombok.Getter;
+
+import java.util.UUID;
+
+
+public class UnknownChatroomException extends RuntimeException
+{
+ @Getter
+ private final UUID chatroomId;
+
+ public UnknownChatroomException(UUID chatroomId)
+ {
+ super("Chatroom does not exist: " + chatroomId);
+ this.chatroomId = chatroomId;
+ }
+}