Skip to content

Commit ed66255

Browse files
committed
Return Validated<?>
1 parent fca59b2 commit ed66255

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

todo-api/src/main/java/lol/maki/dev/todo/TodoController.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import am.ik.yavi.arguments.Arguments;
44
import am.ik.yavi.arguments.Arguments1Validator;
55
import am.ik.yavi.builder.StringValidatorBuilder;
6+
import am.ik.yavi.core.ConstraintViolation;
67
import am.ik.yavi.core.ConstraintViolations;
7-
import am.ik.yavi.core.ConstraintViolationsException;
8+
import am.ik.yavi.core.Validated;
89
import java.net.URI;
910
import java.util.List;
1011
import java.util.Map;
@@ -47,22 +48,24 @@ public ResponseEntity<Todo> getTodo(@PathVariable("todoId") UUID todoId) {
4748
}
4849

4950
@PostMapping(path = "")
50-
public ResponseEntity<Todo> postTodos(@RequestBody Map<String, String> request, @AuthenticationPrincipal Jwt jwt,
51+
public ResponseEntity<?> postTodos(@RequestBody Map<String, String> request, @AuthenticationPrincipal Jwt jwt,
5152
UriComponentsBuilder builder) {
52-
TodoCreateRequest req = TodoCreateRequest.parse(request);
53-
String email = jwt.getClaimAsString("email");
54-
Todo created = this.todoService.create(req.todoTitle(), email);
55-
URI uri = builder.pathSegment("todos", created.todoId().toString()).build().toUri();
56-
return ResponseEntity.created(uri).body(created);
53+
return TodoCreateRequest.parse(request).fold(this::badRequest, req -> {
54+
String email = jwt.getClaimAsString("email");
55+
Todo created = this.todoService.create(req.todoTitle(), email);
56+
URI uri = builder.pathSegment("todos", created.todoId().toString()).build().toUri();
57+
return ResponseEntity.created(uri).body(created);
58+
});
5759
}
5860

5961
@PatchMapping(path = "/{todoId}")
60-
public ResponseEntity<Todo> patchTodo(@PathVariable("todoId") UUID todoId, @RequestBody Map<String, String> request,
62+
public ResponseEntity<?> patchTodo(@PathVariable("todoId") UUID todoId, @RequestBody Map<String, String> request,
6163
@AuthenticationPrincipal Jwt jwt) {
62-
TodoUpdateRequest req = TodoUpdateRequest.parse(request);
63-
String email = jwt.getClaimAsString("email");
64-
Todo updated = this.todoService.update(todoId, req.todoTitle(), req.finished(), email);
65-
return ResponseEntity.ok(updated);
64+
return TodoUpdateRequest.parse(request).fold(this::badRequest, req -> {
65+
String email = jwt.getClaimAsString("email");
66+
Todo updated = this.todoService.update(todoId, req.todoTitle(), req.finished(), email);
67+
return ResponseEntity.ok(updated);
68+
});
6669
}
6770

6871
@DeleteMapping(path = "/{todoId}")
@@ -76,20 +79,18 @@ public ResponseEntity<?> handleNotFound(TodoService.NotFoundException e) {
7679
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("message", e.getMessage()));
7780
}
7881

79-
@ExceptionHandler(ConstraintViolationsException.class)
80-
public ResponseEntity<?> handleConstraintViolations(ConstraintViolationsException e) {
82+
ResponseEntity<Map<String, Object>> badRequest(List<ConstraintViolation> violations) {
8183
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
82-
.body(Map.of("message", "Validation failed", "violations",
83-
ConstraintViolations.of(e.violations()).details()));
84+
.body(Map.of("message", "Validation failed", "violations", ConstraintViolations.of(violations).details()));
8485
}
8586

8687
record TodoCreateRequest(String todoTitle) {
8788
private static final Arguments1Validator<Map<String, String>, TodoCreateRequest> validator = Todo.todoTitleValidator
8889
.andThen(TodoCreateRequest::new)
8990
.compose(map -> map.get("todoTitle"));
9091

91-
static TodoCreateRequest parse(Map<String, String> map) {
92-
return validator.validated(map);
92+
static Validated<TodoCreateRequest> parse(Map<String, String> map) {
93+
return validator.validate(map);
9394
}
9495
}
9596

@@ -101,8 +102,8 @@ record TodoUpdateRequest(String todoTitle, Boolean finished) {
101102
.apply(TodoUpdateRequest::new)
102103
.compose(map -> Arguments.of(map.get("todoTitle"), map.get("finished")));
103104

104-
static TodoUpdateRequest parse(Map<String, String> map) {
105-
return validator.validated(map);
105+
static Validated<TodoUpdateRequest> parse(Map<String, String> map) {
106+
return validator.validate(map);
106107
}
107108
}
108109

0 commit comments

Comments
 (0)