1
1
package com .github .throyer .common .springboot .controllers .api ;
2
2
3
- import static org .springframework .http .HttpStatus .CREATED ;
4
- import static org .springframework .http .HttpStatus .NO_CONTENT ;
5
-
6
- import static com .github .throyer .common .springboot .utils .Responses .created ;
7
- import static com .github .throyer .common .springboot .utils .Responses .ok ;
8
-
9
3
import com .github .throyer .common .springboot .domain .pagination .model .Page ;
10
- import com .github .throyer .common .springboot .domain .user .service .FindUserService ;
11
- import com .github .throyer .common .springboot .domain .user .service .RemoveUserService ;
4
+ import com .github .throyer .common .springboot .domain .pagination .service .Pagination ;
12
5
import com .github .throyer .common .springboot .domain .user .form .CreateUserProps ;
13
- import com .github .throyer .common .springboot .domain .user .service .CreateUserService ;
14
6
import com .github .throyer .common .springboot .domain .user .form .UpdateUserProps ;
7
+ import com .github .throyer .common .springboot .domain .user .model .UserDetails ;
8
+ import com .github .throyer .common .springboot .domain .user .repository .UserRepository ;
9
+ import com .github .throyer .common .springboot .domain .user .service .CreateUserService ;
15
10
import com .github .throyer .common .springboot .domain .user .service .FindUserByIdService ;
11
+ import com .github .throyer .common .springboot .domain .user .service .RemoveUserService ;
16
12
import com .github .throyer .common .springboot .domain .user .service .UpdateUserService ;
17
- import com .github .throyer .common .springboot .domain .user .model .UserDetails ;
18
-
19
13
import io .swagger .v3 .oas .annotations .Operation ;
20
14
import io .swagger .v3 .oas .annotations .security .SecurityRequirement ;
21
-
22
- import java .util .Optional ;
23
-
24
15
import io .swagger .v3 .oas .annotations .tags .Tag ;
25
16
import org .springframework .beans .factory .annotation .Autowired ;
26
17
import org .springframework .http .ResponseEntity ;
27
18
import org .springframework .security .access .prepost .PreAuthorize ;
28
19
import org .springframework .validation .annotation .Validated ;
29
- import org .springframework .web .bind .annotation .DeleteMapping ;
30
- import org .springframework .web .bind .annotation .GetMapping ;
31
- import org .springframework .web .bind .annotation .PathVariable ;
32
- import org .springframework .web .bind .annotation .PostMapping ;
33
- import org .springframework .web .bind .annotation .PutMapping ;
34
- import org .springframework .web .bind .annotation .RequestBody ;
35
- import org .springframework .web .bind .annotation .RequestMapping ;
36
- import org .springframework .web .bind .annotation .ResponseStatus ;
37
- import org .springframework .web .bind .annotation .RestController ;
20
+ import org .springframework .web .bind .annotation .*;
21
+
22
+ import java .util .Optional ;
23
+
24
+ import static com .github .throyer .common .springboot .utils .Responses .*;
25
+ import static org .springframework .http .HttpStatus .CREATED ;
26
+ import static org .springframework .http .HttpStatus .NO_CONTENT ;
38
27
39
28
@ RestController
40
29
@ Tag (name = "Users" )
@@ -44,22 +33,23 @@ public class UsersController {
44
33
private final CreateUserService createService ;
45
34
private final UpdateUserService updateService ;
46
35
private final RemoveUserService removeService ;
47
- private final FindUserService findService ;
48
36
private final FindUserByIdService findByIdService ;
49
37
38
+ private final UserRepository repository ;
39
+
50
40
@ Autowired
51
41
public UsersController (
52
42
CreateUserService createService ,
53
43
UpdateUserService updateService ,
54
44
RemoveUserService removeService ,
55
- FindUserService findService ,
56
- FindUserByIdService findByIdService
45
+ FindUserByIdService findByIdService ,
46
+ UserRepository repository
57
47
) {
58
48
this .createService = createService ;
59
49
this .updateService = updateService ;
60
50
this .removeService = removeService ;
61
- this .findService = findService ;
62
51
this .findByIdService = findByIdService ;
52
+ this .repository = repository ;
63
53
}
64
54
65
55
@ GetMapping
@@ -70,17 +60,19 @@ public ResponseEntity<Page<UserDetails>> index(
70
60
Optional <Integer > page ,
71
61
Optional <Integer > size
72
62
) {
73
- var content = findService .findAll (page , size );
74
- return ok (content );
63
+ var pageable = Pagination .of (page , size );
64
+ var content = repository .findAll (pageable );
65
+ return ok (content .map (UserDetails ::new ));
75
66
}
76
67
77
68
@ GetMapping ("/{id}" )
78
69
@ SecurityRequirement (name = "token" )
79
70
@ PreAuthorize ("hasAnyAuthority('ADM', 'USER')" )
80
71
@ Operation (summary = "Show user info" )
81
72
public ResponseEntity <UserDetails > show (@ PathVariable Long id ) {
82
- var user = findByIdService .find (id );
83
- return ok (user );
73
+ var user = repository .findById (id )
74
+ .orElseThrow (() -> notFound ("user not found" ));
75
+ return ok (new UserDetails (user ));
84
76
}
85
77
86
78
@ PostMapping
0 commit comments