From: Kai Moritz Date: Mon, 9 Jan 2023 22:21:50 +0000 (+0100) Subject: WIP X-Git-Tag: TEST~11 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=7397955b4b8b752d0ce0a40ce74caf3e0bb42e15;p=demos%2Fkafka%2Fchat WIP --- 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 da33a5bf..701998ef 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 @@ -2,6 +2,8 @@ package de.juplo.kafka.chat.backend.persistence.storage.mongodb; import de.juplo.kafka.chat.backend.domain.ChatRoom; import lombok.*; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.DBRef; import java.util.List; import java.util.UUID; @@ -15,15 +17,17 @@ import java.util.UUID; @ToString(of = { "id", "name" }) public class ChatRoomTo { - private UUID id; + @Id + private String id; private String name; + @DBRef private List messages; public static ChatRoomTo from(ChatRoom chatroom) { return new ChatRoomTo( - chatroom.getId(), + chatroom.getId().toString(), chatroom.getName(), chatroom .getMessages() 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 7af18457..1eef2739 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 @@ -4,6 +4,9 @@ import de.juplo.kafka.chat.backend.domain.Message; import lombok.*; import java.time.LocalDateTime; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @AllArgsConstructor @@ -14,15 +17,20 @@ import java.time.LocalDateTime; @ToString(of = { "user", "id" }) class MessageTo { - private Long id; + final static Pattern SPLIT_ID = Pattern.compile("^([a-z-]+)--([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$"); + private String id; private Long serial; private LocalDateTime time; - private String user; private String text; Message toMessage() { - return new Message(Message.MessageKey.of(user, id), serial, time, text); + Matcher matcher = SPLIT_ID.matcher(id); + if (!matcher.matches()) + throw new RuntimeException("MessageTo with invalid ID: " + id); + UUID uuid = UUID.fromString(matcher.group(2)); + String user = matcher.group(1); + return new Message(Message.MessageKey.of(user, uuid), serial, time, text); } static MessageTo from(Message message)