From c3d8ff110b30df7013d9b9c8440c3591766ba072 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 11 Jun 2021 18:17:38 +0200 Subject: [PATCH] WIP --- .../transfer/TransferServiceApplication.java | 4 +++- .../transfer/controller/TransferController.java | 12 +++++++++++- .../transfer/controller/TransferRequest.java | 3 +-- .../kafka/payment/transfer/domain/Transfer.java | 4 ++++ .../payment/transfer/domain/TransferRepository.java | 2 +- .../payment/transfer/domain/TransferService.java | 13 ++++++++++--- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java index 80f1d4e..cc3e056 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java @@ -2,6 +2,7 @@ package de.juplo.kafka.payment.transfer; import com.fasterxml.jackson.databind.ObjectMapper; +import de.juplo.kafka.payment.transfer.domain.TransferRepository; import de.juplo.kafka.payment.transfer.domain.TransferService; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.KafkaProducer; @@ -35,11 +36,12 @@ public class TransferServiceApplication @Bean TransferService transferService( + TransferRepository repository, KafkaProducer producer, ObjectMapper mapper, TransferServiceProperties properties) { - return new TransferService(producer, mapper, properties.topic); + return new TransferService(repository, producer, mapper, properties.topic); } diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java index 12547ef..d8dcc83 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java @@ -31,7 +31,17 @@ public class TransferController produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity transfer(@Valid @RequestBody TransferRequest transferRequest) { - service.initiate(transferRequest); + Transfer transfer = + Transfer + .builder() + .id(transferRequest.getId()) + .payer(transferRequest.getPayer()) + .payee(transferRequest.getPayee()) + .amount(transferRequest.getAmount()) + .build(); + + service.initiate(transfer); + return ResponseEntity.created(URI.create(PATH + transferRequest.getId())).build(); } } diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java index db3c781..43b4f90 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java @@ -3,7 +3,6 @@ package de.juplo.kafka.payment.transfer.controller; import lombok.Data; import javax.validation.constraints.NotNull; -import java.util.UUID; /** @@ -13,7 +12,7 @@ import java.util.UUID; public class TransferRequest { @NotNull(message = "Cannot be null") - private UUID id; + private long id; @NotNull(message = "Cannot be null") private long payer; @NotNull(message = "Cannot be null") diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java index 17330f6..2aa87fe 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java @@ -1,14 +1,18 @@ package de.juplo.kafka.payment.transfer.domain; +import lombok.Builder; import lombok.Data; @Data +@Builder public class Transfer { public enum State { + CREATED, + FAILED, PENDING, APPROVED, REJECTED diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferRepository.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferRepository.java index ac37192..908caa1 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferRepository.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferRepository.java @@ -9,5 +9,5 @@ import java.util.UUID; public interface TransferRepository { void put(Transfer transfer); - Optional get(UUID uuid); + Optional get(Long id); } diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java index 34ef29c..6545e29 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java @@ -21,19 +21,26 @@ public class TransferService private final ObjectMapper mapper; private final String topic; - public void initiate(Transfer transfer) + public synchronized void initiate(Transfer transfer) { repository .get(transfer.getId()) .ifPresentOrElse( stored -> { - + switch (stored.getState()) + { + case FAILED: + } }, () -> { - }); + } + + + private void create(Transfer transfer) + { try { ProducerRecord record = -- 2.20.1