From ac540fd9382c2191798c979241e02cce788a4169 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 7 Jan 2023 20:29:32 +0100 Subject: [PATCH] feat: `ChatHome` returns an `Optional` for `getChatroom(UUID)` --- .../backend/api/ChatBackendController.java | 35 +++++++++++++++++-- .../backend/api/UnknownChatroomException.java | 18 ++++++++++ .../kafka/chat/backend/domain/ChatHome.java | 4 +-- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/api/UnknownChatroomException.java 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() -- 2.20.1