feat: `ChatHome` returns an `Optional` for `getChatroom(UUID)`
authorKai Moritz <kai@juplo.de>
Sat, 7 Jan 2023 19:29:32 +0000 (20:29 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/api/UnknownChatroomException.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java

index 385c95c..7d1655d 100644 (file)
@@ -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<Chatroom> 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<MessageTo> 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<MessageTo> 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<MessageTo> 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 (file)
index 0000000..2861826
--- /dev/null
@@ -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;
+  }
+}
index a75a9a1..84b9e94 100644 (file)
@@ -19,9 +19,9 @@ public class ChatHome
     return chatroom;
   }
 
-  public Chatroom getChatroom(UUID id)
+  public Optional<Chatroom> getChatroom(UUID id)
   {
-    return chatrooms.get(id);
+    return Optional.ofNullable(chatrooms.get(id));
   }
 
   public Collection<Chatroom> list()