mapper,
new TransferConsumer.ConsumerUseCases() {
@Override
- public void create(Transfer transfer)
+ public void create(Long id, Long payer, Long payee, Integer amount)
{
- productionTransferService.create(transfer);
+ productionTransferService.create(id, payer, payee, amount);
}
@Override
}
@Override
- public void handle(Transfer transfer)
+ public void handleStateChange(Long id, Transfer.State state)
{
- productionTransferService.handle(transfer);
+ productionTransferService.handleStateChange(id, state);
}
},
new TransferConsumer.ConsumerUseCases() {
@Override
- public void create(Transfer transfer)
+ public void create(Long id, Long payer, Long payee, Integer amount)
{
- restoreTransferService.create(transfer);
+ restoreTransferService.create(id, payer, payee, amount);
}
@Override
}
@Override
- public void handle(Transfer transfer)
+ public void handleStateChange(Long id, Transfer.State state)
{
- restoreTransferService.handle(transfer);
+ restoreTransferService.handleStateChange(id, state);
}
});
}
private Long payee;
private Integer amount;
- public Transfer toTransfer()
- {
- return
- Transfer
- .builder()
- .id(id)
- .payer(payer)
- .payee(payee)
- .amount(amount)
- .build();
- }
-
public static NewTransferEvent ofTransfer(Transfer transfer)
{
return
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.juplo.kafka.payment.transfer.domain.Transfer;
import de.juplo.kafka.payment.transfer.ports.CreateTransferUseCase;
import de.juplo.kafka.payment.transfer.ports.GetTransferUseCase;
import de.juplo.kafka.payment.transfer.ports.HandleStateChangeUseCase;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
-import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CREATED;
-
@RequestMapping("/consumer")
@ResponseBody
NewTransferEvent newTransferEvent =
mapper.readValue(record.value(), NewTransferEvent.class);
- useCases.create(newTransferEvent.toTransfer().setState(CREATED));
+ useCases
+ .create(
+ newTransferEvent.getId(),
+ newTransferEvent.getPayer(),
+ newTransferEvent.getPayee(),
+ newTransferEvent.getAmount());
break;
case EventType.TRANSFER_STATE_CHANGED:
TransferStateChangedEvent stateChangedEvent =
mapper.readValue(record.value(), TransferStateChangedEvent.class);
- useCases
- .get(stateChangedEvent.getId())
- .ifPresentOrElse(
- transfer -> useCases.handle(transfer.setState(stateChangedEvent.getState())),
- () -> log.error("unknown transfer: {}", stateChangedEvent.getId()));
+ useCases.handleStateChange(stateChangedEvent.getId(), stateChangedEvent.getState());
break;
}
}
private final MessagingService messagingService;
@Override
- public void create(Transfer transfer)
+ public void create(Long id, Long payer, Long payee, Integer amount)
{
repository
- .get(transfer.getId())
+ .get(id)
.ifPresentOrElse(
- stored -> log.info("transfer already exisits: {}, ignoring: {}", stored, transfer),
+ stored -> log.info(
+ "transfer already exisits: {}, ignoring: id={}, payer={}, payee={}, amount={}",
+ stored,
+ payer,
+ payee,
+ amount),
() ->
{
+ Transfer transfer =
+ Transfer
+ .builder()
+ .id(id)
+ .payer(payer)
+ .payee(payee)
+ .amount(amount)
+ .build();
+
log.info("creating transfer: {}", transfer);
repository.store(transfer);
messagingService.send(transfer.getId(), CREATED);
}
@Override
- public void handle(Transfer transfer)
+ public void handleStateChange(Long id, Transfer.State state)
{
- Transfer.State state = transfer.getState();
- switch (state)
- {
- case CREATED:
- repository.store(transfer);
- check(transfer);
- break;
-
- case CHECKED:
- repository.store(transfer);
- // TODO: What's next...?
- break;
-
- default:
- log.warn("TODO: handle {} state {}", state.foreign ? "foreign" : "domain", state);
- }
+ get(id)
+ .ifPresentOrElse(
+ transfer ->
+ {
+ switch (state)
+ {
+ case CREATED:
+
+ transfer.setState(CREATED);
+ repository.store(transfer);
+ check(transfer);
+ break;
+
+ case CHECKED:
+
+ transfer.setState(CHECKED);
+ repository.store(transfer);
+ // TODO: What's next...?
+ break;
+
+ default:
+
+ log.warn("TODO: handle {} state {}", state.foreign ? "foreign" : "domain", state);
+ }
+ },
+ () -> log.error("unknown transfer: {}", id));
}
private void check(Transfer transfer)
public interface CreateTransferUseCase
{
- void create(Transfer transfer);
+ void create(Long id, Long payer, Long payee, Integer amount);
}
public interface HandleStateChangeUseCase
{
- void handle(Transfer transfer);
+ void handleStateChange(Long id, Transfer.State state);
}