From: Kai Moritz Date: Sat, 19 Jun 2021 12:51:25 +0000 (+0200) Subject: Moved TransferService into package ports X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=0d1ffab6d4ff4a1d401a4b0b16bc30e0746a9441;p=demos%2Fkafka%2Fdemos-kafka-payment-system-transfer Moved TransferService into package ports --- diff --git a/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java b/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java index 557b857..7e90c57 100644 --- a/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java +++ b/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java @@ -7,8 +7,8 @@ import de.juplo.kafka.payment.transfer.adapter.NoOpMessageService; import de.juplo.kafka.payment.transfer.adapter.TransferConsumer; import de.juplo.kafka.payment.transfer.adapter.TransferController; import de.juplo.kafka.payment.transfer.domain.Transfer; -import de.juplo.kafka.payment.transfer.domain.TransferService; import de.juplo.kafka.payment.transfer.ports.TransferRepository; +import de.juplo.kafka.payment.transfer.ports.TransferService; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.KafkaConsumer; diff --git a/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java b/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java deleted file mode 100644 index ed1a98e..0000000 --- a/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java +++ /dev/null @@ -1,91 +0,0 @@ -package de.juplo.kafka.payment.transfer.domain; - - -import de.juplo.kafka.payment.transfer.ports.*; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -import java.util.Optional; - -import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CHECKED; -import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CREATED; - - -@Slf4j -@RequiredArgsConstructor -public class TransferService implements CreateTransferUseCase, HandleStateChangeUseCase, GetTransferUseCase -{ - private final TransferRepository repository; - private final MessagingService messagingService; - - @Override - public void create(Long id, Long payer, Long payee, Integer amount) - { - repository - .get(id) - .ifPresentOrElse( - 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 handleStateChange(Long id, Transfer.State 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) - { - // TODO: Do some time consuming checks... - messagingService.send(transfer.getId(), CHECKED); - } - - public Optional get(Long id) - { - return repository.get(id); - } -} diff --git a/src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java b/src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java new file mode 100644 index 0000000..d4eb3ef --- /dev/null +++ b/src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java @@ -0,0 +1,91 @@ +package de.juplo.kafka.payment.transfer.ports; + + +import de.juplo.kafka.payment.transfer.domain.Transfer; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.Optional; + +import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CHECKED; +import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CREATED; + + +@Slf4j +@RequiredArgsConstructor +public class TransferService implements CreateTransferUseCase, HandleStateChangeUseCase, GetTransferUseCase +{ + private final TransferRepository repository; + private final MessagingService messagingService; + + @Override + public void create(Long id, Long payer, Long payee, Integer amount) + { + repository + .get(id) + .ifPresentOrElse( + 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 handleStateChange(Long id, Transfer.State 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) + { + // TODO: Do some time consuming checks... + messagingService.send(transfer.getId(), CHECKED); + } + + public Optional get(Long id) + { + return repository.get(id); + } +}