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.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Optional;
.map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer)))
.orElse(ResponseEntity.notFound().build());
}
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(MethodArgumentNotValidException.class)
+ public Map<String, String> handleValidationExceptions(
+ MethodArgumentNotValidException ex) {
+ Map<String, String> errors = new HashMap<>();
+ ex.getBindingResult().getAllErrors().forEach((error) -> {
+ String fieldName = ((FieldError) error).getField();
+ String errorMessage = error.getDefaultMessage();
+ errors.put(fieldName, errorMessage);
+ });
+ return errors;
+ }
}
import lombok.Builder;
import lombok.Data;
+import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class TransferDTO
{
@NotNull(message = "Cannot be null")
- private long id;
+ @Min(value = 1, message = "A valid transfer id must be a positive number")
+ private Long id;
@NotNull(message = "Cannot be null")
- private long payer;
+ @Min(value = 1, message = "A valid banc account id must be a positive number")
+ private Long payer;
@NotNull(message = "Cannot be null")
- private long payee;
+ @Min(value = 1, message = "A valid banc account id must be a positive number")
+ private Long payee;
@NotNull(message = "Cannot be null")
- private int amount;
+ @Min(value = 1, message = "Cannot transfer a non-positiv amount")
+ private Integer amount;
private Transfer.State state;