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;
@ToString(of = { "id", "name" })
public class ChatRoomTo
{
- private UUID id;
+ @Id
+ private String id;
private String name;
+ @DBRef
private List<MessageTo> messages;
public static ChatRoomTo from(ChatRoom chatroom)
{
return new ChatRoomTo(
- chatroom.getId(),
+ chatroom.getId().toString(),
chatroom.getName(),
chatroom
.getMessages()
import lombok.*;
import java.time.LocalDateTime;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@AllArgsConstructor
@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)