From cf3fea1c98e1d73a2e8834a42be822f68e05b0f3 Mon Sep 17 00:00:00 2001 From: Yehia Mohamed <102627389+YehiaFarghaly@users.noreply.github.com> Date: Fri, 2 May 2025 20:12:41 +0300 Subject: [PATCH] Resolve #2 add delete end point --- .../auth/controller/AdminController.java | 14 +++++++++ .../podzilla/auth/service/AdminService.java | 21 ++++++++----- .../podzilla/auth/service/UserService.java | 30 +++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/podzilla/auth/service/UserService.java diff --git a/src/main/java/com/podzilla/auth/controller/AdminController.java b/src/main/java/com/podzilla/auth/controller/AdminController.java index 71e425f..fa2e911 100644 --- a/src/main/java/com/podzilla/auth/controller/AdminController.java +++ b/src/main/java/com/podzilla/auth/controller/AdminController.java @@ -46,4 +46,18 @@ public void updateUserActivation( logger.debug("Admin requested to update activation status for userId={} to isActive={}", userId, isActive); adminService.updateUserActivation(userId, isActive); } + + + @DeleteMapping("/users/{userId}") + @Operation(summary = "Delete a user", + description = "Allows an admin to delete a specific user account.") + @ApiResponse(responseCode = "200", + description = "User deleted successfully") + public void deleteUser( + @Parameter(description = "ID of the user to delete") + @PathVariable final Long userId) { + + logger.debug("Admin requested to delete user with userId={}", userId); + adminService.deleteUser(userId); + } } diff --git a/src/main/java/com/podzilla/auth/service/AdminService.java b/src/main/java/com/podzilla/auth/service/AdminService.java index b27701a..9f86742 100644 --- a/src/main/java/com/podzilla/auth/service/AdminService.java +++ b/src/main/java/com/podzilla/auth/service/AdminService.java @@ -16,9 +16,11 @@ public class AdminService { private static final Logger logger = LoggerFactory.getLogger(AdminService.class); private final UserRepository userRepository; + private final UserService userService; - public AdminService(final UserRepository userRepository) { + public AdminService(final UserRepository userRepository, final UserService userService) { this.userRepository = userRepository; + this.userService = userService; } public List getUsers() { @@ -27,16 +29,19 @@ public List getUsers() { @Transactional public void updateUserActivation(final Long userId, final boolean isActive) { - logger.debug("Fetching user with id={}", userId); - User user = userRepository.findById(userId) - .orElseThrow(() -> { - logger.warn("User not found with id={}", userId); - return new NotFoundException("User with id " + userId + " does not exist."); - }); - + User user = userService.getUserOrThrow(userId); logger.debug("Updating isActive status for userId={} from {} to {}", userId, user.getIsActive(), isActive); user.setIsActive(isActive); userRepository.save(user); logger.debug("User activation status updated successfully for userId={}", userId); } + + + @Transactional + public void deleteUser(final Long userId) { + User user = userService.getUserOrThrow(userId); + logger.debug("Deleting user with userId={}", userId); + userRepository.delete(user); + logger.debug("User deleted successfully with userId={}", userId); + } } diff --git a/src/main/java/com/podzilla/auth/service/UserService.java b/src/main/java/com/podzilla/auth/service/UserService.java new file mode 100644 index 0000000..24d660a --- /dev/null +++ b/src/main/java/com/podzilla/auth/service/UserService.java @@ -0,0 +1,30 @@ +package com.podzilla.auth.service; + +import com.podzilla.auth.exception.NotFoundException; +import com.podzilla.auth.model.User; +import com.podzilla.auth.repository.UserRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + private static final Logger logger = LoggerFactory.getLogger(UserService.class); + + private final UserRepository userRepository; + + public UserService(final UserRepository userRepository) { + this.userRepository = userRepository; + } + + + public User getUserOrThrow(final Long userId) { + logger.debug("Fetching user with id={}", userId); + return userRepository.findById(userId) + .orElseThrow(() -> { + logger.warn("User not found with id={}", userId); + return new NotFoundException("User with id " + userId + " does not exist."); + }); + } +}