mapper,
new TransferConsumer.ConsumerUseCases() {
@Override
- public void create(Long id, Long payer, Long payee, Integer amount)
+ public TransferStateChangedEvent create(Long id, Long payer, Long payee, Integer amount)
{
- productionTransferService.create(id, payer, payee, amount);
+ return productionTransferService.create(id, payer, payee, amount);
}
@Override
}
@Override
- public void handleStateChange(Long id, Transfer.State state)
+ public TransferStateChangedEvent handleStateChange(
+ TransferStateChangedEvent stateChangedEvent)
{
- productionTransferService.handleStateChange(id, state);
+ return productionTransferService.handleStateChange(stateChangedEvent);
}
},
new TransferConsumer.ConsumerUseCases() {
@Override
- public void create(Long id, Long payer, Long payee, Integer amount)
+ public TransferStateChangedEvent create(Long id, Long payer, Long payee, Integer amount)
{
- restoreTransferService.create(id, payer, payee, amount);
+ return restoreTransferService.create(id, payer, payee, amount);
}
@Override
}
@Override
- public void handleStateChange(Long id, Transfer.State state)
+ public TransferStateChangedEvent handleStateChange(
+ TransferStateChangedEvent stateChangedEvent)
{
- restoreTransferService.handleStateChange(id, state);
+ return restoreTransferService.handleStateChange(stateChangedEvent);
}
});
}
TransferStateChangedEvent stateChangedEvent =
mapper.readValue(record.value(), TransferStateChangedEvent.class);
- useCases.handleStateChange(stateChangedEvent.getId(), stateChangedEvent.getState());
+ useCases.handleStateChange(stateChangedEvent);
break;
}
}
package de.juplo.kafka.payment.transfer.ports;
-import de.juplo.kafka.payment.transfer.domain.Transfer;
+import de.juplo.kafka.payment.transfer.adapter.TransferStateChangedEvent;
+
+import java.util.Optional;
public interface CreateTransferUseCase
{
- void create(Long id, Long payer, Long payee, Integer amount);
+ Optional<TransferStateChangedEvent> create(Long id, Long payer, Long payee, Integer amount);
}
package de.juplo.kafka.payment.transfer.ports;
-import de.juplo.kafka.payment.transfer.domain.Transfer;
+import de.juplo.kafka.payment.transfer.adapter.TransferStateChangedEvent;
public interface HandleStateChangeUseCase
{
- void handleStateChange(Long id, Transfer.State state);
+ TransferStateChangedEvent handleStateChange(TransferStateChangedEvent stateChangedEvent);
}
package de.juplo.kafka.payment.transfer.ports;
+import de.juplo.kafka.payment.transfer.adapter.TransferStateChangedEvent;
import de.juplo.kafka.payment.transfer.domain.Transfer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
private final MessagingService messagingService;
@Override
- public void create(Long id, Long payer, Long payee, Integer amount)
+ public Optional<TransferStateChangedEvent> create(Long id, Long payer, Long payee, Integer amount)
{
+ return
repository
.get(id)
- .ifPresentOrElse(
- stored -> log.info(
+ .flatMap(
+ stored ->
+ {log.info(
"transfer already exisits: {}, ignoring: id={}, payer={}, payee={}, amount={}",
stored,
payer,
payee,
- amount),
- () ->
+ amount);
+ return Optional.empty();
+ })
+ .or(() ->
{
- Transfer transfer =
- Transfer
+ log.info("creating transfer: {}", transfer);
+ return
+ TransferStateChangedEvent
.builder()
- .id(id)
- .payer(payer)
- .payee(payee)
- .amount(amount)
+ .id(transfer.getId())
+ .state(CREATED)
.build();
-
- log.info("creating transfer: {}", transfer);
- repository.store(transfer);
- messagingService.send(transfer.getId(), CREATED);
});
}
@Override
- public void handleStateChange(Long id, Transfer.State state)
+ public TransferStateChangedEvent handleStateChange(Long id, Transfer.State state)
{
get(id)
.ifPresentOrElse(