From: Kai Moritz Date: Wed, 25 Jan 2023 17:35:10 +0000 (+0100) Subject: WIP:refactor X-Git-Tag: wip~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=2f099cf12d3cd2ef0c017a335d573cd2dcbf59d3;p=demos%2Fkafka%2Fchat WIP:refactor --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java index 9ae967bd..dd7321ed 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java @@ -65,20 +65,30 @@ public class ChatRoom throw new InvalidUsernameException(user); Message.MessageKey key = Message.MessageKey.of(user, id); - return service - .getMessage(key) - .flatMap(existing -> text.equals(existing.getMessageText()) - ? Mono.just(existing) - : service - .persistMessage(key, LocalDateTime.now(clock), text) - .doOnNext(m -> - { - Sinks.EmitResult result = sink.tryEmitNext(m); - if (result.isFailure()) - { - log.warn("Emitting of message failed with {} for {}", result.name(), m); - } - })); + Mono mono = service + .getMessage(key) + .handle((existing, sink) -> + { + if (existing.getMessageText().equals(text)) + { + sink.next(existing); + } + else + { + sink.error(new MessageMutationException(existing, text)); + } + }); + + return mono.>switchIfEmpty(service + .persistMessage(key, LocalDateTime.now(clock), text) + .>doOnNext(m -> + { + Sinks.EmitResult result = sink.tryEmitNext(m); + if (result.isFailure()) + { + log.warn("Emitting of message failed with {} for {}", result.name(), m); + } + })); }