refactor: Renamed `ChatRoom` into `ChatRoomData` - Aligned Code
authorKai Moritz <kai@juplo.de>
Tue, 5 Sep 2023 18:46:56 +0000 (20:46 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 5 Sep 2023 18:46:56 +0000 (20:46 +0200)
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageRepository.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageTo.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageStrategy.java

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 (file)
index d58ba5e..0000000
+++ /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;
-}
index c2a7f0d..a429f96 100644 (file)
@@ -7,5 +7,5 @@ import java.util.List;
 
 public interface MessageRepository extends MongoRepository<MessageTo, String>
 {
-  List<MessageTo> findByChatRoomId(String chatRoomId);
+  List<MessageTo> findByChatRoomIdOrderBySerialAsc(String chatRoomId);
 }
index 45dd50a..90e4e9c 100644 (file)
@@ -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());
index c7698e8..e64acf2 100644 (file)
@@ -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<Message> readChatRoomData(UUID chatRoomId)
   {
     return Flux
-        .fromIterable(messageRepository.findByChatRoomId(chatRoomId.toString()))
+        .fromIterable(messageRepository.findByChatRoomIdOrderBySerialAsc(chatRoomId.toString()))
         .map(messageTo -> messageTo.toMessage());
   }
 }