36 lines
1.2 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.crud.base import CRUDBase
from app.models.question import Question, QuestionType
from app.schemas.question import QuestionCreate, QuestionUpdate
class CRUDQuestion(CRUDBase[Question, QuestionCreate, QuestionUpdate]):
def get_by_exam_id(self, db: Session, *, exam_id: int) -> List[Question]:
"""Get all questions for a specific exam"""
return db.query(self.model).filter(Question.exam_id == exam_id).all()
def get_by_exam_id_and_type(
self, db: Session, *, exam_id: int, question_type: QuestionType
) -> List[Question]:
"""Get all questions of a specific type for an exam"""
return (
db.query(self.model)
.filter(
Question.exam_id == exam_id,
Question.question_type == question_type
)
.all()
)
def get_with_options(self, db: Session, *, id: int) -> Optional[Question]:
"""Get a question with its options"""
return (
db.query(self.model)
.filter(Question.id == id)
.first()
)
question = CRUDQuestion(Question)