1 package de.juplo.kafka.payment.transfer.controller;
4 import de.juplo.kafka.payment.transfer.domain.Transfer;
5 import de.juplo.kafka.payment.transfer.domain.TransferService;
6 import lombok.RequiredArgsConstructor;
7 import lombok.extern.slf4j.Slf4j;
8 import org.springframework.http.HttpStatus;
9 import org.springframework.http.MediaType;
10 import org.springframework.http.ResponseEntity;
11 import org.springframework.validation.FieldError;
12 import org.springframework.web.bind.MethodArgumentNotValidException;
13 import org.springframework.web.bind.annotation.*;
15 import javax.validation.Valid;
17 import java.util.HashMap;
19 import java.util.Optional;
23 @RequiredArgsConstructor
25 public class TransferController
27 public final static String PATH = "/transfers";
29 private final TransferService service;
34 consumes = MediaType.APPLICATION_JSON_VALUE,
35 produces = MediaType.APPLICATION_JSON_VALUE)
36 public ResponseEntity<?> transfer(@Valid @RequestBody TransferDTO transferDTO)
41 .id(transferDTO.getId())
42 .payer(transferDTO.getPayer())
43 .payee(transferDTO.getPayee())
44 .amount(transferDTO.getAmount())
47 service.initiate(transfer);
49 return ResponseEntity.created(URI.create(PATH + "/" + transferDTO.getId())).build();
53 path = PATH + "/{id}",
54 produces = MediaType.APPLICATION_JSON_VALUE)
55 public ResponseEntity<TransferDTO> get(@PathVariable Long id)
60 .map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer)))
61 .orElse(ResponseEntity.notFound().build());
64 @ResponseStatus(HttpStatus.BAD_REQUEST)
65 @ExceptionHandler(MethodArgumentNotValidException.class)
66 public Map<String, String> handleValidationExceptions(
67 MethodArgumentNotValidException ex) {
68 Map<String, String> errors = new HashMap<>();
69 ex.getBindingResult().getAllErrors().forEach((error) -> {
70 String fieldName = ((FieldError) error).getField();
71 String errorMessage = error.getDefaultMessage();
72 errors.put(fieldName, errorMessage);