X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fdomain%2FChatHome.java;h=13f18b913fde4082b68823fb6d0a3d45b2e15b9d;hb=f9b0d9a71a85741983a451a51fdbd1c38632be87;hp=ed4d8c7f410d0cbc4bfa0069a3bb8fe14a03c7c6;hpb=70ffb4f9a4bf5a3e419e0a39824f7dfb888bb8dc;p=demos%2Fkafka%2Fchat 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 ed4d8c7f..13f18b91 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 @@ -2,39 +2,41 @@ package de.juplo.kafka.chat.backend.domain; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.*; -import java.util.stream.Stream; @Slf4j public class ChatHome { - private final Map chatrooms; - private final ChatroomFactory factory; + private final Map chatrooms; + private final ChatHomeService service; - public ChatHome(ChatroomFactory factory, Flux chatroomFlux) + public ChatHome(ChatHomeService service, Flux chatroomFlux) { - log.debug("Creating ChatHome with factory: {}", factory); - this.factory = factory; + log.debug("Creating ChatHome with factory: {}", service); + this.service = service; this.chatrooms = new HashMap<>(); chatroomFlux.subscribe(chatroom -> chatrooms.put(chatroom.getId(), chatroom)); } - public Chatroom createChatroom(String name) + public Mono createChatroom(String name) { - Chatroom chatroom = factory.createChatroom(UUID.randomUUID(), name); + ChatRoom chatroom = service.createChatroom(UUID.randomUUID(), name); chatrooms.put(chatroom.getId(), chatroom); - return chatroom; + return Mono.justOrEmpty(chatroom); } - public Optional getChatroom(UUID id) + public Mono getChatroom(UUID id) { - return Optional.ofNullable(chatrooms.get(id)); + return Mono + .justOrEmpty(chatrooms.get(id)) + .or(Mono.error(() -> new UnknownChatroomException(id))); } - public Stream list() + public Flux list() { - return chatrooms.values().stream(); + return Flux.fromStream(chatrooms.values().stream()); } }