WIP:callbacks - chat-home rebase--2024-02-19--08-16
authorKai Moritz <kai@juplo.de>
Sun, 18 Feb 2024 20:46:56 +0000 (21:46 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 18 Feb 2024 20:46:56 +0000 (21:46 +0100)
src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java

index 9dd7625..11f91f4 100644 (file)
@@ -17,27 +17,39 @@ public interface StorageStrategy
   Logger log = LoggerFactory.getLogger(StorageStrategy.class.getCanonicalName());
 
   default void write(ChatHomeService chatHomeService)
+  {
+    write(
+        chatHomeService,
+        this::logSuccessChatHomeService,
+        this::logFailureChatHomeService);
+  }
+
+  default void write(
+      ChatHomeService chatHomeService,
+      ChatHomeServiceWrittenSuccessCallback successCallback,
+      ChatHomeServiceWrittenFailureCallback failureCallback)
   {
     writeChatRoomInfo(
         chatHomeService
             .getChatRoomInfo()
+            .doOnComplete(() -> successCallback.accept(chatHomeService))
+            .doOnError(throwable -> failureCallback.accept(chatHomeService, throwable))
             .doOnNext(chatRoomInfo ->
                 writeChatRoomData(
                     chatRoomInfo.getId(),
                     chatHomeService
                         .getChatRoomData(chatRoomInfo.getId())
                         .flatMapMany(chatRoomData -> chatRoomData.getMessages()),
-                    this::logSuccess,
-                    this::logFailure)));
+                    this::logSuccessChatRoom,
+                    this::logFailureChatRoom)));
   }
-
   void writeChatRoomInfo(Flux<ChatRoomInfo> chatRoomInfoFlux);
   Flux<ChatRoomInfo> readChatRoomInfo();
   default void writeChatRoomData(
       UUID chatRoomId,
       Flux<Message> messageFlux,
-      SuccessCallback successCallback,
-      FailureCallback failureCallback)
+      ChatRoomWrittenSuccessCallback successCallback,
+      ChatRoomWrittenFailureCallback failureCallback)
   {
     writeChatRoomData(
         chatRoomId,
@@ -48,15 +60,28 @@ public interface StorageStrategy
   void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux);
   Flux<Message> readChatRoomData(UUID chatRoomId);
 
-  interface SuccessCallback extends Consumer<UUID> {}
-  interface FailureCallback extends BiConsumer<UUID, Throwable> {}
+  interface ChatHomeServiceWrittenSuccessCallback extends Consumer<ChatHomeService> {}
+  interface ChatHomeServiceWrittenFailureCallback extends BiConsumer<ChatHomeService, Throwable> {}
+
+  default void logSuccessChatHomeService(ChatHomeService chatHomeService)
+  {
+    log.info("Successfully stored chat-home {}", chatHomeService);
+  }
+
+  default void logFailureChatHomeService(ChatHomeService chatHomeService, Throwable throwable)
+  {
+    log.error("Could not store chat-home {}: {}", chatHomeService, throwable);
+  }
+
+  interface ChatRoomWrittenSuccessCallback extends Consumer<UUID> {}
+  interface ChatRoomWrittenFailureCallback extends BiConsumer<UUID, Throwable> {}
 
-  default void logSuccess(UUID chatRoomId)
+  default void logSuccessChatRoom(UUID chatRoomId)
   {
     log.info("Successfully stored chat-room {}", chatRoomId);
   }
 
-  default void logFailure(UUID chatRoomId, Throwable throwable)
+  default void logFailureChatRoom(UUID chatRoomId, Throwable throwable)
   {
     log.error("Could not store chat-room {}: {}", chatRoomId, throwable);
   }