Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
Marc Gorzala committed Dec 20, 2023
1 parent ee7e16d commit 3559244
Showing 14 changed files with 77 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ public ResponseEntity postChatMessage(@PathVariable UUID chatId, @Validated @Req
postChatMessageRequestDto.getText(),
new Message.AuthorId(postChatMessageRequestDto.getAuthorId()),
new Chat.ChatId((chatId)));
createChatMessageUseCase.post(createChatMessageCommand);
createChatMessageUseCase.create(createChatMessageCommand);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -19,15 +19,14 @@ public class OutboxJpaEntity {
@GeneratedValue
private UUID id;

private String topic;
private String type;

@JdbcTypeCode(SqlTypes.JSON)
private String metaData;
private String source;

private String key;

@JdbcTypeCode(SqlTypes.JSON)
private String payload;
private String data;

private OffsetDateTime createdAt;

Original file line number Diff line number Diff line change
@@ -15,24 +15,26 @@
@Component
public class ScheduleMessagesAdapter implements SendChatCreatedEventPort {
private static final Logger log = LoggerFactory.getLogger(ScheduleMessagesAdapter.class);
private static final String CHAT_CREATED_SOURCE = "http://chat-dancer.dancier.net/chat-created";

private final OutboxJpaRepository outboxJpaRepository;

private final ObjectMapper objectMapper;

@Override
public void send(SendChatCreatedEventDto sendChatCreatedEventDto) throws JsonProcessingException {
log.info("scheduling the sending of the message in database: " + sendChatCreatedEventDto);
String payload = objectMapper.writeValueAsString(sendChatCreatedEventDto);
log.info("Scheduling Business Event for: " + sendChatCreatedEventDto);
String data = objectMapper.writeValueAsString(sendChatCreatedEventDto);
OutboxJpaEntity outboxJpaEntity = new OutboxJpaEntity();
outboxJpaEntity.setMetaData(payload);
if (sendChatCreatedEventDto.getChatId() != null) {
outboxJpaEntity.setKey(sendChatCreatedEventDto.getChatId().toString());
}
outboxJpaEntity.setPayload(payload);
outboxJpaEntity.setTopic("sccd");
outboxJpaEntity.setData(data);
outboxJpaEntity.setType(TopicNames.CHAT_CREATED);
outboxJpaEntity.setCreatedAt(OffsetDateTime.now());
outboxJpaEntity.setStatus(OutboxJpaEntity.STATUS.NEW);
outboxJpaEntity.setKey(sendChatCreatedEventDto.getChatId().toString());
outboxJpaEntity.setSource(CHAT_CREATED_SOURCE);
outboxJpaRepository.save(outboxJpaEntity);
}

Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

@RequiredArgsConstructor
@@ -24,15 +23,14 @@ public class SendOutboxService {

private final ObjectMapper objectMapper;

public void send(OutboxJpaEntity entity) throws UnsupportedEncodingException, JsonProcessingException {
public void send(OutboxJpaEntity entity) throws JsonProcessingException {
CloudEvent cloudEvent = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withSource(URI.create("https://chat-dancer.dancier.net"))
.withType("bla")
.withData(objectMapper.writeValueAsBytes(entity.getPayload()))
.withExtension("foo", "bar").build();
log.info("This is the cloud event: " + cloudEvent);
kafkaTemplate.send(entity.getTopic(),entity.getKey(), cloudEvent);
.withSource(URI.create(entity.getSource()))
.withType(entity.getType())
.withData(objectMapper.writeValueAsBytes(entity.getData()))
.build();
kafkaTemplate.send(entity.getType(),entity.getKey(), cloudEvent);
}

}
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.port.in.ChatsByParticipantQuery;
import net.dancier.chatdancer.application.port.out.ChatsByParticipantPort;
import net.dancier.chatdancer.application.port.out.LoadChatPort;
import net.dancier.chatdancer.application.port.out.GetChatPort;
import net.dancier.chatdancer.application.port.out.UpdateChatPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,7 +15,7 @@

@Component
@AllArgsConstructor
public class ChatPersistenceAdapter implements LoadChatPort, UpdateChatPort, ChatsByParticipantPort {
public class ChatPersistenceAdapter implements GetChatPort, UpdateChatPort, ChatsByParticipantPort {

public static final Logger log = LoggerFactory.getLogger(ChatPersistenceAdapter.class);

@@ -24,7 +24,7 @@ public class ChatPersistenceAdapter implements LoadChatPort, UpdateChatPort, Cha
private final ChatMapper chatMapper;

@Override
public Chat loadChat(Chat.ChatId chatId) {
public Chat get(Chat.ChatId chatId) {
ChatJpaEntity jpaChatEntity = chatJpaRepository.findById(chatId.getId()).orElseThrow();
log.info("got: " + jpaChatEntity);
return chatMapper.fromJpaChatEntityToChat(jpaChatEntity);
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
import net.dancier.chatdancer.application.domain.model.Message;
import net.dancier.chatdancer.application.port.in.CreateChatMessageCommand;
import net.dancier.chatdancer.application.port.in.CreateChatMessageUseCase;
import net.dancier.chatdancer.application.port.out.LoadChatPort;
import net.dancier.chatdancer.application.port.out.GetChatPort;
import net.dancier.chatdancer.application.port.out.UpdateChatPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -17,15 +17,15 @@
public class CreateChatMessageService implements CreateChatMessageUseCase {
Logger log = LoggerFactory.getLogger(CreateChatMessageService.class);

private final LoadChatPort loadChatPort;
private final GetChatPort getChatPort;

private final UpdateChatPort updateChatPort;


@Override
public void post(CreateChatMessageCommand command) {
public void create(CreateChatMessageCommand command) {
log.info("Posting: " + command);
Chat chat = loadChatPort.loadChat(command.chatId());
Chat chat = getChatPort.get(command.chatId());
Message message = Message.withoutId(command.text(),command.authorId());
chat.addMessage(message);
updateChatPort.updateChat(chat);
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.exception.ApplicationException;
import net.dancier.chatdancer.application.port.in.CreateChatCommand;
import net.dancier.chatdancer.application.port.in.CreateChatUseCase;
import net.dancier.chatdancer.application.port.out.SendChatCreatedEventDto;
@@ -28,19 +29,22 @@ public class CreateChatService implements CreateChatUseCase {
@Override
@Transactional
public Chat.ChatId createChat(CreateChatCommand createChatCommand) {
System.out.println("Creating Chat");
Chat chat = Chat.withoutId(LocalDateTime.now());
createChatCommand.participants().forEach(p -> chat.addParticipant(p));
createChatCommand.participants()
.forEach(
participantId -> chat.addParticipant(participantId)
);

Chat.ChatId chatId = updateChatPort.updateChat(chat);

SendChatCreatedEventDto sendChatCreatedEventDto = new SendChatCreatedEventDto();

sendChatCreatedEventDto.setChatId(chatId.getId());
sendChatCreatedEventDto.setParticipantIds(createChatCommand.participants());
try {
sendChatCreatedEventPort.send(sendChatCreatedEventDto);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (JsonProcessingException jpe) {
log.error("Permanent Error: " + jpe);
throw new ApplicationException("Unable to serialize..", jpe);
}
return chatId;
}
Original file line number Diff line number Diff line change
@@ -3,17 +3,17 @@
import lombok.RequiredArgsConstructor;
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.port.in.GetChatUseCase;
import net.dancier.chatdancer.application.port.out.LoadChatPort;
import net.dancier.chatdancer.application.port.out.GetChatPort;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class GetChatService implements GetChatUseCase {

private final LoadChatPort loadChatPort;
private final GetChatPort getChatPort;

@Override
public Chat get(Chat.ChatId id) {
return loadChatPort.loadChat(id);
return getChatPort.get(id);
}
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import net.dancier.chatdancer.application.domain.model.Chat;
import net.dancier.chatdancer.application.domain.model.Message;
import net.dancier.chatdancer.application.port.in.GetMessagesByChatUseCase;
import net.dancier.chatdancer.application.port.out.LoadChatPort;
import net.dancier.chatdancer.application.port.out.GetChatPort;
import org.springframework.stereotype.Component;

import java.util.List;
@@ -13,10 +13,10 @@
@Component
public class GetMessagesByChatService implements GetMessagesByChatUseCase {

private final LoadChatPort loadChatPort;
private final GetChatPort getChatPort;

@Override
public List<Message> byChatId(Chat.ChatId chatId) {
return loadChatPort.loadChat(chatId).getMessages();
return getChatPort.get(chatId).getMessages();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.dancier.chatdancer.application.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class ApplicationException extends RuntimeException{
public ApplicationException(String message) {
super(message);
}

public ApplicationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@

public interface CreateChatMessageUseCase {

void post(CreateChatMessageCommand command);
void create(CreateChatMessageCommand command);

}
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@

import net.dancier.chatdancer.application.domain.model.Chat;

public interface LoadChatPort {
public interface GetChatPort {

Chat loadChat(Chat.ChatId chatId);
Chat get(Chat.ChatId chatId);

}

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/resources/liquibase-changeLog.xml
Original file line number Diff line number Diff line change
@@ -57,4 +57,24 @@
ALTER COLUMN author_id TYPE VARCHAR(256);
</sql>
</changeSet>
<changeSet id="2023-12-20-alter-table-outbox" author="Marc Gorzala">
<sql>
ALTER TABLE outbox
RENAME payload TO data;
ALTER TABLE outbox
DROP COLUMN meta_data;
ALTER TABLE outbox
ADD COLUMN source VARCHAR(256);
ALTER TABLE outbox
ADD COLUMN type VARCHAR(256);
</sql>
</changeSet>
<changeSet id="2023-12-20-drop-type-rename-topic" author="Marc Gorzala">
<sql>
ALTER TABLE outbox
DROP COLUMN type;
ALTER TABLE outbox
RENAME topic TO type;
</sql>
</changeSet>
</databaseChangeLog>

0 comments on commit 3559244

Please sign in to comment.