11package lol .maki .dev .todo ;
22
3+ import am .ik .yavi .arguments .Arguments ;
4+ import am .ik .yavi .arguments .Arguments1Validator ;
5+ import am .ik .yavi .builder .StringValidatorBuilder ;
36import am .ik .yavi .core .ConstraintViolations ;
47import am .ik .yavi .core .ConstraintViolationsException ;
58import java .net .URI ;
69import java .util .List ;
710import java .util .Map ;
11+ import java .util .Set ;
812import java .util .UUID ;
913import org .springframework .http .HttpStatus ;
1014import org .springframework .http .ResponseEntity ;
@@ -44,20 +48,21 @@ public ResponseEntity<Todo> getTodo(@PathVariable("todoId") UUID todoId) {
4448 }
4549
4650 @ PostMapping (path = "" )
47- public ResponseEntity <Todo > postTodos (@ RequestBody Map <String , Object > request , @ AuthenticationPrincipal Jwt jwt ,
51+ public ResponseEntity <Todo > postTodos (@ RequestBody Map <String , String > request , @ AuthenticationPrincipal Jwt jwt ,
4852 UriComponentsBuilder builder ) {
53+ TodoCreateRequest req = TodoCreateRequest .parse (request );
4954 String email = jwt .getClaimAsString ("email" );
50- Todo created = this .todoService .create (( String ) request . get ( " todoTitle" ), email );
55+ Todo created = this .todoService .create (req . todoTitle ( ), email );
5156 URI uri = builder .pathSegment ("todos" , created .todoId ().toString ()).build ().toUri ();
5257 return ResponseEntity .created (uri ).body (created );
5358 }
5459
5560 @ PatchMapping (path = "/{todoId}" )
56- public ResponseEntity <Todo > patchTodo (@ PathVariable ("todoId" ) UUID todoId , @ RequestBody Map <String , Object > request ,
61+ public ResponseEntity <Todo > patchTodo (@ PathVariable ("todoId" ) UUID todoId , @ RequestBody Map <String , String > request ,
5762 @ AuthenticationPrincipal Jwt jwt ) {
63+ TodoUpdateRequest req = TodoUpdateRequest .parse (request );
5864 String email = jwt .getClaimAsString ("email" );
59- Todo updated = this .todoService .update (todoId , (String ) request .get ("todoTitle" ),
60- (Boolean ) request .get ("finished" ), email );
65+ Todo updated = this .todoService .update (todoId , req .todoTitle (), req .finished (), email );
6166 return ResponseEntity .ok (updated );
6267 }
6368
@@ -79,4 +84,27 @@ public ResponseEntity<?> handleConstraintViolations(ConstraintViolationsExceptio
7984 ConstraintViolations .of (e .violations ()).details ()));
8085 }
8186
87+ record TodoCreateRequest (String todoTitle ) {
88+ private static final Arguments1Validator <Map <String , String >, TodoCreateRequest > validator = Todo .todoTitleValidator
89+ .andThen (TodoCreateRequest ::new )
90+ .compose (map -> map .get ("todoTitle" ));
91+
92+ static TodoCreateRequest parse (Map <String , String > map ) {
93+ return validator .validated (map );
94+ }
95+ }
96+
97+ record TodoUpdateRequest (String todoTitle , Boolean finished ) {
98+ private static final Arguments1Validator <Map <String , String >, TodoUpdateRequest > validator = Todo .todoTitleValidator
99+ .split (StringValidatorBuilder .of ("finished" , c -> c .oneOf (Set .of ("true" , "false" )))
100+ .build (Boolean ::parseBoolean )
101+ .andThen (Todo .finishedValidator ))
102+ .apply (TodoUpdateRequest ::new )
103+ .compose (map -> Arguments .of (map .get ("todoTitle" ), map .get ("finished" )));
104+
105+ static TodoUpdateRequest parse (Map <String , String > map ) {
106+ return validator .validated (map );
107+ }
108+ }
109+
82110}
0 commit comments