feat: Introduced `ChatHome` that gives multiple `Chatroom`s a home
authorKai Moritz <kai@juplo.de>
Thu, 5 Jan 2023 18:18:50 +0000 (19:18 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 7 Jan 2023 19:27:00 +0000 (20:27 +0100)
src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java [new file with mode: 0644]

index 34036dd..835627a 100644 (file)
@@ -1,5 +1,6 @@
 package de.juplo.kafka.chat.backend;
 
+import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
 import de.juplo.kafka.chat.backend.persistence.InMemoryChatroomFactory;
 import de.juplo.kafka.chat.backend.persistence.InMemoryPersistenceStrategy;
@@ -12,6 +13,12 @@ import java.time.Clock;
 @Configuration
 public class ChatBackendConfiguration
 {
+  @Bean
+  public ChatHome chatHome(ChatroomFactory chatroomFactory)
+  {
+    return new ChatHome(chatroomFactory);
+  }
+
   @Bean
   ChatroomFactory chatroomFactory(InMemoryPersistenceStrategy persistenceStrategy)
   {
index b8024e6..385c95c 100644 (file)
@@ -1,7 +1,7 @@
 package de.juplo.kafka.chat.backend.api;
 
+import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.domain.Chatroom;
-import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
@@ -11,8 +11,6 @@ import reactor.core.publisher.Mono;
 import java.time.Clock;
 import java.time.LocalDateTime;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.UUID;
 
 
@@ -20,29 +18,26 @@ import java.util.UUID;
 @RequiredArgsConstructor
 public class ChatBackendController
 {
-  private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
-  private final ChatroomFactory factory;
+  private final ChatHome chatHome;
   private final Clock clock;
 
 
   @PostMapping("create")
   public Chatroom create(@RequestBody String name)
   {
-    Chatroom chatroom = factory.createChatroom(UUID.randomUUID(), name);
-    chatrooms.put(chatroom.getId(), chatroom);
-    return chatroom;
+    return chatHome.createChatroom(name);
   }
 
   @GetMapping("list")
   public Collection<Chatroom> list()
   {
-    return chatrooms.values();
+    return chatHome.list();
   }
 
   @GetMapping("get/{chatroomId}")
   public Chatroom get(@PathVariable UUID chatroomId)
   {
-    return chatrooms.get(chatroomId);
+    return chatHome.getChatroom(chatroomId);
   }
 
   @PutMapping("put/{chatroomId}/{username}/{messageId}")
@@ -52,7 +47,7 @@ public class ChatBackendController
       @PathVariable Long messageId,
       @RequestBody String text)
   {
-    Chatroom chatroom = chatrooms.get(chatroomId);
+    Chatroom chatroom = chatHome.getChatroom(chatroomId);
     return
         chatroom
             .addMessage(
@@ -71,8 +66,8 @@ public class ChatBackendController
       @PathVariable Long messageId)
   {
     return
-        chatrooms
-            .get(chatroomId)
+        chatHome
+            .getChatroom(chatroomId)
             .getMessage(username, messageId)
             .map(message -> MessageTo.from(message));
   }
@@ -82,8 +77,8 @@ public class ChatBackendController
       produces = MediaType.TEXT_EVENT_STREAM_VALUE)
   public Flux<MessageTo> listen(@PathVariable UUID chatroomId)
   {
-    return chatrooms
-        .get(chatroomId)
+    return chatHome
+        .getChatroom(chatroomId)
         .listen()
         .log()
         .map(message -> MessageTo.from(message));
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
new file mode 100644 (file)
index 0000000..a75a9a1
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import lombok.RequiredArgsConstructor;
+
+import java.util.*;
+
+
+@RequiredArgsConstructor
+public class ChatHome
+{
+  private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
+  private final ChatroomFactory factory;
+
+
+  public Chatroom createChatroom(String name)
+  {
+    Chatroom chatroom = factory.createChatroom(UUID.randomUUID(), name);
+    chatrooms.put(chatroom.getId(), chatroom);
+    return chatroom;
+  }
+
+  public Chatroom getChatroom(UUID id)
+  {
+    return chatrooms.get(id);
+  }
+
+  public Collection<Chatroom> list()
+  {
+    return chatrooms.values();
+  }
+}