Class UserService
java.lang.Object
com.goldenleaf.shop.service.UserService
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 Summary
ConstructorsConstructorDescriptionUserService(UserRepository repo, org.springframework.security.crypto.password.PasswordEncoder encoder) Constructs a newUserServicewith the provided repository and password encoder. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a newUserto the database.voidUpdates an existingUser.Retrieves all users from the database.getUserById(Long id) Retrieves aUserby their unique ID.getUserByLogin(String login) Retrieves aUserby their login.getUserByName(String name) Retrieves aUserby their name.voidRegisters a newUserwith the given password.voidremoveUser(User user) Removes an existingUserfrom the database.voidremoveUserById(Long id) Removes aUserby their ID.voidremoveUserByLogin(String login) Removes aUserby their login.
-
Constructor Details
-
UserService
public UserService(UserRepository repo, org.springframework.security.crypto.password.PasswordEncoder encoder) Constructs a newUserServicewith the provided repository and password encoder.- Parameters:
repo- the repository used to perform operations on usersencoder- the password encoder used to hash passwords- Throws:
IllegalArgumentException- ifrepoorencoderisnull- See Also:
-
-
Method Details
-
getAllUsers
-
getUserById
Retrieves aUserby their unique ID.- Parameters:
id- the ID of the user- Returns:
- the
Userwith the specified ID - Throws:
RuntimeException- if no user with the given ID exists- See Also:
-
getUserByName
Retrieves aUserby their name.- Parameters:
name- the name of the user- Returns:
- the
Userwith the specified name - Throws:
RuntimeException- if no user with the given name exists- See Also:
-
getUserByLogin
Retrieves aUserby their login.- Parameters:
login- the login of the user- Returns:
- the
Userwith the specified login - Throws:
RuntimeException- if no user with the given login exists- See Also:
-
addUser
-
removeUser
-
removeUserById
-
removeUserByLogin
Removes aUserby their login.- Parameters:
login- the login of the user to delete- Throws:
RuntimeException- if no user with the given login exists- See Also:
-
editUser
Updates an existingUser.The user must have a valid ID that exists in the database. Otherwise, a
RuntimeExceptionis thrown.- Parameters:
user- the user to update- Throws:
RuntimeException- if the user does not exist or ID is null- See Also:
-
register
-