refactor: Refined stream-definition in `StorageStrategy#write`
authorKai Moritz <kai@juplo.de>
Tue, 20 Feb 2024 10:28:22 +0000 (11:28 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 20 Feb 2024 10:29:00 +0000 (11:29 +0100)
* Changed the stream-definition to a more natural order.
* As a result, the stored `ChatRoomInfo`-instances do not have to be
  handed clumsily to the following stream.

src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java

index 94b2bb4..647ac52 100644 (file)
@@ -17,19 +17,16 @@ public interface StorageStrategy
 
   default Mono<Void> write(ChatHomeService chatHomeService)
   {
-    return writeChatRoomInfo(
-        chatHomeService
-            .getChatRoomInfo()
-            .flatMap(chatRoomInfo -> writeChatRoomData(
-                chatRoomInfo.getId(),
-                chatHomeService
-                    .getChatRoomData(chatRoomInfo.getId())
-                    .flatMapMany(chatRoomData -> chatRoomData.getMessages())
-                )
-                .then(Mono.just(chatRoomInfo))
-                .doOnSuccess(emittedChatRoomInfo -> log.info("Stored {}", chatRoomInfo))
-                .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable)))
-        )
+    return writeChatRoomInfo(chatHomeService.getChatRoomInfo())
+        .flatMap(chatRoomInfo -> writeChatRoomData(
+            chatRoomInfo.getId(),
+            chatHomeService
+                .getChatRoomData(chatRoomInfo.getId())
+                .flatMapMany(chatRoomData -> chatRoomData.getMessages())
+            )
+            .then()
+            .doOnSuccess(empty -> log.info("Stored {}", chatRoomInfo))
+            .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable)))
         .then()
         .doOnSuccess(empty -> log.info("Stored {}", chatHomeService))
         .doOnError(throwable -> log.error("Could not store {}: {}", chatHomeService, throwable));