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;
@Configuration
public class ChatBackendConfiguration
{
+ @Bean
+ public ChatHome chatHome(ChatroomFactory chatroomFactory)
+ {
+ return new ChatHome(chatroomFactory);
+ }
+
@Bean
ChatroomFactory chatroomFactory(InMemoryPersistenceStrategy persistenceStrategy)
{
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.*;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
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}")
@PathVariable Long messageId,
@RequestBody String text)
{
- Chatroom chatroom = chatrooms.get(chatroomId);
+ Chatroom chatroom = chatHome.getChatroom(chatroomId);
return
chatroom
.addMessage(
@PathVariable Long messageId)
{
return
- chatrooms
- .get(chatroomId)
+ chatHome
+ .getChatroom(chatroomId)
.getMessage(username, messageId)
.map(message -> MessageTo.from(message));
}
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));
--- /dev/null
+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();
+ }
+}