Skip to content

Commit

Permalink
read flag business event
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 22, 2023
1 parent 5be0531 commit ab7620f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import lombok.RequiredArgsConstructor;
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.domain.model.Message;
import net.dancier.chatdancer.application.port.out.SendChatCreatedEventDto;
import net.dancier.chatdancer.application.port.out.SendChatCreatedEventPort;
import net.dancier.chatdancer.application.port.out.SendMessageCreatedEventPort;
import net.dancier.chatdancer.application.port.out.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
Expand All @@ -17,11 +15,13 @@

@RequiredArgsConstructor
@Component
public class ScheduleMessagesAdapter implements SendChatCreatedEventPort, SendMessageCreatedEventPort {
public class ScheduleMessagesAdapter implements
SendChatCreatedEventPort,
SendMessageCreatedEventPort,
SendReadFlagUpdatedEventPort
{
private static final Logger log = LoggerFactory.getLogger(ScheduleMessagesAdapter.class);
private static final String CHAT_CREATED_SOURCE = "http://chat-dancer.dancier.net/chat-created";

private static final String MESSAGE_CREATED_SOURCE = "http://chat-dancer.dancier.net/message-created";
private static final String SOURCE = "http://chat-dancer.dancier.net";

private final OutboxJpaRepository outboxJpaRepository;

Expand All @@ -37,7 +37,7 @@ public void send(SendChatCreatedEventDto sendChatCreatedEventDto) throws JsonPro
outboxJpaEntity.setCreatedAt(OffsetDateTime.now());
outboxJpaEntity.setStatus(OutboxJpaEntity.STATUS.NEW);
outboxJpaEntity.setKey(sendChatCreatedEventDto.getChatId().toString());
outboxJpaEntity.setSource(CHAT_CREATED_SOURCE);
outboxJpaEntity.setSource(SOURCE);
outboxJpaRepository.save(outboxJpaEntity);
}

Expand All @@ -59,7 +59,20 @@ public void send(Chat chat, Message message) throws JsonProcessingException {
outboxJpaEntity.setType(TopicNames.MESSAGE_POSTED);
outboxJpaEntity.setCreatedAt(OffsetDateTime.now());
outboxJpaEntity.setStatus(OutboxJpaEntity.STATUS.NEW);
outboxJpaEntity.setSource(MESSAGE_CREATED_SOURCE);
outboxJpaEntity.setSource(SOURCE);
outboxJpaRepository.save(outboxJpaEntity);
}

@Override
public void send(SendReadFlagUpdatedEventDto sendReadFlagUpdatedEventDto) throws JsonProcessingException {
String data = objectMapper.writeValueAsString(sendReadFlagUpdatedEventDto);
OutboxJpaEntity outboxJpaEntity = new OutboxJpaEntity();
outboxJpaEntity.setKey(sendReadFlagUpdatedEventDto.getReaderId());
outboxJpaEntity.setData(data);
outboxJpaEntity.setType(TopicNames.MESSAGE_READ);
outboxJpaEntity.setCreatedAt(OffsetDateTime.now());
outboxJpaEntity.setStatus(OutboxJpaEntity.STATUS.NEW);
outboxJpaEntity.setSource(SOURCE);
outboxJpaRepository.save(outboxJpaEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public void addReadBy(Chat.ParticipantId participantId) {
this.readBy.add(participantId);
}

public void removeReadBy(Chat.ParticipantId participantId) {
this.readBy.remove(participantId);
}

@Value
public static class MessageId {
private UUID value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package net.dancier.chatdancer.application.domain.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.domain.model.Message;
import net.dancier.chatdancer.application.exception.ApplicationException;
import net.dancier.chatdancer.application.port.in.SetReadFlagUseCase;
import net.dancier.chatdancer.application.port.out.MessageAndChatIdDto;
import net.dancier.chatdancer.application.port.out.MessagePort;
import net.dancier.chatdancer.application.port.out.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class SetReadFlagService implements SetReadFlagUseCase {

private static final Logger log = LoggerFactory.getLogger(SetReadFlagService.class);

private final MessagePort messagePort;
private final SendReadFlagUpdatedEventPort sendReadFlagUpdatedEventPort;

@Override
public void setReadFlag(Message.MessageId messageId, Chat.ParticipantId participantId, Boolean read) {

MessageAndChatIdDto messageAndChatIdDto = messagePort.loadMessage(messageId);
Message message = messageAndChatIdDto.getMessage();
if (read) {
Expand All @@ -26,5 +33,16 @@ public void setReadFlag(Message.MessageId messageId, Chat.ParticipantId particip
}
}
messagePort.update(messageAndChatIdDto);

SendReadFlagUpdatedEventDto sendReadFlagUpdatedEventDto = new SendReadFlagUpdatedEventDto();
sendReadFlagUpdatedEventDto.setMessageId(messageId.getValue().toString());
sendReadFlagUpdatedEventDto.setReaderId(participantId.getId());
sendReadFlagUpdatedEventDto.setRead(read);
try {
sendReadFlagUpdatedEventPort.send(sendReadFlagUpdatedEventDto);
} catch (JsonProcessingException jpe) {
log.error("Unable to send setReadFlag Event." + jpe);
throw new ApplicationException("Unable to process.", jpe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface SendMessageCreatedEventPort {

public void send(Chat chat, Message message) throws JsonProcessingException;
void send(Chat chat, Message message) throws JsonProcessingException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.dancier.chatdancer.application.port.out;

import lombok.Data;

import java.util.Set;

@Data
public class SendReadFlagUpdatedEventDto {

String messageId;
Boolean read;
String readerId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.dancier.chatdancer.application.port.out;

import com.fasterxml.jackson.core.JsonProcessingException;

public interface SendReadFlagUpdatedEventPort {

void send(SendReadFlagUpdatedEventDto sendReadFlagUpdatedEventDto) throws JsonProcessingException;

}

0 comments on commit ab7620f

Please sign in to comment.