java.lang.Object
ai.tutor.cab302exceptionalhandlers.model.UserDAO
All Implemented Interfaces:
IUserDAO

public class UserDAO extends Object implements IUserDAO
Provides SQL operations for managing User entities in the SQLite database.

This Data Access Object (DAO) provides methods to perform CRUD operations on the users table in the SQLite database. Users are identified by a unique ID and username, with a stored password hash, supporting authentication and user management within the application.

Author:
Joshua M.
  • Constructor Details

    • UserDAO

      public UserDAO(SQLiteConnection sqliteConnection) throws SQLException, RuntimeException
      Creates a UserDAO table with an SQLite database connection.
      Parameters:
      sqliteConnection - the SQLiteConnection instance for database access
      Throws:
      SQLException - if a database error occurs during initialisation
      RuntimeException - if the SQLite connection cannot be established
  • Method Details

    • createUser

      public void createUser(User user) throws SQLException
      Inserts a new User entity to the database.

      This method inserts a User entity into the users table, storing its username and password hash. The user ID is automatically generated by the SQLite database and set on the User entity to ensure uniqueness.

      Specified by:
      createUser in interface IUserDAO
      Parameters:
      user - the User entity to save
      Throws:
      SQLException - if a database error occurs during insertion
    • updateUser

      public void updateUser(User user) throws SQLException
      Updates an existing User entity in the database.

      This method updates the username and password hash of a User entity in the users table, identified by the user’s ID.

      Specified by:
      updateUser in interface IUserDAO
      Parameters:
      user - the User entity to update
      Throws:
      SQLException - if a database error occurs during update
    • deleteUser

      public void deleteUser(User user) throws SQLException
      Removes a User entity from the database.

      This method removes a User entity from the users table identified by the user’s ID. Cascading deletions in the chats and messages tables, handled by foreign key constraints.

      Specified by:
      deleteUser in interface IUserDAO
      Parameters:
      user - the User entity to delete
      Throws:
      SQLException - if a database error occurs during deletion
    • getUser

      public User getUser(int id) throws IllegalArgumentException, SQLException
      Retrieves a User entity by its ID.

      This method fetches a single User entity from the users table that matches the specified ID. Returns null if no user is found for the given ID.

      Specified by:
      getUser in interface IUserDAO
      Parameters:
      id - the ID of the user
      Returns:
      the User entity, or null if none exists
      Throws:
      IllegalArgumentException - if id is negative
      SQLException - if a database error occurs during retrieval
    • getUser

      public User getUser(String username) throws IllegalArgumentException, SQLException
      Retrieves a User entity by its username.

      This method fetches a single User entity from the users table that matches the specified username. Returns null if no user is found for the given username.

      Specified by:
      getUser in interface IUserDAO
      Parameters:
      username - the username of the user
      Returns:
      the User entity, or null if none exists
      Throws:
      IllegalArgumentException - if username is null or empty
      SQLException - if a database error occurs during retrieval
    • getAllUsers

      public List<User> getAllUsers() throws SQLException
      Retrieves all User entities from the database.

      This method fetches all users from the users table. Returns a list of User entities, which may be empty if no users are found.

      Specified by:
      getAllUsers in interface IUserDAO
      Returns:
      a List of User entities, or an empty list if none exist
      Throws:
      SQLException - if a database error occurs during retrieval