Spaces:
Runtime error
Runtime error
Greg Claude Opus 4.7 (1M context) commited on
Commit ·
8c218e0
1
Parent(s): bc019a7
perf(teacher-sections): eliminate N+1 in GET /teacher-sections
Browse filesRefactor _enrich to accept pre-fetched users_by_id and subjects_by_id
dicts. Build both dicts with single batch queries before the list
comprehension; _enrich now does only dict lookups.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
backend/app/api/v1/endpoints/teacher_sections.py
CHANGED
|
@@ -13,9 +13,9 @@ from app.models.section import Section
|
|
| 13 |
router = APIRouter(prefix="/teacher-sections", tags=["teacher-sections"])
|
| 14 |
|
| 15 |
|
| 16 |
-
def _enrich(ts: TeacherSection,
|
| 17 |
-
teacher =
|
| 18 |
-
sub =
|
| 19 |
return TeacherSectionResponse(
|
| 20 |
section_id=ts.section_id,
|
| 21 |
subject_id=ts.subject_id,
|
|
@@ -32,7 +32,12 @@ def list_assignments(current_user: User = Depends(get_current_user), db: Session
|
|
| 32 |
if current_user.role == "teacher":
|
| 33 |
query = query.filter(TeacherSection.teacher_id == current_user.id)
|
| 34 |
items = query.all()
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
@router.post("", response_model=TeacherSectionResponse, status_code=status.HTTP_201_CREATED)
|
|
@@ -67,7 +72,7 @@ def assign_teacher(payload: TeacherSectionCreate, current_user: User = Depends(R
|
|
| 67 |
db.add(obj)
|
| 68 |
db.commit()
|
| 69 |
db.refresh(obj)
|
| 70 |
-
return _enrich(obj,
|
| 71 |
|
| 72 |
|
| 73 |
@router.delete("/{section_id}/{teacher_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
| 13 |
router = APIRouter(prefix="/teacher-sections", tags=["teacher-sections"])
|
| 14 |
|
| 15 |
|
| 16 |
+
def _enrich(ts: TeacherSection, users_by_id: dict, subjects_by_id: dict) -> TeacherSectionResponse:
|
| 17 |
+
teacher = users_by_id.get(ts.teacher_id)
|
| 18 |
+
sub = subjects_by_id.get(ts.subject_id)
|
| 19 |
return TeacherSectionResponse(
|
| 20 |
section_id=ts.section_id,
|
| 21 |
subject_id=ts.subject_id,
|
|
|
|
| 32 |
if current_user.role == "teacher":
|
| 33 |
query = query.filter(TeacherSection.teacher_id == current_user.id)
|
| 34 |
items = query.all()
|
| 35 |
+
# Batch-load users and subjects referenced by all items
|
| 36 |
+
teacher_ids = {t.teacher_id for t in items if t.teacher_id is not None}
|
| 37 |
+
subject_ids = {t.subject_id for t in items if t.subject_id is not None}
|
| 38 |
+
users_by_id = {u.id: u for u in db.query(User).filter(User.id.in_(teacher_ids)).all()} if teacher_ids else {}
|
| 39 |
+
subjects_by_id = {s.id: s for s in db.query(Subject).filter(Subject.id.in_(subject_ids)).all()} if subject_ids else {}
|
| 40 |
+
return [_enrich(t, users_by_id, subjects_by_id) for t in items]
|
| 41 |
|
| 42 |
|
| 43 |
@router.post("", response_model=TeacherSectionResponse, status_code=status.HTTP_201_CREATED)
|
|
|
|
| 72 |
db.add(obj)
|
| 73 |
db.commit()
|
| 74 |
db.refresh(obj)
|
| 75 |
+
return _enrich(obj, {teacher.id: teacher}, {sub.id: sub})
|
| 76 |
|
| 77 |
|
| 78 |
@router.delete("/{section_id}/{teacher_id}", status_code=status.HTTP_204_NO_CONTENT)
|