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
@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());
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
+++ /dev/null
-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");
- }
-}