Files
notytex/repositories/student_repository.py

50 lines
1.7 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import joinedload
from models import Student, ClassGroup
from .base_repository import BaseRepository
class StudentRepository(BaseRepository[Student]):
"""Repository pour les étudiants."""
def __init__(self):
super().__init__(Student)
def find_by_class_ordered(self, class_group_id: int) -> List[Student]:
"""Trouve les étudiants d'une classe triés par nom."""
return Student.query.filter_by(
class_group_id=class_group_id
).order_by(
Student.last_name,
Student.first_name
).all()
def find_all_with_class_ordered(self) -> List[Student]:
"""Trouve tous les étudiants avec leur classe, triés par classe puis nom."""
return Student.query.options(
joinedload(Student.class_group)
).join(
ClassGroup
).order_by(
ClassGroup.name,
Student.last_name,
Student.first_name
).all()
def find_by_class_group(self, class_group_id: int) -> List[Student]:
"""Trouve tous les étudiants d'une classe."""
return Student.query.filter_by(
class_group_id=class_group_id
).all()
def count_by_class_group(self, class_group_id: int) -> int:
"""Compte les étudiants d'une classe."""
return Student.query.filter_by(
class_group_id=class_group_id
).count()
def find_with_class_group(self, id: int) -> Optional[Student]:
"""Trouve un étudiant avec sa classe."""
return Student.query.options(
joinedload(Student.class_group)
).filter_by(id=id).first()