Class UserService

java.lang.Object
com.goldenleaf.shop.service.UserService

@Service public class UserService extends Object
Service class for managing User entities.

Provides business logic for retrieving, adding, updating, deleting, and registering users. Supports operations by user ID, name, or login. Handles password encoding via PasswordEncoder. Acts as an intermediary between controllers and the UserRepository.

See Also:
  • Constructor Details

    • UserService

      public UserService(UserRepository repo, org.springframework.security.crypto.password.PasswordEncoder encoder)
      Constructs a new UserService with the provided repository and password encoder.
      Parameters:
      repo - the repository used to perform operations on users
      encoder - the password encoder used to hash passwords
      Throws:
      IllegalArgumentException - if repo or encoder is null
      See Also:
  • Method Details

    • getAllUsers

      public List<User> getAllUsers()
      Retrieves all users from the database.
      Returns:
      a List of all User entities
      See Also:
      • ListCrudRepository.findAll()
    • getUserById

      public User getUserById(Long id)
      Retrieves a User by their unique ID.
      Parameters:
      id - the ID of the user
      Returns:
      the User with the specified ID
      Throws:
      RuntimeException - if no user with the given ID exists
      See Also:
      • CrudRepository.findById(Object)
    • getUserByName

      public User getUserByName(String name)
      Retrieves a User by their name.
      Parameters:
      name - the name of the user
      Returns:
      the User with the specified name
      Throws:
      RuntimeException - if no user with the given name exists
      See Also:
    • getUserByLogin

      public User getUserByLogin(String login)
      Retrieves a User by their login.
      Parameters:
      login - the login of the user
      Returns:
      the User with the specified login
      Throws:
      RuntimeException - if no user with the given login exists
      See Also:
    • addUser

      public void addUser(User user)
      Adds a new User to the database.
      Parameters:
      user - the user to add
      See Also:
      • CrudRepository.save(Object)
    • removeUser

      public void removeUser(User user)
      Removes an existing User from the database.

      If the user exists (by ID), it will be deleted; otherwise, nothing happens.

      Parameters:
      user - the user to remove
      See Also:
      • CrudRepository.delete(Object)
      • CrudRepository.existsById(Object)
    • removeUserById

      public void removeUserById(Long id)
      Removes a User by their ID.
      Parameters:
      id - the ID of the user to delete
      See Also:
      • CrudRepository.deleteById(Object)
    • removeUserByLogin

      public void removeUserByLogin(String login)
      Removes a User by their login.
      Parameters:
      login - the login of the user to delete
      Throws:
      RuntimeException - if no user with the given login exists
      See Also:
    • editUser

      public void editUser(User user)
      Updates an existing User.

      The user must have a valid ID that exists in the database. Otherwise, a RuntimeException is thrown.

      Parameters:
      user - the user to update
      Throws:
      RuntimeException - if the user does not exist or ID is null
      See Also:
      • CrudRepository.save(Object)
      • CrudRepository.existsById(Object)
    • register

      public void register(User user, String password)
      Registers a new User with the given password.

      The password is hashed using PasswordEncoder before saving to the database.

      Parameters:
      user - the user to register
      password - the plain-text password to encode and save
      See Also:
      • CrudRepository.save(Object)
      • PasswordEncoder.encode(CharSequence)