diff --git a/src/main/java/no/ntnu/idi/stud/savingsapp/controller/user/UserController.java b/src/main/java/no/ntnu/idi/stud/savingsapp/controller/user/UserController.java index 39244808a53a656b9a60e6e25f6db0d7728e25f1..35a8e39ec8bcdc41e7d72f540943e25c4cf54d6a 100644 --- a/src/main/java/no/ntnu/idi/stud/savingsapp/controller/user/UserController.java +++ b/src/main/java/no/ntnu/idi/stud/savingsapp/controller/user/UserController.java @@ -152,6 +152,17 @@ public class UserController { return ResponseEntity.ok(userDTO); } + @Operation(summary = "Delete the authenticated user", description = "Delete the authenticated user") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successfully deleted user") + }) + @DeleteMapping(value = "/me", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Void> deleteUser(@AuthenticationPrincipal AuthIdentity identity) { + userService.delete(identity.getId()); + log.info("[UserController:deleteUser] user: {}", identity.getId()); + return ResponseEntity.ok().build(); + } + @Operation(summary = "Update a password", description = "Update the password of the authenticated user") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successfully updated password") diff --git a/src/main/java/no/ntnu/idi/stud/savingsapp/service/UserService.java b/src/main/java/no/ntnu/idi/stud/savingsapp/service/UserService.java index 6acf9031d3eefc059486161b8a61df1cf6f49442..16adfa92404e68d3c9ae7aa35e3cd55560cabd28 100644 --- a/src/main/java/no/ntnu/idi/stud/savingsapp/service/UserService.java +++ b/src/main/java/no/ntnu/idi/stud/savingsapp/service/UserService.java @@ -55,6 +55,15 @@ public interface UserService { */ User update(User user); + /** + * Deletes a user from the system based on the specified user ID. + * This method permanently removes the user's record from the database. It should be used with caution, + * as this operation is irreversible and results in the loss of all data associated with the user's account. + * + * @param userId The unique identifier of the user to be deleted. + */ + void delete(long userId); + /** * Updates the password of a user. * diff --git a/src/main/java/no/ntnu/idi/stud/savingsapp/service/impl/UserServiceImpl.java b/src/main/java/no/ntnu/idi/stud/savingsapp/service/impl/UserServiceImpl.java index b4341e68ad77cbef179721dd472457e22d27974a..ce8c227673f1f9d0ebc35f24e8112fc000cac2ed 100644 --- a/src/main/java/no/ntnu/idi/stud/savingsapp/service/impl/UserServiceImpl.java +++ b/src/main/java/no/ntnu/idi/stud/savingsapp/service/impl/UserServiceImpl.java @@ -227,6 +227,18 @@ public class UserServiceImpl implements UserService { } } + /** + * Deletes a user from the system based on the specified user ID. + * This method permanently removes the user's record from the database. It should be used with caution, + * as this operation is irreversible and results in the loss of all data associated with the user's account. + * + * @param userId The unique identifier of the user to be deleted. + */ + @Override + public void delete(long userId) { + userRepository.deleteById(userId); + } + /** * Updates the password of a user. *