Class UserDao
java.lang.Object
persistence.dao.UserDao
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanexistsByEmail(String email) Checks whether a user with the given email address already exists in the database.booleanexistsByUsername(String username) Checks whether a user with the given username already exists in the database.findByEmail(String email) Finds a user by their email address.findByLogin(String login) Finds a user by either email or username.findByUsername(String username) Finds a user by their username.voidInserts a new user into the database.
-
Constructor Details
-
UserDao
public UserDao()
-
-
Method Details
-
insert
Inserts a new user into the database. Upon successful insertion, the generated database ID is set on the user object.- Specified by:
insertin interfaceUserRepository- Parameters:
user- theUserto 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
Finds a user by their email address.- Specified by:
findByEmailin interfaceUserRepository- Parameters:
email- the email address to search for; leading/trailing whitespace is trimmed- Returns:
- an
Optionalcontaining the user if found, or empty if no match exists - Throws:
RuntimeException- if a database error occurs during the query
-
findByUsername
Finds a user by their username.- Specified by:
findByUsernamein interfaceUserRepository- Parameters:
username- the username to search for- Returns:
- an
Optionalcontaining the user if found, or empty if no match exists - Throws:
RuntimeException- if a database error occurs during the query
-
findByLogin
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:
findByLoginin interfaceUserRepository- Parameters:
login- the email or username to search for; returns empty if null or blank- Returns:
- an
Optionalcontaining the user if found, or empty if no match exists
-
existsByEmail
Checks whether a user with the given email address already exists in the database.- Specified by:
existsByEmailin interfaceUserRepository- Parameters:
email- the email address to check- Returns:
trueif a user with the given email exists,falseotherwise- Throws:
RuntimeException- if a database error occurs during the query
-
existsByUsername
Checks whether a user with the given username already exists in the database.- Specified by:
existsByUsernamein interfaceUserRepository- Parameters:
username- the username to check- Returns:
trueif a user with the given username exists,falseotherwise- Throws:
RuntimeException- if a database error occurs during the query
-