22
33import lk .ijse .dep .note .dto .UserDTO ;
44import lk .ijse .dep .note .service .UserService ;
5+ import lk .ijse .dep .note .service .exception .DuplicateEmailException ;
6+ import lk .ijse .dep .note .service .exception .NotFoundException ;
57import org .springframework .beans .factory .annotation .Autowired ;
68import org .springframework .http .HttpStatus ;
79import org .springframework .web .bind .annotation .*;
10+ import org .springframework .web .server .ResponseStatusException ;
811
912@ RestController
1013@ RequestMapping ("api/v1/users" )
@@ -17,22 +20,40 @@ public class UserController {
1720 public UserDTO registerUser (@ RequestBody UserDTO user ){
1821 //validate
1922
20- return user ;
23+
24+ try {
25+ userService .registerUser (user );
26+ } catch (DuplicateEmailException e ) {
27+ throw new ResponseStatusException (HttpStatus .CONFLICT ,"Email already exists" ,e );
28+ }
29+ return user ;
2130 }
22- @ GetMapping (path = "{userId}" ,produces = "application/json" )
31+ @ GetMapping (path = "{userId:[A-Fa-f0- \\ ~]{36}} }" ,produces = "application/json" )
2332 public UserDTO getUserInfo (@ PathVariable String userId ){
2433 System .out .println ("get" +userId );
25- return null ;
34+ try {
35+ return userService .getUserInfo (userId );
36+ } catch (NotFoundException e ) {
37+ throw new ResponseStatusException (404 ,"invalid user id" ,e );
38+ }
2639 }
2740 @ ResponseStatus (HttpStatus .NO_CONTENT )
28- @ DeleteMapping ("{userId}" )
41+ @ DeleteMapping ("{userId:[A-Fa-f0- \\ ~]{36} }" )
2942 public void deleteuser (@ PathVariable String userId ){
30- System .out .println ("delete" );
43+ try {
44+ userService .deleteUser (userId );
45+ } catch (NotFoundException e ) {
46+ throw new ResponseStatusException (404 ,"invalid user id" ,e );
47+ }
3148 }
3249 @ ResponseStatus (HttpStatus .NO_CONTENT )
33- @ PatchMapping (path = "{userId}" ,consumes = "application/json" )
50+ @ PatchMapping (path = "{userId:[A-Fa-f0- \\ ~]{36} }" ,consumes = "application/json" )
3451 public void updateUser (@ PathVariable String userId ,@ RequestBody UserDTO user ){
35- System .out .println ("update" );
52+ try {
53+ userService .updateUser (user );
54+ } catch (NotFoundException e ) {
55+ throw new ResponseStatusException (404 ,"invalid user id" ,e );
56+ }
3657 }
3758
3859}
0 commit comments