X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fapi%2FChatBackendController.java;h=6c7cff7de38056ea93f7804f9da26499a72fb3cc;hb=7107fd0b4795d95b6c7f49cf4e765b82c16d016e;hp=d6130927547b169fff7b90abc5dec4235bac9df0;hpb=7414187b1d200ee3f837a509d22e55dda3a00d32;p=demos%2Fkafka%2Fchat 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 d6130927..6c7cff7d 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,89 +1,162 @@ -package de.juplo.kafka.chatroom.api; +package de.juplo.kafka.chat.backend.api; -import de.juplo.kafka.chatroom.domain.Chatroom; -import lombok.RequiredArgsConstructor; -import org.springframework.http.MediaType; +import de.juplo.kafka.chat.backend.ChatBackendProperties; +import de.juplo.kafka.chat.backend.domain.ChatHomeService; +import de.juplo.kafka.chat.backend.domain.ChatRoomData; +import de.juplo.kafka.chat.backend.implementation.StorageStrategy; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; 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; +import java.util.logging.Level; @RestController -@RequiredArgsConstructor -public class ChatroomController +public class ChatBackendController { - private final Map chatrooms = new HashMap<>(); - private final Clock clock; + private final ChatHomeService chatHomeService; + private final StorageStrategy storageStrategy; + private final String loggingCategory = ChatBackendController.class.getSimpleName(); + private final Level loggingLevel; + private final boolean showOperatorLine; + + + public ChatBackendController( + ChatHomeService chatHomeService, + StorageStrategy storageStrategy, + ChatBackendProperties properties) + { + this.chatHomeService = chatHomeService; + this.storageStrategy = storageStrategy; + this.loggingLevel = properties.getProjectreactor().getLoggingLevel(); + this.showOperatorLine = properties.getProjectreactor().isShowOperatorLine(); + } @PostMapping("create") - public Chatroom create(@RequestBody String name) + public Mono create(@RequestBody String name) { - Chatroom chatroom = new Chatroom(UUID.randomUUID(), name); - chatrooms.put(chatroom.getId(), chatroom); - return chatroom; + UUID chatRoomId = UUID.randomUUID(); + return chatHomeService + .createChatRoom(chatRoomId, name) + .map(ChatRoomInfoTo::from); } @GetMapping("list") - public Collection list() + public Flux list() + { + return chatHomeService + .getChatRoomInfo() + .map(chatroomInfo -> ChatRoomInfoTo.from(chatroomInfo)); + } + + @GetMapping("{chatRoomId}/list") + public Flux list(@PathVariable UUID chatRoomId) { - return chatrooms.values(); + return chatHomeService + .getChatRoomData(chatRoomId) + .flatMapMany(chatRoomData -> chatRoomData + .getMessages() + .map(MessageTo::from)); } - @GetMapping("get/{chatroomId}") - public Chatroom get(@PathVariable UUID chatroomId) + @GetMapping("{chatRoomId}") + public Mono get(@PathVariable UUID chatRoomId) { - return chatrooms.get(chatroomId); + return chatHomeService + .getChatRoomInfo(chatRoomId) + .map(chatRoomInfo -> ChatRoomInfoTo.from(chatRoomInfo)); } - @PutMapping("put/{chatroomId}/{username}/{messageId}") + @PutMapping("{chatRoomId}/{username}/{messageId}") public Mono put( - @PathVariable UUID chatroomId, + @PathVariable UUID chatRoomId, @PathVariable String username, @PathVariable Long messageId, @RequestBody String text) { - Chatroom chatroom = chatrooms.get(chatroomId); return - chatroom + chatHomeService + .getChatRoomData(chatRoomId) + .flatMap(chatRoomData -> put(chatRoomData, username, messageId, text)); + } + + private Mono put( + ChatRoomData chatRoomData, + String username, + Long messageId, + String text) + { + return + chatRoomData .addMessage( messageId, - LocalDateTime.now(clock), username, text) - .switchIfEmpty(chatroom.getMessage(username, messageId)) .map(message -> MessageTo.from(message)); } - @GetMapping("get/{chatroomId}/{username}/{messageId}") + @GetMapping("{chatRoomId}/{username}/{messageId}") public Mono get( - @PathVariable UUID chatroomId, + @PathVariable UUID chatRoomId, @PathVariable String username, @PathVariable Long messageId) { return - chatrooms - .get(chatroomId) + chatHomeService + .getChatRoomData(chatRoomId) + .flatMap(chatRoomData -> get(chatRoomData, username, messageId)); + } + + private Mono get( + ChatRoomData chatRoomData, + String username, + Long messageId) + { + return + chatRoomData .getMessage(username, messageId) .map(message -> MessageTo.from(message)); } - @GetMapping( - path = "listen/{chatroomId}", - produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public Flux listen(@PathVariable UUID chatroomId) + @GetMapping(path = "{chatRoomId}/listen") + public Flux> listen(@PathVariable UUID chatRoomId) + { + return chatHomeService + .getChatRoomData(chatRoomId) + .flatMapMany(chatRoomData -> listen(chatRoomData)); + } + + private Flux> listen(ChatRoomData chatRoomData) { - return chatrooms - .get(chatroomId) + return chatRoomData .listen() - .log() - .map(message -> MessageTo.from(message)); + .log( + loggingCategory, + loggingLevel, + showOperatorLine) + .map(message -> MessageTo.from(message)) + .map(messageTo -> + ServerSentEvent + .builder(messageTo) + .id(messageTo.getSerial().toString()) + .event("message") + .build()); + } + + @GetMapping("/shards") + public Mono getShardOwners() + { + return chatHomeService.getShardOwners(); + } + + @PostMapping("/store") + public void store() + { + storageStrategy + .write(chatHomeService) + .subscribe(); } }