From: Kai Moritz Date: Sat, 7 Jan 2023 19:29:32 +0000 (+0100) Subject: feat: `ChatHome` returns an `Optional` for `getChatroom(UUID)` X-Git-Tag: wip~87 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=45c723b0b0c2c6fb39d126ef2cbaf0bd9f365236;p=demos%2Fkafka%2Fchat feat: `ChatHome` returns an `Optional` for `getChatroom(UUID)` --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 385c95c8..7d1655db 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -11,6 +11,7 @@ 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; @@ -35,7 +36,7 @@ public class ChatBackendController } @GetMapping("get/{chatroomId}") - public Chatroom get(@PathVariable UUID chatroomId) + public Optional get(@PathVariable UUID chatroomId) { return chatHome.getChatroom(chatroomId); } @@ -47,7 +48,19 @@ public class ChatBackendController @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 put( + Chatroom chatroom, + String username, + Long messageId, + String text) + { return chatroom .addMessage( @@ -68,6 +81,17 @@ public class ChatBackendController return chatHome .getChatroom(chatroomId) + .map(chatroom -> get(chatroom, username, messageId)) + .orElseThrow(() -> new UnknownChatroomException(chatroomId)); + } + + private Mono get( + Chatroom chatroom, + String username, + Long messageId) + { + return + chatroom .getMessage(username, messageId) .map(message -> MessageTo.from(message)); } @@ -79,6 +103,13 @@ public class ChatBackendController { return chatHome .getChatroom(chatroomId) + .map(chatroom -> listen(chatroom)) + .orElseThrow(() -> new UnknownChatroomException(chatroomId)); + } + + private Flux listen(Chatroom chatroom) + { + return chatroom .listen() .log() .map(message -> MessageTo.from(message)); diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/UnknownChatroomException.java b/src/main/java/de/juplo/kafka/chat/backend/api/UnknownChatroomException.java new file mode 100644 index 00000000..28618264 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/api/UnknownChatroomException.java @@ -0,0 +1,18 @@ +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; + } +} diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java index a75a9a13..84b9e946 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java @@ -19,9 +19,9 @@ public class ChatHome return chatroom; } - public Chatroom getChatroom(UUID id) + public Optional getChatroom(UUID id) { - return chatrooms.get(id); + return Optional.ofNullable(chatrooms.get(id)); } public Collection list()