From: Kai Moritz Date: Fri, 11 Jun 2021 15:48:29 +0000 (+0200) Subject: WIP X-Git-Tag: wip-initialer-commit~11 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=5bffe60126c5fa74bff59c91dfbd722d291b1c93;p=demos%2Fkafka%2Fdemos-kafka-payment-system-transfer WIP --- 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 49e57a3..80f1d4e 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,7 +2,7 @@ package de.juplo.kafka.payment.transfer; import com.fasterxml.jackson.databind.ObjectMapper; -import de.juplo.kafka.payment.transfer.impl.TransferServiceImpl; +import de.juplo.kafka.payment.transfer.domain.TransferService; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; @@ -34,12 +34,12 @@ public class TransferServiceApplication } @Bean - TransferServiceImpl transferService( + TransferService transferService( KafkaProducer producer, ObjectMapper mapper, TransferServiceProperties properties) { - return new TransferServiceImpl(producer, mapper, properties.topic); + return new TransferService(producer, mapper, properties.topic); } diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/Transfer.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/Transfer.java deleted file mode 100644 index 58ca15a..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/Transfer.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.juplo.kafka.payment.transfer.api; - -import lombok.Data; - -import javax.validation.constraints.NotNull; -import java.util.UUID; - - -/** - * Simple DTO used by the REST interface - */ -@Data -public class Transfer -{ - @NotNull(message = "Cannot be null") - private UUID id; - @NotNull(message = "Cannot be null") - private long payer; - @NotNull(message = "Cannot be null") - private long payee; - @NotNull(message = "Cannot be null") - private int amount; -} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/TransferService.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/TransferService.java deleted file mode 100644 index e4bb4f8..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/api/TransferService.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.juplo.kafka.payment.transfer.api; - -public interface TransferService -{ - void handleTransfer(Transfer transfer); -} 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 c93fb46..12547ef 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 @@ -1,8 +1,8 @@ package de.juplo.kafka.payment.transfer.controller; -import de.juplo.kafka.payment.transfer.api.Transfer; -import de.juplo.kafka.payment.transfer.impl.TransferServiceImpl; +import de.juplo.kafka.payment.transfer.domain.Transfer; +import de.juplo.kafka.payment.transfer.domain.TransferService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; @@ -22,16 +22,16 @@ public class TransferController { public final static String PATH = "/transfers"; - private final TransferServiceImpl service; + private final TransferService service; @PostMapping( path = PATH, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity transfer(@Valid @RequestBody Transfer transfer) + public ResponseEntity transfer(@Valid @RequestBody TransferRequest transferRequest) { - service.initiate(transfer); - return ResponseEntity.created(URI.create(PATH + transfer.getId())).build(); + service.initiate(transferRequest); + 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 new file mode 100644 index 0000000..db3c781 --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java @@ -0,0 +1,23 @@ +package de.juplo.kafka.payment.transfer.controller; + +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.UUID; + + +/** + * Simple DTO used by the REST interface + */ +@Data +public class TransferRequest +{ + @NotNull(message = "Cannot be null") + private UUID id; + @NotNull(message = "Cannot be null") + private long payer; + @NotNull(message = "Cannot be null") + private long payee; + @NotNull(message = "Cannot be null") + private int amount; +} 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 new file mode 100644 index 0000000..17330f6 --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java @@ -0,0 +1,23 @@ +package de.juplo.kafka.payment.transfer.domain; + + +import lombok.Data; + + +@Data +public class Transfer +{ + public enum State + { + PENDING, + APPROVED, + REJECTED + } + + private final long id; + private final long payer; + private final long payee; + private final int amount; + + private State state; +} 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 new file mode 100644 index 0000000..ac37192 --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferRepository.java @@ -0,0 +1,13 @@ +package de.juplo.kafka.payment.transfer.domain; + +import de.juplo.kafka.payment.transfer.domain.Transfer; + +import java.util.Optional; +import java.util.UUID; + + +public interface TransferRepository +{ + void put(Transfer transfer); + Optional get(UUID uuid); +} 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 new file mode 100644 index 0000000..34ef29c --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java @@ -0,0 +1,65 @@ +package de.juplo.kafka.payment.transfer.domain; + + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.springframework.http.ResponseEntity; + +import java.net.URI; +import java.util.UUID; + + +@Slf4j +@RequiredArgsConstructor +public class TransferService +{ + private final TransferRepository repository; + private final KafkaProducer producer; + private final ObjectMapper mapper; + private final String topic; + + public void initiate(Transfer transfer) + { + repository + .get(transfer.getId()) + .ifPresentOrElse( + stored -> + { + + }, + () -> + { + + }); + try + { + ProducerRecord record = + new ProducerRecord<>( + topic, + transfer.getId(), + mapper.writeValueAsString(transfer)); + + producer.send(record, (metadata, exception) -> + { + if (exception != null) + { + log.error("Could not place order {}: {}", transfer, exception.toString()); + result.setErrorResult(exception); + return; + } + + result.setResult(ResponseEntity.created(URI.create(path + transfer.getId())).build()); + }); + } + catch (Exception e) + { + log.error("Unexpected exception!", e); + result.setErrorResult(e); + } + + return result; + } +} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/impl/TransferServiceImpl.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/impl/TransferServiceImpl.java deleted file mode 100644 index 9a24b2b..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/impl/TransferServiceImpl.java +++ /dev/null @@ -1,73 +0,0 @@ -package de.juplo.kafka.payment.transfer.impl; - - -import com.fasterxml.jackson.databind.ObjectMapper; -import de.juplo.kafka.payment.transfer.api.Transfer; -import de.juplo.kafka.payment.transfer.api.TransferService; -import de.juplo.kafka.payment.transfer.persistence.TransferRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.context.request.async.DeferredResult; - -import javax.validation.Valid; -import java.net.URI; -import java.util.UUID; - - -@Slf4j -@RequiredArgsConstructor -public class TransferServiceImpl implements TransferService -{ - private final TransferRepository repository; - private final KafkaProducer producer; - private final ObjectMapper mapper; - private final String topic; - - - @Override - public void initiate(Transfer transfer) - { - repository - .get(transfer.getId()) - .ifPresentOrElse( - stored -> - { - - }, - () -> - { - - }); - try - { - ProducerRecord record = - new ProducerRecord<>( - topic, - transfer.getId(), - mapper.writeValueAsString(transfer)); - - producer.send(record, (metadata, exception) -> - { - if (exception != null) - { - log.error("Could not place order {}: {}", transfer, exception.toString()); - result.setErrorResult(exception); - return; - } - - result.setResult(ResponseEntity.created(URI.create(path + transfer.getId())).build()); - }); - } - catch (Exception e) - { - log.error("Unexpected exception!", e); - result.setErrorResult(e); - } - - return result; - } -} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/TransferRepository.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/TransferRepository.java deleted file mode 100644 index 5b3d8b4..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/TransferRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.juplo.kafka.payment.transfer.persistence; - -import de.juplo.kafka.payment.transfer.api.Transfer; - -import java.util.Optional; -import java.util.UUID; - - -public interface TransferRepository -{ - void put(Transfer transfer); - Optional get(UUID uuid); -}