feat: Generated endpoint endpoints/create-quiz.post.py via AI for Biblequiz
This commit is contained in:
parent
2097cd34d2
commit
63282f3ba3
@ -0,0 +1,53 @@
|
||||
"""create tables for bible quiz
|
||||
Revision ID: 2c3f98a4d512
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-23 11:05:42.300123
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
import uuid
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '2c3f98a4d512'
|
||||
down_revision = '0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'bible_quizzes',
|
||||
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||
sa.Column('name', sa.String(), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True, server_default='1'),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True, server_default=func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True, server_default=func.now(), onupdate=func.now())
|
||||
)
|
||||
op.create_table(
|
||||
'bible_quiz_questions',
|
||||
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||
sa.Column('question_text', sa.Text(), nullable=False),
|
||||
sa.Column('quiz_id', sa.String(36), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True, server_default=func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True, server_default=func.now(), onupdate=func.now()),
|
||||
sa.ForeignKeyConstraint(['quiz_id'], ['bible_quizzes.id'], )
|
||||
)
|
||||
op.create_table(
|
||||
'bible_quiz_answers',
|
||||
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||
sa.Column('answer_text', sa.Text(), nullable=False),
|
||||
sa.Column('is_correct', sa.Boolean(), nullable=True, server_default='0'),
|
||||
sa.Column('question_id', sa.String(36), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True, server_default=func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True, server_default=func.now(), onupdate=func.now()),
|
||||
sa.ForeignKeyConstraint(['question_id'], ['bible_quiz_questions.id'], )
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('bible_quiz_answers')
|
||||
op.drop_table('bible_quiz_questions')
|
||||
op.drop_table('bible_quizzes')
|
@ -0,0 +1,16 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.biblequiz import BibleQuizSchema, BibleQuizCreate
|
||||
from helpers.biblequiz_helpers import create_bible_quiz
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/create-quiz", status_code=status.HTTP_201_CREATED, response_model=BibleQuizSchema)
|
||||
async def create_new_quiz(
|
||||
quiz_data: BibleQuizCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Create a new Bible quiz"""
|
||||
new_quiz = create_bible_quiz(db=db, quiz_data=quiz_data)
|
||||
return new_quiz
|
81
helpers/biblequiz_helpers.py
Normal file
81
helpers/biblequiz_helpers.py
Normal file
@ -0,0 +1,81 @@
|
||||
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()
|
42
models/biblequiz.py
Normal file
42
models/biblequiz.py
Normal file
@ -0,0 +1,42 @@
|
||||
from sqlalchemy import Column, String, Boolean, DateTime, ForeignKey, Text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
from core.database import Base
|
||||
import uuid
|
||||
|
||||
class BibleQuiz(Base):
|
||||
__tablename__ = "bible_quizzes"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(Text, nullable=True)
|
||||
is_active = Column(Boolean, default=True)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
questions = relationship("BibleQuizQuestion", back_populates="quiz")
|
||||
|
||||
class BibleQuizQuestion(Base):
|
||||
__tablename__ = "bible_quiz_questions"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
question_text = Column(Text, nullable=False)
|
||||
quiz_id = Column(UUID, ForeignKey("bible_quizzes.id"), nullable=False)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
quiz = relationship("BibleQuiz", back_populates="questions")
|
||||
answers = relationship("BibleQuizAnswer", back_populates="question")
|
||||
|
||||
class BibleQuizAnswer(Base):
|
||||
__tablename__ = "bible_quiz_answers"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
answer_text = Column(Text, nullable=False)
|
||||
is_correct = Column(Boolean, default=False)
|
||||
question_id = Column(UUID, ForeignKey("bible_quiz_questions.id"), nullable=False)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
question = relationship("BibleQuizQuestion", back_populates="answers")
|
75
schemas/biblequiz.py
Normal file
75
schemas/biblequiz.py
Normal file
@ -0,0 +1,75 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
# Base schema for BibleQuiz
|
||||
class BibleQuizBase(BaseModel):
|
||||
name: str = Field(..., description="Name of the Bible quiz")
|
||||
description: Optional[str] = Field(None, description="Description of the Bible quiz")
|
||||
is_active: bool = Field(True, description="Whether the Bible quiz is active or not")
|
||||
|
||||
# Schema for creating a new BibleQuiz
|
||||
class BibleQuizCreate(BibleQuizBase):
|
||||
pass
|
||||
|
||||
# Schema for updating an existing BibleQuiz
|
||||
class BibleQuizUpdate(BibleQuizBase):
|
||||
name: Optional[str] = Field(None, description="Name of the Bible quiz")
|
||||
description: Optional[str] = Field(None, description="Description of the Bible quiz")
|
||||
is_active: Optional[bool] = Field(None, description="Whether the Bible quiz is active or not")
|
||||
|
||||
# Schema for representing a BibleQuiz in responses
|
||||
class BibleQuizSchema(BibleQuizBase):
|
||||
id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
# Base schema for BibleQuizQuestion
|
||||
class BibleQuizQuestionBase(BaseModel):
|
||||
question_text: str = Field(..., description="Text of the Bible quiz question")
|
||||
|
||||
# Schema for creating a new BibleQuizQuestion
|
||||
class BibleQuizQuestionCreate(BibleQuizQuestionBase):
|
||||
quiz_id: UUID = Field(..., description="ID of the associated Bible quiz")
|
||||
|
||||
# Schema for updating an existing BibleQuizQuestion
|
||||
class BibleQuizQuestionUpdate(BibleQuizQuestionBase):
|
||||
question_text: Optional[str] = Field(None, description="Text of the Bible quiz question")
|
||||
|
||||
# Schema for representing a BibleQuizQuestion in responses
|
||||
class BibleQuizQuestionSchema(BibleQuizQuestionBase):
|
||||
id: UUID
|
||||
quiz_id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
# Base schema for BibleQuizAnswer
|
||||
class BibleQuizAnswerBase(BaseModel):
|
||||
answer_text: str = Field(..., description="Text of the Bible quiz answer")
|
||||
is_correct: bool = Field(False, description="Whether the answer is correct or not")
|
||||
|
||||
# Schema for creating a new BibleQuizAnswer
|
||||
class BibleQuizAnswerCreate(BibleQuizAnswerBase):
|
||||
question_id: UUID = Field(..., description="ID of the associated Bible quiz question")
|
||||
|
||||
# Schema for updating an existing BibleQuizAnswer
|
||||
class BibleQuizAnswerUpdate(BibleQuizAnswerBase):
|
||||
answer_text: Optional[str] = Field(None, description="Text of the Bible quiz answer")
|
||||
is_correct: Optional[bool] = Field(None, description="Whether the answer is correct or not")
|
||||
|
||||
# Schema for representing a BibleQuizAnswer in responses
|
||||
class BibleQuizAnswerSchema(BibleQuizAnswerBase):
|
||||
id: UUID
|
||||
question_id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user