WIP
[demos/kafka/demos-kafka-payment-system-transfer] / transfer / src / main / java / de / juplo / kafka / payment / transfer / controller / TransferController.java
index c93fb46..2d83ad9 100644 (file)
@@ -1,37 +1,88 @@
 package de.juplo.kafka.payment.transfer.controller;
 
 
-import de.juplo.kafka.payment.transfer.api.Transfer;
-import de.juplo.kafka.payment.transfer.impl.TransferServiceImpl;
+import de.juplo.kafka.payment.transfer.domain.Transfer;
+import de.juplo.kafka.payment.transfer.domain.TransferService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
 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.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import java.net.URI;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
 
 
 @RestController
 @RequiredArgsConstructor
 @Slf4j
-public class TransferController
+ public class TransferController
 {
   public final static String PATH = "/transfers";
 
-  private final TransferServiceImpl service;
+  private final TransferService service;
 
 
   @PostMapping(
       path = PATH,
       consumes = MediaType.APPLICATION_JSON_VALUE,
       produces = MediaType.APPLICATION_JSON_VALUE)
-  public ResponseEntity<?> transfer(@Valid @RequestBody Transfer transfer)
+  public ResponseEntity<?> transfer(@Valid @RequestBody TransferDTO transferDTO)
   {
+    Transfer transfer =
+        Transfer
+            .builder()
+            .id(transferDTO.getId())
+            .payer(transferDTO.getPayer())
+            .payee(transferDTO.getPayee())
+            .amount(transferDTO.getAmount())
+            .build();
+
     service.initiate(transfer);
-    return ResponseEntity.created(URI.create(PATH + transfer.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());
+  }
+
+  @ResponseStatus(HttpStatus.BAD_REQUEST)
+  @ExceptionHandler(MethodArgumentNotValidException.class)
+  public Map<String, Object> handleValidationExceptions(
+      HttpServletRequest request,
+      MethodArgumentNotValidException e)
+  {
+    Map<String, Object> errorAttributes = new HashMap<>();
+    errorAttributes.put("status", HttpStatus.BAD_REQUEST.value());
+    errorAttributes.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
+    errorAttributes.put("path", request.getRequestURI());
+    errorAttributes.put("method", request.getMethod());
+    errorAttributes.put("timestamp", new Date());
+    Map<String, String> errors = new HashMap<>();
+    e.getBindingResult().getAllErrors().forEach((error) -> {
+      String fieldName = ((FieldError) error).getField();
+      String errorMessage = error.getDefaultMessage();
+      errors.put(fieldName, errorMessage);
+    });
+    errorAttributes.put("errors", errors);
+    errorAttributes.put("message", "Validation failed: Invalid message format, error count: " + errors.size());
+    return errorAttributes;
   }
 }