Class UserServiceImpl
- All Implemented Interfaces:
UserService
UserService that provides business logic
for managing User entities.
This service handles user creation, login time updates, retrieval by email,
existence checks, and paginated queries. It interacts with the
UserRepository and RoleRepository for persistence,
and uses UserConverter to transform entities into DTOs.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateUser(String role, UserDto userDto) Creates a new user with the specified role.findUserByEmail(String userEmail) Finds a user by their email address.findUserById(String id) Retrieves a user by their unique ID and converts the result to aUserDto.findUsersAfterId(Long lastId, int size) Retrieves a paginated list of users after a given ID.voidupdateLoginTime(String email) Updates the login time of a user identified by their email.voidupdateUserById(String id, String email, String name, String phone) Updates the user information for the specified user ID.booleanuserExistsByEmail(String userEmail) Checks whether a user exists with the given email.
-
Constructor Details
-
UserServiceImpl
public UserServiceImpl()
-
-
Method Details
-
createUser
Creates a new user with the specified role.Validates the role, checks for duplicate emails, encodes the user's password, and assigns the appropriate
Rolebefore saving.- Specified by:
createUserin interfaceUserService- Parameters:
role- the role to assign to the user (e.g., "ROLE_USER" or "ROLE_ADMIN")userDto- the DTO containing user details- Returns:
- the created
UserDto - Throws:
NullPointerException- if the role isnullNotFoundException- if the specified role does not existDuplicateEmailException- if a user with the same email already exists
-
updateLoginTime
Updates the login time of a user identified by their email.- Specified by:
updateLoginTimein interfaceUserService- Parameters:
email- the email of the user whose login time should be updated
-
findUserByEmail
Finds a user by their email address.- Specified by:
findUserByEmailin interfaceUserService- Parameters:
userEmail- the email of the user to find- Returns:
- the corresponding
UserDto, ornullif no user is found
-
userExistsByEmail
Checks whether a user exists with the given email.- Specified by:
userExistsByEmailin interfaceUserService- Parameters:
userEmail- the email to check- Returns:
trueif a user exists with the given email,falseotherwise
-
findUsersAfterId
Retrieves a paginated list of users after a given ID.This method is useful for implementing cursor-based pagination.
- Specified by:
findUsersAfterIdin interfaceUserService- Parameters:
lastId- the ID after which users should be retrievedsize- the maximum number of users to retrieve- Returns:
- a sorted list of
UserDtoobjects
-
updateUserById
Updates the user information for the specified user ID.This method performs validation on the input parameters and ensures that the new email and phone number do not conflict with existing records. It throws exceptions for invalid input, non-existent users, or duplicate data. If all checks pass, the user's email, name, and phone number are updated in the database.
- Specified by:
updateUserByIdin interfaceUserService- Parameters:
id- the unique identifier of the user to update; must be a valid numeric stringemail- the new email address; must not match the current email or any existing user's emailname- the new name; must contain only letters and spacesphone- the new phone number; must not match the current phone or any existing user's phone- Throws:
NotValidException- if the ID or name format is invalidNotFoundException- if no user exists with the given IDDuplicateException- if the email or phone number is unchanged or already used by another userRuntimeException- if an unexpected error occurs during database save
-
findUserById
Retrieves a user by their unique ID and converts the result to aUserDto.- Specified by:
findUserByIdin interfaceUserService- Parameters:
id- the unique identifier of the user to retrieve; must be a valid numeric string- Returns:
- a
UserDtorepresenting the user with the given ID
-