81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
from typing import Optional, List
|
|
from uuid import UUID
|
|
from sqlalchemy.orm import Session
|
|
from models import BibleQuiz, BibleQuizQuestion, BibleQuizAnswer
|
|
from schemas import BibleQuizCreate, BibleQuizQuestionCreate, BibleQuizAnswerCreate
|
|
|
|
def create_bible_quiz(db: Session, quiz_data: BibleQuizCreate) -> BibleQuiz:
|
|
"""
|
|
Creates a new Bible quiz in the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
quiz_data (BibleQuizCreate): The data for the Bible quiz to create.
|
|
|
|
Returns:
|
|
BibleQuiz: The newly created Bible quiz object.
|
|
"""
|
|
db_quiz = BibleQuiz(**quiz_data.dict())
|
|
db.add(db_quiz)
|
|
db.commit()
|
|
db.refresh(db_quiz)
|
|
return db_quiz
|
|
|
|
def create_bible_quiz_question(db: Session, question_data: BibleQuizQuestionCreate) -> BibleQuizQuestion:
|
|
"""
|
|
Creates a new Bible quiz question in the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
question_data (BibleQuizQuestionCreate): The data for the Bible quiz question to create.
|
|
|
|
Returns:
|
|
BibleQuizQuestion: The newly created Bible quiz question object.
|
|
"""
|
|
db_question = BibleQuizQuestion(**question_data.dict())
|
|
db.add(db_question)
|
|
db.commit()
|
|
db.refresh(db_question)
|
|
return db_question
|
|
|
|
def create_bible_quiz_answer(db: Session, answer_data: BibleQuizAnswerCreate) -> BibleQuizAnswer:
|
|
"""
|
|
Creates a new Bible quiz answer in the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
answer_data (BibleQuizAnswerCreate): The data for the Bible quiz answer to create.
|
|
|
|
Returns:
|
|
BibleQuizAnswer: The newly created Bible quiz answer object.
|
|
"""
|
|
db_answer = BibleQuizAnswer(**answer_data.dict())
|
|
db.add(db_answer)
|
|
db.commit()
|
|
db.refresh(db_answer)
|
|
return db_answer
|
|
|
|
def get_bible_quiz_by_id(db: Session, quiz_id: UUID) -> Optional[BibleQuiz]:
|
|
"""
|
|
Retrieves a single Bible quiz by its ID.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
quiz_id (UUID): The ID of the Bible quiz to retrieve.
|
|
|
|
Returns:
|
|
Optional[BibleQuiz]: The Bible quiz object if found, otherwise None.
|
|
"""
|
|
return db.query(BibleQuiz).filter(BibleQuiz.id == quiz_id).first()
|
|
|
|
def get_all_bible_quizzes(db: Session) -> List[BibleQuiz]:
|
|
"""
|
|
Retrieves all Bible quizzes from the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
|
|
Returns:
|
|
List[BibleQuiz]: A list of all Bible quiz objects.
|
|
"""
|
|
return db.query(BibleQuiz).all() |