1 package de.juplo.kafka.payment.transfer.adapter;
4 import de.juplo.kafka.payment.transfer.domain.Transfer;
5 import de.juplo.kafka.payment.transfer.ports.GetTransferUseCase;
6 import de.juplo.kafka.payment.transfer.ports.MessagingService;
7 import lombok.RequiredArgsConstructor;
8 import lombok.extern.slf4j.Slf4j;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.http.MediaType;
11 import org.springframework.http.ResponseEntity;
12 import org.springframework.validation.FieldError;
13 import org.springframework.web.bind.MethodArgumentNotValidException;
14 import org.springframework.web.bind.annotation.*;
15 import org.springframework.web.context.request.async.DeferredResult;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.validation.Valid;
20 import java.util.Date;
21 import java.util.HashMap;
23 import java.util.Optional;
24 import java.util.concurrent.CompletableFuture;
28 @RequiredArgsConstructor
30 public class TransferController
32 public final static String PATH = "/transfers";
34 private final GetTransferUseCase getTransferUseCase;
35 private final MessagingService messagingService;
40 consumes = MediaType.APPLICATION_JSON_VALUE,
41 produces = MediaType.APPLICATION_JSON_VALUE)
42 public DeferredResult<ResponseEntity<?>> transfer(
43 HttpServletRequest request,
44 @Valid @RequestBody TransferDTO transferDTO)
46 DeferredResult<ResponseEntity<?>> result = new DeferredResult<>();
49 .get(transferDTO.getId())
51 CompletableFuture.completedFuture(
54 .location(location(transferDTO))
62 .id(transferDTO.getId())
63 .payer(transferDTO.getPayer())
64 .payee(transferDTO.getPayee())
65 .amount(transferDTO.getAmount())
69 .created(location(transferDTO))
72 .thenAccept(responseEntity -> result.setResult(responseEntity))
75 result.setErrorResult(e);
82 private URI location(TransferDTO transferDTO)
84 return URI.create(PATH + "/" + transferDTO.getId());
88 path = PATH + "/{id}",
89 produces = MediaType.APPLICATION_JSON_VALUE)
90 public ResponseEntity<TransferDTO> get(@PathVariable Long id)
95 .map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer)))
96 .orElse(ResponseEntity.notFound().build());
99 @ResponseStatus(HttpStatus.BAD_REQUEST)
100 @ExceptionHandler(MethodArgumentNotValidException.class)
101 public Map<String, Object> handleValidationExceptions(
102 HttpServletRequest request,
103 MethodArgumentNotValidException e)
105 Map<String, Object> errorAttributes = new HashMap<>();
106 errorAttributes.put("status", HttpStatus.BAD_REQUEST.value());
107 errorAttributes.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
108 errorAttributes.put("path", request.getRequestURI());
109 errorAttributes.put("method", request.getMethod());
110 errorAttributes.put("timestamp", new Date());
111 Map<String, String> errors = new HashMap<>();
112 e.getBindingResult().getAllErrors().forEach((error) -> {
113 String fieldName = ((FieldError) error).getField();
114 String errorMessage = error.getDefaultMessage();
115 errors.put(fieldName, errorMessage);
117 errorAttributes.put("errors", errors);
118 errorAttributes.put("message", "Validation failed: Invalid message format, error count: " + errors.size());
119 return errorAttributes;