java.lang.Object
ai.tutor.cab302exceptionalhandlers.model.UserAnswerDAO
All Implemented Interfaces:
IUserAnswerDAO

public class UserAnswerDAO extends Object implements IUserAnswerDAO
Represents the data operations for UserAnswer entities in the SQLite database.

This Data Access Object (DAO) provides methods to perform CRUD operations and queries on the userAnswers table in the SQLite database. User answers are associated with quiz questions via a composite key of message ID, attempt number, and question number, representing user responses to quiz questions.

Author:
Joshua M.
  • Constructor Details

    • UserAnswerDAO

      public UserAnswerDAO(SQLiteConnection sqliteConnection) throws SQLException, RuntimeException
      Constructs a sqlite UserAnswerDAO connection for database operations.
      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

    • createUserAnswer

      public void createUserAnswer(UserAnswer userAnswer) throws SQLException
      Inserts a new UserAnswer entity to the database.

      This method inserts a UserAnswer entity into the userAnswers table, storing its message ID, attempt number, question number, and answer option. The message ID and question number must correspond to an existing quiz question in the quizQuestions table.

      Specified by:
      createUserAnswer in interface IUserAnswerDAO
      Parameters:
      userAnswer - the UserAnswer entity to insert
      Throws:
      SQLException - if a database error occurs during insertion
    • getUserQuestionAnswer

      public UserAnswer getUserQuestionAnswer(int messageId, int attempt, int questionNumber) throws IllegalArgumentException, SQLException
      Retrieves a UserAnswer entity

      This method fetches a single UserAnswer entity from the userAnswers table that matches the specified composite key. Returns null if no user answer is found for the given key.

      Specified by:
      getUserQuestionAnswer in interface IUserAnswerDAO
      Parameters:
      messageId - the ID of the associated quiz
      attempt - the attempt number for the quiz
      questionNumber - the question number within the quiz
      Returns:
      the UserAnswer entity, or null if none exists
      Throws:
      IllegalArgumentException - if messageId, attempt, or questionNumber is negative
      SQLException - if a database error occurs during retrieval
    • getAllUserQuestionAttempts

      public List<UserAnswer> getAllUserQuestionAttempts(int messageId, int questionNumber) throws IllegalArgumentException, SQLException
      Retrieves all UserAnswer entities for a specific quiz question across all attempts.

      This method fetches all user answers associated with the specified message ID and question number from the userAnswers table. Returns a list of UserAnswer entities, which may be empty if no answers are found for the question.

      Specified by:
      getAllUserQuestionAttempts in interface IUserAnswerDAO
      Parameters:
      messageId - the ID of the associated quiz
      questionNumber - the question number within the quiz
      Returns:
      a List of UserAnswer entities for the question, or an empty list if none exist
      Throws:
      IllegalArgumentException - if messageId or questionNumber is negative
      SQLException - if a database error occurs during retrieval
    • getAllUserQuizAnswers

      public List<UserAnswer> getAllUserQuizAnswers(int messageId, int attempt) throws IllegalArgumentException, SQLException
      Retrieves all UserAnswer entities for a specific quiz attempt.

      This method fetches all user answers associated with the specified message ID and attempt number from the userAnswers table. Returns a list of UserAnswer entities, which may be empty if no answers are found for the attempt.

      Specified by:
      getAllUserQuizAnswers in interface IUserAnswerDAO
      Parameters:
      messageId - the ID of the associated quiz
      attempt - the attempt number for the quiz
      Returns:
      a List of UserAnswer entities for the attempt, or an empty list if none exist
      Throws:
      IllegalArgumentException - if messageId or attempt is negative
      SQLException - if a database error occurs during retrieval
    • getAllUserQuizAttempts

      public List<UserAnswer> getAllUserQuizAttempts(int messageId) throws IllegalArgumentException, SQLException
      Retrieves all UserAnswer entities for a specific quiz across all attempts.

      This method fetches all user answers associated with the specified message ID from the userAnswers table. Returns a list of UserAnswer entities, which may be empty if no answers are found for the quiz.

      Specified by:
      getAllUserQuizAttempts in interface IUserAnswerDAO
      Parameters:
      messageId - the ID of the associated quiz
      Returns:
      a List of UserAnswer entities for the quiz, or an empty list if none exist
      Throws:
      IllegalArgumentException - if messageId is negative
      SQLException - if a database error occurs during retrieval