From: Kai Moritz Date: Thu, 5 Jan 2023 18:18:50 +0000 (+0100) Subject: feat: Introduced `ChatHome` that gives multiple `Chatroom`s a home X-Git-Tag: TEST~58 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=20347c717e822a67ff85154c69ead5740f00a859;p=demos%2Fkafka%2Fchat feat: Introduced `ChatHome` that gives multiple `Chatroom`s a home --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index 34036dda..835627a0 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -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) { 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 b8024e63..385c95c8 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 @@ -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 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 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 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 index 00000000..a75a9a13 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java @@ -0,0 +1,31 @@ +package de.juplo.kafka.chat.backend.domain; + +import lombok.RequiredArgsConstructor; + +import java.util.*; + + +@RequiredArgsConstructor +public class ChatHome +{ + private final Map 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 list() + { + return chatrooms.values(); + } +}