Class UserService

java.lang.Object
com.needleandstitch.pavuk.service.UserService

@Service public class UserService extends Object
Service layer for managing users.

This class provides business logic for operations related to users, such as retrieving, creating, updating, and deleting users.

Usage:

  • Retrieve roles by ID or email
  • Create new users
  • Update existing users
  • Delete users
  • Handle user authentication
Since:
15.12.2024
Version:
1.0.0
Author:
Needle & Stitch
  • Constructor Details

    • UserService

      public UserService()
  • Method Details

    • findById

      @Transactional(readOnly=true) public User findById(Long id)
      Finds a user by their ID.
      Parameters:
      id - The ID of the user to find
      Returns:
      The user with the specified ID
      Throws:
      jakarta.persistence.EntityNotFoundException - if the user is not found
    • findByEmail

      @Transactional(readOnly=true) public User findByEmail(String email)
      Finds a user by their email address.
      Parameters:
      email - The email address of the user to find
      Returns:
      The user with the specified email
      Throws:
      jakarta.persistence.EntityNotFoundException - if the user is not found
    • findAll

      @Transactional(readOnly=true) public List<User> findAll()
      Retrieves all users from the database.
      Returns:
      A list of all users
    • createUser

      @Transactional public User createUser(String firstName, String lastName, LocalDate dateOfBirth, String phone, String email, Boolean newsletterSubscription, String password, Role role)
      Creates a new user.
      Parameters:
      firstName - The first name of the user
      lastName - The last name of the user
      dateOfBirth - The date of birth of the user
      phone - The phone number of the user
      email - The email address of the user
      newsletterSubscription - Whether the user is subscribed to the newsletter
      password - The password of the user
      role - The role assigned to the user
      Returns:
      The newly created user
    • signInUser

      @Transactional(readOnly=true) public User signInUser(String email, String password)
      Authenticates a user by email and password.
      Parameters:
      email - The email address of the user
      password - The password of the user
      Returns:
      The authenticated user
      Throws:
      jakarta.persistence.EntityNotFoundException - if the user with the specified email is not found
      IllegalArgumentException - if the password does not match the stored password
    • updateUser

      @Transactional public void updateUser(Long id, String email)
      Updates the email address of a user.
      Parameters:
      id - The ID of the user to update
      email - The new email address for the user
    • deleteUser

      @Transactional(isolation=READ_COMMITTED) public void deleteUser(Long id)
      Deletes a user by their ID.
      Parameters:
      id - The ID of the user to delete
      Throws:
      jakarta.persistence.EntityNotFoundException - if the user with the specified ID does not exist