From: Kai Moritz Date: Tue, 5 Sep 2023 18:46:56 +0000 (+0200) Subject: refactor: Renamed `ChatRoom` into `ChatRoomData` - Aligned Code X-Git-Tag: rebase--2023-09-05--23-53~5 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=3d0f8b0ffc397ba7862b2a32ebcd575cf960ed0c;p=demos%2Fkafka%2Fchat refactor: Renamed `ChatRoom` into `ChatRoomData` - Aligned Code --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java deleted file mode 100644 index d58ba5e0..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence.storage.mongodb; - -import lombok.*; - - -@AllArgsConstructor -@NoArgsConstructor -@Getter(AccessLevel.PACKAGE) -@Setter(AccessLevel.PACKAGE) -@EqualsAndHashCode -@ToString -public class MessageId -{ - private String chatRoomId; - private String user; - private Long id; -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageRepository.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageRepository.java index c2a7f0dc..a429f96e 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageRepository.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageRepository.java @@ -7,5 +7,5 @@ import java.util.List; public interface MessageRepository extends MongoRepository { - List findByChatRoomId(String chatRoomId); + List findByChatRoomIdOrderBySerialAsc(String chatRoomId); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageTo.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageTo.java index 45dd50a3..90e4e9cd 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageTo.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageTo.java @@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.persistence.storage.mongodb; import de.juplo.kafka.chat.backend.domain.Message; import lombok.*; import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.time.LocalDateTime; @@ -19,8 +20,13 @@ import java.util.regex.Matcher; @Document class MessageTo { - @Id - private MessageId id; + @Indexed + private String chatRoomId; + @Indexed + private String user; + @Indexed + private Long id; + @Indexed private Long serial; private String time; private String text; @@ -28,7 +34,7 @@ class MessageTo Message toMessage() { return new Message( - Message.MessageKey.of(id.getUser(), id.getId()), + Message.MessageKey.of(user, id), serial, LocalDateTime.parse(time), text); @@ -38,10 +44,9 @@ class MessageTo { return new MessageTo( - new MessageId( - chatRoomId.toString(), - message.getUsername(), - message.getId()), + chatRoomId.toString(), + message.getUsername(), + message.getId(), message.getSerialNumber(), message.getTimestamp().toString(), message.getMessageText()); diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageStrategy.java index c7698e89..e64acf29 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageStrategy.java @@ -1,6 +1,5 @@ package de.juplo.kafka.chat.backend.persistence.storage.mongodb; -import de.juplo.kafka.chat.backend.domain.ChatRoomData; import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; import de.juplo.kafka.chat.backend.domain.Message; import de.juplo.kafka.chat.backend.persistence.inmemory.ShardingStrategy; @@ -69,7 +68,7 @@ public class MongoDbStorageStrategy implements StorageStrategy public Flux readChatRoomData(UUID chatRoomId) { return Flux - .fromIterable(messageRepository.findByChatRoomId(chatRoomId.toString())) + .fromIterable(messageRepository.findByChatRoomIdOrderBySerialAsc(chatRoomId.toString())) .map(messageTo -> messageTo.toMessage()); } }