Class UserDao

java.lang.Object
persistence.dao.UserDao
All Implemented Interfaces:
UserRepository

public class UserDao extends Object implements UserRepository
Data Access Object for user-related database operations. Implements UserRepository and provides methods for inserting users and looking them up by email, username, or either.
  • Constructor Details

    • UserDao

      public UserDao()
  • Method Details

    • insert

      public void insert(User user)
      Inserts a new user into the database. Upon successful insertion, the generated database ID is set on the user object.
      Specified by:
      insert in interface UserRepository
      Parameters:
      user - the User to insert; must not be null and must have a unique email and username
      Throws:
      RuntimeException - if a database error occurs, including unique constraint violations
    • findByEmail

      public Optional<User> findByEmail(String email)
      Finds a user by their email address.
      Specified by:
      findByEmail in interface UserRepository
      Parameters:
      email - the email address to search for; leading/trailing whitespace is trimmed
      Returns:
      an Optional containing the user if found, or empty if no match exists
      Throws:
      RuntimeException - if a database error occurs during the query
    • findByUsername

      public Optional<User> findByUsername(String username)
      Finds a user by their username.
      Specified by:
      findByUsername in interface UserRepository
      Parameters:
      username - the username to search for
      Returns:
      an Optional containing the user if found, or empty if no match exists
      Throws:
      RuntimeException - if a database error occurs during the query
    • findByLogin

      public Optional<User> findByLogin(String login)
      Finds a user by either email or username. First attempts to match by email, then falls back to username if no email match is found.
      Specified by:
      findByLogin in interface UserRepository
      Parameters:
      login - the email or username to search for; returns empty if null or blank
      Returns:
      an Optional containing the user if found, or empty if no match exists
    • existsByEmail

      public boolean existsByEmail(String email)
      Checks whether a user with the given email address already exists in the database.
      Specified by:
      existsByEmail in interface UserRepository
      Parameters:
      email - the email address to check
      Returns:
      true if a user with the given email exists, false otherwise
      Throws:
      RuntimeException - if a database error occurs during the query
    • existsByUsername

      public boolean existsByUsername(String username)
      Checks whether a user with the given username already exists in the database.
      Specified by:
      existsByUsername in interface UserRepository
      Parameters:
      username - the username to check
      Returns:
      true if a user with the given username exists, false otherwise
      Throws:
      RuntimeException - if a database error occurs during the query