From: Kai Moritz Date: Tue, 5 Sep 2023 18:37:12 +0000 (+0200) Subject: refactor: Renamed `ChatRoom` into `ChatRoomData` - Aligned Code X-Git-Tag: rebase--2023-09-05--23-53~6 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=57c1e76bfc7ea5b1486ae5cc30693cd68ab88cbc;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/ChatRoomTo.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/ChatRoomTo.java index 58f5bce5..0086053e 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/ChatRoomTo.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/ChatRoomTo.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 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 index 00000000..d58ba5e0 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageId.java @@ -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; +} 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 4f93695c..45dd50a3 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 @@ -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()); 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 8a172c16..c7698e89 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 @@ -61,8 +61,8 @@ public class MongoDbStorageStrategy implements StorageStrategy public void writeChatRoomData(UUID chatRoomId, Flux messageFlux) { messageFlux - .map(message -> MessageTo.from(message)) - .subscribe(messageTo -> messageRepository.save(messageTo)); // TODO: Namespace + .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 index 33a8a503..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MessageToTest.java +++ /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"); - } -}