49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from typing import List, Optional, Dict, Any, Union
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.student_answer import StudentAnswer
|
|
from app.schemas.student_answer import StudentAnswerCreate, StudentAnswerUpdate
|
|
|
|
|
|
class CRUDStudentAnswer(CRUDBase[StudentAnswer, StudentAnswerCreate, StudentAnswerUpdate]):
|
|
def get_by_exam_result(self, db: Session, *, exam_result_id: int) -> List[StudentAnswer]:
|
|
"""Get all answers for a specific exam result"""
|
|
return db.query(self.model).filter(StudentAnswer.exam_result_id == exam_result_id).all()
|
|
|
|
def get_by_question(
|
|
self, db: Session, *, exam_result_id: int, question_id: int
|
|
) -> Optional[StudentAnswer]:
|
|
"""Get a student's answer for a specific question in an exam"""
|
|
return (
|
|
db.query(self.model)
|
|
.filter(
|
|
StudentAnswer.exam_result_id == exam_result_id,
|
|
StudentAnswer.question_id == question_id
|
|
)
|
|
.first()
|
|
)
|
|
|
|
def update_answer(
|
|
self,
|
|
db: Session,
|
|
*,
|
|
db_obj: StudentAnswer,
|
|
obj_in: Union[StudentAnswerUpdate, Dict[str, Any]]
|
|
) -> StudentAnswer:
|
|
"""Update a student's answer"""
|
|
return super().update(db, db_obj=db_obj, obj_in=obj_in)
|
|
|
|
def grade_answer(
|
|
self, db: Session, *, db_obj: StudentAnswer, is_correct: bool, points_earned: float
|
|
) -> StudentAnswer:
|
|
"""Grade a student's answer"""
|
|
db_obj.is_correct = is_correct
|
|
db_obj.points_earned = points_earned
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
student_answer = CRUDStudentAnswer(StudentAnswer) |