WIP
authorKai Moritz <kai@juplo.de>
Sat, 12 Jun 2021 09:57:43 +0000 (11:57 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 12 Jun 2021 09:57:43 +0000 (11:57 +0200)
transfer/pom.xml
transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java
transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferRequest.java [deleted file]
transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/Transfer.java
transfer/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java

index f471b10..8a26995 100644 (file)
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
+    <dependency> 
+      <groupId>org.springframework.boot</groupId> 
+      <artifactId>spring-boot-starter-validation</artifactId> 
+    </dependency>
 
     <dependency>
       <groupId>org.apache.kafka</groupId>
index 2067ed0..170d8c8 100644 (file)
@@ -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<TransferDTO> 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 (file)
index 43b4f90..0000000
+++ /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;
-}
index 878a233..5556a1b 100644 (file)
@@ -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
index f5d41cb..d708826 100644 (file)
@@ -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<Transfer> get(Long id)
+  {
+    return repository.get(id);
+  }
 }