refactor: Renamed `ChatRoom` into `ChatRoomData` - Aligned Code
authorKai Moritz <kai@juplo.de>
Tue, 5 Sep 2023 18:37:12 +0000 (20:37 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 5 Sep 2023 18:37:12 +0000 (20:37 +0200)
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/ChatRoomTo.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java [new file with mode: 0644]
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
src/test/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageToTest.java [deleted file]

index 58f5bce..0086053 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 lombok.*;
 import org.springframework.data.annotation.Id;
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
new file mode 100644 (file)
index 0000000..d58ba5e
--- /dev/null
@@ -0,0 +1,17 @@
+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 4f93695..45dd50a 100644 (file)
@@ -2,10 +2,12 @@ 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.mapping.Document;
 
 import java.time.LocalDateTime;
+import java.util.UUID;
 import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 
 @AllArgsConstructor
@@ -14,33 +16,32 @@ import java.util.regex.Pattern;
 @Setter(AccessLevel.PACKAGE)
 @EqualsAndHashCode(of = { "user", "id" })
 @ToString(of = { "user", "id" })
+@Document
 class MessageTo
 {
-  final static Pattern SPLIT_ID = Pattern.compile("^([a-z-0-9]+)--([0-9]+)$");
-  private String id;
+  @Id
+  private MessageId id;
   private Long serial;
   private String time;
   private String text;
 
   Message toMessage()
   {
-    Matcher matcher = SPLIT_ID.matcher(id);
-    if (!matcher.matches())
-      throw new RuntimeException("MessageTo with invalid ID: " + id);
-    Long messageId = Long.parseLong(matcher.group(2));
-    String user = matcher.group(1);
     return new Message(
-        Message.MessageKey.of(user, messageId),
+        Message.MessageKey.of(id.getUser(), id.getId()),
         serial,
         LocalDateTime.parse(time),
         text);
   }
 
-  static MessageTo from(Message message)
+  static MessageTo from(UUID chatRoomId, Message message)
   {
     return
         new MessageTo(
-             message.getUsername() + "--" + message.getId(),
+            new MessageId(
+                chatRoomId.toString(),
+                message.getUsername(),
+                message.getId()),
             message.getSerialNumber(),
             message.getTimestamp().toString(),
             message.getMessageText());
index 8a172c1..c7698e8 100644 (file)
@@ -61,8 +61,8 @@ public class MongoDbStorageStrategy implements StorageStrategy
   public void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux)
   {
     messageFlux
-        .map(message -> MessageTo.from(message))
-        .subscribe(messageTo -> messageRepository.save(messageTo)); // TODO: Namespace <chatRoomId>
+        .map(message -> MessageTo.from(chatRoomId, message))
+        .subscribe(messageTo -> messageRepository.save(messageTo));
   }
 
   @Override
diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageToTest.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageToTest.java
deleted file mode 100644 (file)
index 33a8a50..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-package de.juplo.kafka.chat.backend.persistence.storage.mongodb;
-
-import de.juplo.kafka.chat.backend.domain.Message;
-import org.junit.jupiter.api.Test;
-
-import java.time.LocalDateTime;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-public class MessageToTest
-{
-  @Test
-  void testFrom()
-  {
-    Message message = new Message(
-        Message.MessageKey.of("ute", 1l),
-        6l,
-        LocalDateTime.now(),
-        "foo");
-    MessageTo messageTo = MessageTo.from(message);
-    assertThat(messageTo.getId()).isEqualTo("ute--1");
-  }
-
-  @Test
-  void testToMessage()
-  {
-    MessageTo messageTo = new MessageTo(
-        "ute--1",
-        6l,
-        LocalDateTime.now().toString(),
-        "foo");
-    Message message = messageTo.toMessage();
-    assertThat(message.getId()).isEqualTo(1l);
-    assertThat(message.getUsername()).isEqualTo("ute");
-  }
-}