Skip to content

Commit 08df6cc

Browse files
committed
Apply the basic structure on api layer part -2
1 parent a7afa47 commit 08df6cc

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/main/java/lk/ijse/dep/note/api/UserController.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import lk.ijse.dep.note.dto.UserDTO;
44
import lk.ijse.dep.note.service.UserService;
5+
import lk.ijse.dep.note.service.exception.DuplicateEmailException;
6+
import lk.ijse.dep.note.service.exception.NotFoundException;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.http.HttpStatus;
79
import 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

Comments
 (0)