Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Mapper and other Events #4

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.sample.schulung.accounts.kafka;

import de.sample.schulung.accounts.domain.Customer;
import de.sample.schulung.accounts.domain.Customer.CustomerState;
import de.sample.schulung.accounts.domain.events.CustomerCreatedEvent;
import de.sample.schulung.accounts.domain.events.CustomerDeletedEvent;
import de.sample.schulung.accounts.domain.events.CustomerReplacedEvent;
import org.springframework.stereotype.Component;

@Component
public class CustomerEventRecordMapper {

public String map(CustomerState state) {
return switch (state) {
case ACTIVE -> "active";
case LOCKED -> "locked";
case DISABLED -> "disabled";
};
}

public CustomerRecord map(Customer customer) {
return new CustomerRecord(
customer.getName(),
customer.getDateOfBirth(),
this.map(customer.getState())
);
}

public CustomerEventRecord map(CustomerCreatedEvent event) {
var customer = event.customer();
return new CustomerEventRecord(
"created",
customer.getUuid(),
this.map(customer)
);
}

public CustomerEventRecord map(CustomerReplacedEvent event) {
var customer = event.customer();
return new CustomerEventRecord(
"replaced",
customer.getUuid(),
this.map(customer)
);
}

public CustomerEventRecord map(CustomerDeletedEvent event) {
return new CustomerEventRecord(
"deleted",
event.uuid(),
null
);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.sample.schulung.accounts.kafka;

import de.sample.schulung.accounts.domain.events.CustomerCreatedEvent;
import de.sample.schulung.accounts.domain.events.CustomerDeletedEvent;
import de.sample.schulung.accounts.domain.events.CustomerReplacedEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.kafka.core.KafkaTemplate;
Expand All @@ -13,24 +15,24 @@
public class CustomerEventsProducer {

private final KafkaTemplate<UUID, Object> kafkaTemplate;
private final CustomerEventRecordMapper mapper;

@EventListener
public void handleCustomerCreatedEvent(CustomerCreatedEvent event) {
// map event to record
var customer = event.customer();
var payload = new CustomerEventRecord(
"created",
customer.getUuid(),
new CustomerRecord(
customer.getName(),
customer.getDateOfBirth(),
switch(customer.getState()) {
case ACTIVE -> "active";
case LOCKED -> "locked";
case DISABLED -> "disabled";
}
)
var payload = this.mapper.map(event);
// send message
kafkaTemplate.send(
KafkaConstants.CUSTOMER_EVENTS_TOPIC,
event.customer().getUuid(),
payload
);
}

@EventListener
public void handleCustomerReplacedEvent(CustomerReplacedEvent event) {
// map event to record
var payload = this.mapper.map(event);
// send message
kafkaTemplate.send(
KafkaConstants.CUSTOMER_EVENTS_TOPIC,
Expand All @@ -39,4 +41,16 @@ public void handleCustomerCreatedEvent(CustomerCreatedEvent event) {
);
}

@EventListener
public void handleCustomerDeletedEvent(CustomerDeletedEvent event) {
// map event to record
var payload = this.mapper.map(event);
// send message
kafkaTemplate.send(
KafkaConstants.CUSTOMER_EVENTS_TOPIC,
event.uuid(),
payload
);
}

}
Loading