refactor: Simplified the handling of `MessageMutationException`
authorKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 21:08:11 +0000 (22:08 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 21:10:11 +0000 (22:10 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java

index 4f855b8..02f5c08 100644 (file)
@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.core.publisher.Sinks;
+import reactor.core.publisher.SynchronousSink;
 
 import java.time.Clock;
 import java.time.LocalDateTime;
@@ -67,9 +68,17 @@ public class ChatRoom
     Message.MessageKey key = Message.MessageKey.of(user, id);
     return service
         .getMessage(key)
-        .flatMap(existing -> text.equals(existing.getMessageText())
-            ? Mono.just(existing)
-            : Mono.error(() -> new MessageMutationException(existing, text)))
+        .handle((Message existing, SynchronousSink<Message> sink) ->
+        {
+          if (existing.getMessageText().equals(text))
+          {
+            sink.next(existing);
+          }
+          else
+          {
+            sink.error(new MessageMutationException(existing, text));
+          }
+        })
         .switchIfEmpty(
             Mono
                 .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))