<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>
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
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());
}
}
+++ /dev/null
-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;
-}
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.*;
},
() ->
{
- repository.store(transfer);
send(transfer);
+ transfer.setState(SENT);
+ repository.store(transfer);
});
}
throw new RuntimeException("Could not convert " + transfer, e);
}
}
+
+ public Optional<Transfer> get(Long id)
+ {
+ return repository.get(id);
+ }
}