From 1c2d4e84c5906ac25e06b5193aecb1a9ee3288fa Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 12 Jun 2021 11:57:43 +0200 Subject: [PATCH] WIP --- transfer/pom.xml | 4 +++ .../controller/TransferController.java | 29 +++++++++++++------ .../transfer/controller/TransferRequest.java | 22 -------------- .../payment/transfer/domain/Transfer.java | 4 +-- .../transfer/domain/TransferService.java | 12 +++++--- 5 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java diff --git a/transfer/pom.xml b/transfer/pom.xml index f471b10..8a26995 100644 --- a/transfer/pom.xml +++ b/transfer/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.apache.kafka 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 2067ed0..170d8c8 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 @@ -7,12 +7,11 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.net.URI; +import java.util.Optional; @RestController @@ -29,19 +28,31 @@ import java.net.URI; path = PATH, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity transfer(@Valid @RequestBody TransferRequest transferRequest) + public ResponseEntity transfer(@Valid @RequestBody TransferDTO transferDTO) { Transfer transfer = Transfer .builder() - .id(transferRequest.getId()) - .payer(transferRequest.getPayer()) - .payee(transferRequest.getPayee()) - .amount(transferRequest.getAmount()) + .id(transferDTO.getId()) + .payer(transferDTO.getPayer()) + .payee(transferDTO.getPayee()) + .amount(transferDTO.getAmount()) .build(); service.initiate(transfer); - return ResponseEntity.created(URI.create(PATH + transferRequest.getId())).build(); + return ResponseEntity.created(URI.create(PATH + "/" + transferDTO.getId())).build(); + } + + @GetMapping( + path = PATH + "/{id}", + produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity get(@PathVariable Long id) + { + return + service + .get(id) + .map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer))) + .orElse(ResponseEntity.notFound().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 deleted file mode 100644 index 43b4f90..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.juplo.kafka.payment.transfer.controller; - -import lombok.Data; - -import javax.validation.constraints.NotNull; - - -/** - * Simple DTO used by the REST interface - */ -@Data -public class TransferRequest -{ - @NotNull(message = "Cannot be null") - private long 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 index 878a233..5556a1b 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 @@ -2,11 +2,11 @@ package de.juplo.kafka.payment.transfer.domain; import lombok.Builder; +import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.Value; -@Value +@Data @Builder @EqualsAndHashCode(exclude = "state") public class Transfer 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 f5d41cb..d708826 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 @@ -7,10 +7,8 @@ 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; +import java.util.Optional; import static de.juplo.kafka.payment.transfer.domain.Transfer.State.*; @@ -47,8 +45,9 @@ public class TransferService }, () -> { - repository.store(transfer); send(transfer); + transfer.setState(SENT); + repository.store(transfer); }); } @@ -82,4 +81,9 @@ public class TransferService throw new RuntimeException("Could not convert " + transfer, e); } } + + public Optional get(Long id) + { + return repository.get(id); + } } -- 2.20.1