java.lang.Object
com.glowmart.shop_management.service.implementation.UserServiceImpl
All Implemented Interfaces:
UserService

@Service public class UserServiceImpl extends Object implements UserService
Implementation of 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 Details

    • UserServiceImpl

      public UserServiceImpl()
  • Method Details

    • createUser

      public UserDto createUser(String role, UserDto userDto)
      Creates a new user with the specified role.

      Validates the role, checks for duplicate emails, encodes the user's password, and assigns the appropriate Role before saving.

      Specified by:
      createUser in interface UserService
      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 is null
      NotFoundException - if the specified role does not exist
      DuplicateEmailException - if a user with the same email already exists
    • updateLoginTime

      @Transactional public void updateLoginTime(String email)
      Updates the login time of a user identified by their email.
      Specified by:
      updateLoginTime in interface UserService
      Parameters:
      email - the email of the user whose login time should be updated
    • findUserByEmail

      public UserDto findUserByEmail(String userEmail)
      Finds a user by their email address.
      Specified by:
      findUserByEmail in interface UserService
      Parameters:
      userEmail - the email of the user to find
      Returns:
      the corresponding UserDto, or null if no user is found
    • userExistsByEmail

      public boolean userExistsByEmail(String userEmail)
      Checks whether a user exists with the given email.
      Specified by:
      userExistsByEmail in interface UserService
      Parameters:
      userEmail - the email to check
      Returns:
      true if a user exists with the given email, false otherwise
    • findUsersAfterId

      public List<UserDto> findUsersAfterId(Long lastId, int size)
      Retrieves a paginated list of users after a given ID.

      This method is useful for implementing cursor-based pagination.

      Specified by:
      findUsersAfterId in interface UserService
      Parameters:
      lastId - the ID after which users should be retrieved
      size - the maximum number of users to retrieve
      Returns:
      a sorted list of UserDto objects
    • updateUserById

      public void updateUserById(String id, String email, String name, String phone)
      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:
      updateUserById in interface UserService
      Parameters:
      id - the unique identifier of the user to update; must be a valid numeric string
      email - the new email address; must not match the current email or any existing user's email
      name - the new name; must contain only letters and spaces
      phone - 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 invalid
      NotFoundException - if no user exists with the given ID
      DuplicateException - if the email or phone number is unchanged or already used by another user
      RuntimeException - if an unexpected error occurs during database save
    • findUserById

      public UserDto findUserById(String id)
      Retrieves a user by their unique ID and converts the result to a UserDto.
      Specified by:
      findUserById in interface UserService
      Parameters:
      id - the unique identifier of the user to retrieve; must be a valid numeric string
      Returns:
      a UserDto representing the user with the given ID