java.lang.Object
ai.tutor.cab302exceptionalhandlers.model.QuizDAO
All Implemented Interfaces:
IQuizDAO

public class QuizDAO extends Object implements IQuizDAO
Conducts database operations for managing quizzes

This Data Access Object (DAO) provides methods to perform CRUD operations and queries on the quizzes table in the SQLite database. Quizzes are associated with messages via a message ID, representing AI-generated quiz content within chat sessions.

Author:
Joshua M.
  • Constructor Details

    • QuizDAO

      public QuizDAO(SQLiteConnection sqliteConnection) throws SQLException, RuntimeException
      Initialises the QuizDAO 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

    • createQuiz

      public void createQuiz(Quiz quiz) throws SQLException
      Saves a new Quiz entity to the database.
      Specified by:
      createQuiz in interface IQuizDAO
      Parameters:
      quiz - the Quiz entity to save
      Throws:
      SQLException - if a database error occurs during insertion
    • getQuiz

      public Quiz getQuiz(int messageId) throws SQLException
      Retrieves a Quiz entity by its associated message ID.

      This method fetches a single Quiz entity from the quizzes table that matches the specified message ID. Returns null if no quiz is found for the given message ID.

      Specified by:
      getQuiz in interface IQuizDAO
      Parameters:
      messageId - the ID of the associated message
      Returns:
      the Quiz entity, or null if none exists
      Throws:
      SQLException - if a database error occurs during retrieval
    • getAllChatQuizzes

      public List<Quiz> getAllChatQuizzes(int chatId) throws SQLException
      Retrieves all Quiz entities for a specific chat session.

      This method fetches all quizzes associated with AI-generated messages marked as quizzes (where fromUser is false and isQuiz is true) within the specified chat session. Returns a list of Quiz entities, which may be empty if no quizzes are found for the chat ID.

      Specified by:
      getAllChatQuizzes in interface IQuizDAO
      Parameters:
      chatId - the ID of the chat session
      Returns:
      a List of Quiz entities for the chat, or an empty list if none exist
      Throws:
      SQLException - if a database error occurs during retrieval
    • getAllUserQuizzes

      public List<Quiz> getAllUserQuizzes(int userId) throws SQLException
      Retrieves all Quiz entities for a specific user across all their chat sessions.

      This method fetches all quizzes associated with AI-generated messages marked as quizzes from all chat sessions linked to the given user ID. Returns a list of Quiz entities, which may be empty if no quizzes are found for the user.

      Specified by:
      getAllUserQuizzes in interface IQuizDAO
      Parameters:
      userId - the ID of the user
      Returns:
      a List of Quiz entities for the user, or an empty list if none exist
      Throws:
      SQLException - if a database error occurs during retrieval