Class UserAnswerDAO
- All Implemented Interfaces:
IUserAnswerDAO
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 Summary
ConstructorsConstructorDescriptionUserAnswerDAO
(SQLiteConnection sqliteConnection) Constructs a sqliteUserAnswerDAO
connection for database operations. -
Method Summary
Modifier and TypeMethodDescriptionvoid
createUserAnswer
(UserAnswer userAnswer) Inserts a newUserAnswer
entity to the database.getAllUserQuestionAttempts
(int messageId, int questionNumber) Retrieves allUserAnswer
entities for a specific quiz question across all attempts.getAllUserQuizAnswers
(int messageId, int attempt) Retrieves allUserAnswer
entities for a specific quiz attempt.getAllUserQuizAttempts
(int messageId) Retrieves allUserAnswer
entities for a specific quiz across all attempts.getUserQuestionAnswer
(int messageId, int attempt, int questionNumber) Retrieves aUserAnswer
entity
-
Constructor Details
-
UserAnswerDAO
Constructs a sqliteUserAnswerDAO
connection for database operations.- Parameters:
sqliteConnection
- theSQLiteConnection
instance for database access- Throws:
SQLException
- if a database error occurs during initialisationRuntimeException
- if the SQLite connection cannot be established
-
-
Method Details
-
createUserAnswer
Inserts a newUserAnswer
entity to the database.This method inserts a
UserAnswer
entity into theuserAnswers
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 thequizQuestions
table.- Specified by:
createUserAnswer
in interfaceIUserAnswerDAO
- Parameters:
userAnswer
- theUserAnswer
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 aUserAnswer
entityThis method fetches a single
UserAnswer
entity from theuserAnswers
table that matches the specified composite key. Returnsnull
if no user answer is found for the given key.- Specified by:
getUserQuestionAnswer
in interfaceIUserAnswerDAO
- Parameters:
messageId
- the ID of the associated quizattempt
- the attempt number for the quizquestionNumber
- the question number within the quiz- Returns:
- the
UserAnswer
entity, ornull
if none exists - Throws:
IllegalArgumentException
- ifmessageId
,attempt
, orquestionNumber
is negativeSQLException
- if a database error occurs during retrieval
-
getAllUserQuestionAttempts
public List<UserAnswer> getAllUserQuestionAttempts(int messageId, int questionNumber) throws IllegalArgumentException, SQLException Retrieves allUserAnswer
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 ofUserAnswer
entities, which may be empty if no answers are found for the question.- Specified by:
getAllUserQuestionAttempts
in interfaceIUserAnswerDAO
- Parameters:
messageId
- the ID of the associated quizquestionNumber
- the question number within the quiz- Returns:
- a
List
ofUserAnswer
entities for the question, or an empty list if none exist - Throws:
IllegalArgumentException
- ifmessageId
orquestionNumber
is negativeSQLException
- if a database error occurs during retrieval
-
getAllUserQuizAnswers
public List<UserAnswer> getAllUserQuizAnswers(int messageId, int attempt) throws IllegalArgumentException, SQLException Retrieves allUserAnswer
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 ofUserAnswer
entities, which may be empty if no answers are found for the attempt.- Specified by:
getAllUserQuizAnswers
in interfaceIUserAnswerDAO
- Parameters:
messageId
- the ID of the associated quizattempt
- the attempt number for the quiz- Returns:
- a
List
ofUserAnswer
entities for the attempt, or an empty list if none exist - Throws:
IllegalArgumentException
- ifmessageId
orattempt
is negativeSQLException
- if a database error occurs during retrieval
-
getAllUserQuizAttempts
public List<UserAnswer> getAllUserQuizAttempts(int messageId) throws IllegalArgumentException, SQLException Retrieves allUserAnswer
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 ofUserAnswer
entities, which may be empty if no answers are found for the quiz.- Specified by:
getAllUserQuizAttempts
in interfaceIUserAnswerDAO
- Parameters:
messageId
- the ID of the associated quiz- Returns:
- a
List
ofUserAnswer
entities for the quiz, or an empty list if none exist - Throws:
IllegalArgumentException
- ifmessageId
is negativeSQLException
- if a database error occurs during retrieval
-