45 lines
1.5 KiB
Python

from typing import List
from sqlalchemy.orm import Session
from datetime import datetime
from app.crud.base import CRUDBase
from app.models.exam import Exam
from app.schemas.exam import ExamCreate, ExamUpdate
class CRUDExam(CRUDBase[Exam, ExamCreate, ExamUpdate]):
def get_by_course_id(self, db: Session, *, course_id: int) -> List[Exam]:
"""Get all exams for a specific course"""
return db.query(self.model).filter(Exam.course_id == course_id).all()
def get_by_creator(self, db: Session, *, creator_id: int) -> List[Exam]:
"""Get all exams created by a specific user"""
return db.query(self.model).filter(Exam.created_by == creator_id).all()
def get_active_exams(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Exam]:
"""Get all active exams"""
return (
db.query(self.model)
.filter(Exam.is_active)
.offset(skip)
.limit(limit)
.all()
)
def get_available_exams(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Exam]:
"""Get all exams that are currently available (active and within time window)"""
now = datetime.utcnow()
return (
db.query(self.model)
.filter(
Exam.is_active,
(Exam.start_time.is_(None) | (Exam.start_time <= now)),
(Exam.end_time.is_(None) | (Exam.end_time >= now)),
)
.offset(skip)
.limit(limit)
.all()
)
exam = CRUDExam(Exam)