From 7397955b4b8b752d0ce0a40ce74caf3e0bb42e15 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 9 Jan 2023 23:21:50 +0100 Subject: [PATCH] WIP --- .../persistence/storage/mongodb/ChatRoomTo.java | 8 ++++++-- .../persistence/storage/mongodb/MessageTo.java | 14 +++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) 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) -- 2.20.1