Skip to content

Commit

Permalink
Merge pull request #21 from JishnuGoyal/revamp-user-controller
Browse files Browse the repository at this point in the history
Fix #20: Revamp User Controller
  • Loading branch information
Hemanthr1 authored Nov 11, 2024
2 parents 7eb1247 + 07d7342 commit 7e13ca9
Showing 1 changed file with 37 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ public UserController(
*/
@GetMapping
public ResponseEntity<ResponseDTO> getUser(Authentication authentication) {
if (authentication != null && authentication.isAuthenticated()) {
try {
User user = (User) authentication.getPrincipal();
if (user != null) {
return new ResponseEntity<>(new ResponseDTO(true, "User details retrieved successfully", user), HttpStatus.OK);
} else {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not found", null), HttpStatus.UNAUTHORIZED);
}
} else {
return new ResponseEntity<>(new ResponseDTO(true, "User details retrieved successfully", user), HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not authenticated", null), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -52,16 +50,13 @@ public ResponseEntity<ResponseDTO> getUser(Authentication authentication) {
*/
@GetMapping("/is_verified")
public ResponseEntity<ResponseDTO> getUserVerified(Authentication authentication) {
if (authentication != null && authentication.isAuthenticated()) {
try {
User user = (User) authentication.getPrincipal();

if (user != null) {
return new ResponseEntity<>(new ResponseDTO(true, "User verifcation status retrived", user.isEmailVerified()), HttpStatus.OK);
} else {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not found", null), HttpStatus.UNAUTHORIZED);
}
} else {
return new ResponseEntity<>(new ResponseDTO(true, "User verification status retrieved", user.isEmailVerified()), HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not authenticated", null), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -74,16 +69,14 @@ public ResponseEntity<ResponseDTO> getUserVerified(Authentication authentication
*/
@PatchMapping
public ResponseEntity<ResponseDTO> updateUser(Authentication authentication, @RequestBody User updatedUser) {
if (authentication != null && authentication.isAuthenticated()) {
try {
User user = (User) authentication.getPrincipal();
userService.updateUser(user, updatedUser);
if (user != null) {
return new ResponseEntity<>(new ResponseDTO(true, "User updated successfully", user), HttpStatus.OK);
} else {
return new ResponseEntity<>(new ResponseDTO(false, "User not found or update failed", null), HttpStatus.NOT_FOUND);
}
} else {
return new ResponseEntity<>(new ResponseDTO(true, "User updated successfully", user), HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not authenticated", null), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -95,22 +88,20 @@ public ResponseEntity<ResponseDTO> updateUser(Authentication authentication, @Re
* @return ResponseEntity with a ResponseDTO indicating the success of the update.
*/
@PatchMapping("/update-user")
public ResponseEntity<ResponseDTO> updateUserForDashboard(Authentication authentication, @RequestBody UpdateUserRequestDTO updateUserRequestDTO) throws Exception {
if (authentication != null && authentication.isAuthenticated()) {
public ResponseEntity<ResponseDTO> updateUserForDashboard(Authentication authentication, @RequestBody UpdateUserRequestDTO updateUserRequestDTO) {
try {
User user = (User) authentication.getPrincipal();
if (user == null) {
return new ResponseEntity<>(new ResponseDTO(false, "User not found or update failed", null), HttpStatus.NOT_FOUND);
} else {
user.setFullName(updateUserRequestDTO.getFullName());
user.setUserOrganisationRole(updateUserRequestDTO.getUserOrganisationRole());
if (!user.isShouldNavigateToDashboard()) {
user.setShouldNavigateToDashboard(true);
}
userService.saveUser(user);
return new ResponseEntity<>(new ResponseDTO(true, "User updated successfully", user), HttpStatus.OK);
user.setFullName(updateUserRequestDTO.getFullName());
user.setUserOrganisationRole(updateUserRequestDTO.getUserOrganisationRole());
if (!user.isShouldNavigateToDashboard()) {
user.setShouldNavigateToDashboard(true);
}
} else {
userService.saveUser(user);
return new ResponseEntity<>(new ResponseDTO(true, "User updated successfully", user), HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not authenticated", null), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -122,23 +113,19 @@ public ResponseEntity<ResponseDTO> updateUserForDashboard(Authentication authent
*/
@DeleteMapping
public ResponseEntity<ResponseDTO> deleteUser(Authentication authentication) {
if (authentication != null && authentication.isAuthenticated()) {
try {
System.out.println(authentication);
User user = (User) authentication.getPrincipal();
boolean deleted = userService.deleteUser(user);
try {
User user = (User) authentication.getPrincipal();
boolean deleted = userService.deleteUser(user);

if (deleted) {
return new ResponseEntity<>(new ResponseDTO(true, "User deleted successfully", null), HttpStatus.OK);
} else {
return new ResponseEntity<>(new ResponseDTO(false, "User delete failed", null), HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
if (deleted) {
return new ResponseEntity<>(new ResponseDTO(true, "User deleted successfully", null), HttpStatus.OK);
} else {
return new ResponseEntity<>(new ResponseDTO(false, "User delete failed", null), HttpStatus.INTERNAL_SERVER_ERROR);
}
} else {
// User not authenticated or invalid token
} catch (NullPointerException e) {
return new ResponseEntity<>(new ResponseDTO(false, "Invalid token or user not authenticated", null), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(new ResponseDTO(false, "An error occurred: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
}

0 comments on commit 7e13ca9

Please sign in to comment.