11package br .com .developers .controller .user ;
22
3- import javax .validation .Valid ;
43import org .springframework .http .ResponseEntity ;
4+ import org .springframework .security .access .prepost .PreAuthorize ;
5+ import org .springframework .validation .annotation .Validated ;
56import org .springframework .web .bind .annotation .CrossOrigin ;
7+ import org .springframework .web .bind .annotation .DeleteMapping ;
8+ import org .springframework .web .bind .annotation .GetMapping ;
69import org .springframework .web .bind .annotation .PostMapping ;
10+ import org .springframework .web .bind .annotation .PutMapping ;
711import org .springframework .web .bind .annotation .RequestBody ;
812import org .springframework .web .bind .annotation .RequestMapping ;
13+ import org .springframework .web .bind .annotation .RequestParam ;
914import org .springframework .web .bind .annotation .RestController ;
1015import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
1116import br .com .developers .domain .model .User ;
17+ import br .com .developers .login .dto .LoginDTO ;
1218import br .com .developers .login .dto .RegisterDTO ;
1319import br .com .developers .login .service .UserService ;
1420import lombok .AccessLevel ;
@@ -23,12 +29,31 @@ public class UserController {
2329 private UserService userService ;
2430
2531 @ PostMapping ("/" )
26- public ResponseEntity <User > registerUser (@ Valid @ RequestBody RegisterDTO registerData ) {
27- User user = userService .registerUser (registerData );
32+ public ResponseEntity <Object > registerUser (@ Validated @ RequestBody RegisterDTO registerData ) {
33+ User user = this .userService .registerUser (registerData );
34+ return ResponseEntity .created (ServletUriComponentsBuilder .fromCurrentRequest ().path ("/{id}" )
35+ .buildAndExpand (user .getId ()).toUri ()).build ();
36+ }
2837
38+ @ PutMapping ("/" )
39+ @ PreAuthorize ("hasRole('ADMIN')" )
40+ public ResponseEntity <Object > update (@ Validated @ RequestBody RegisterDTO registerData ) {
41+ User user = this .userService .updateUser (registerData );
2942 return ResponseEntity .created (ServletUriComponentsBuilder .fromCurrentRequest ().path ("/{id}" )
3043 .buildAndExpand (user .getId ()).toUri ()).build ();
44+ }
45+
46+ @ GetMapping ("/{id}" )
47+ @ PreAuthorize ("hasRole('ADMIN')" )
48+ public ResponseEntity <Object > find (@ RequestParam (name = "id" ) String id ) {
49+ return ResponseEntity .ok (this .userService .find (id ));
50+ }
3151
52+ @ DeleteMapping ("/" )
53+ @ PreAuthorize ("hasRole('ADMIN')" )
54+ public ResponseEntity <Object > delete (@ Validated @ RequestBody LoginDTO loginDTO ) {
55+ this .userService .delete (loginDTO );
56+ return ResponseEntity .noContent ().build ();
3257 }
3358
3459}
0 commit comments