Greg Claude Opus 4.7 (1M context) commited on
Commit
7d5d51b
·
1 Parent(s): 8c218e0

perf(teacher-subjects): eliminate N+1 in GET and PUT /teacher-subjects

Browse files

Batch-load all subjects in a single query for the GET list. For the
PUT, replace per-subject conflict-check loop with a single filtered
query plus batch user/subject lookups; also batch-validate subject
ownership before inserting.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

backend/app/api/v1/endpoints/teacher_subjects.py CHANGED
@@ -27,9 +27,12 @@ def list_teacher_subjects(
27
  elif current_user.role == "teacher":
28
  query = query.filter(TeacherSubject.teacher_id == current_user.id)
29
  items = query.all()
 
 
 
30
  result = []
31
  for ts in items:
32
- sub = db.query(Subject).filter(Subject.id == ts.subject_id).first()
33
  result.append(TeacherSubjectResponse(
34
  teacher_id=ts.teacher_id,
35
  subject_id=ts.subject_id,
@@ -51,28 +54,41 @@ def set_teacher_subjects(
51
  ).first()
52
  if not teacher:
53
  raise HTTPException(status_code=404, detail="Teacher not found")
54
- # validate: each subject can only be assigned to one teacher
55
- for sid in subject_ids:
56
- existing = db.query(TeacherSubject).filter(
57
- TeacherSubject.subject_id == sid,
58
- TeacherSubject.account_id == current_user.account_id,
59
- TeacherSubject.teacher_id != teacher_id,
60
- ).first()
61
- if existing:
62
- other = db.query(User).filter(User.id == existing.teacher_id).first()
63
- sub = db.query(Subject).filter(Subject.id == sid).first()
64
- other_name = f"{other.lastname}, {other.name}" if other else f"#{existing.teacher_id}"
65
- sub_name = sub.name if sub else f"#{sid}"
66
- raise HTTPException(
67
- status_code=status.HTTP_409_CONFLICT,
68
- detail=f"La materia '{sub_name}' ya está asignada al docente {other_name}"
69
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  db.query(TeacherSubject).filter(TeacherSubject.teacher_id == teacher_id).delete()
71
  for sid in subject_ids:
72
- sub = db.query(Subject).filter(
73
- Subject.id == sid, Subject.account_id == current_user.account_id
74
- ).first()
75
- if sub:
76
  db.add(TeacherSubject(
77
  teacher_id=teacher_id, subject_id=sid,
78
  account_id=current_user.account_id
 
27
  elif current_user.role == "teacher":
28
  query = query.filter(TeacherSubject.teacher_id == current_user.id)
29
  items = query.all()
30
+ # Batch-load subjects referenced by all items
31
+ subject_ids = {ts.subject_id for ts in items if ts.subject_id is not None}
32
+ subjects_by_id = {s.id: s for s in db.query(Subject).filter(Subject.id.in_(subject_ids)).all()} if subject_ids else {}
33
  result = []
34
  for ts in items:
35
+ sub = subjects_by_id.get(ts.subject_id)
36
  result.append(TeacherSubjectResponse(
37
  teacher_id=ts.teacher_id,
38
  subject_id=ts.subject_id,
 
54
  ).first()
55
  if not teacher:
56
  raise HTTPException(status_code=404, detail="Teacher not found")
57
+
58
+ # Batch-validate: each subject can only be assigned to one teacher
59
+ # Fetch all conflicting TeacherSubject rows in one query
60
+ conflicting_rows = db.query(TeacherSubject).filter(
61
+ TeacherSubject.subject_id.in_(subject_ids),
62
+ TeacherSubject.account_id == current_user.account_id,
63
+ TeacherSubject.teacher_id != teacher_id,
64
+ ).all() if subject_ids else []
65
+
66
+ if conflicting_rows:
67
+ # Batch-load conflicting teachers and subjects
68
+ conflict_teacher_ids = {r.teacher_id for r in conflicting_rows if r.teacher_id is not None}
69
+ conflict_subject_ids = {r.subject_id for r in conflicting_rows if r.subject_id is not None}
70
+ conflict_users_by_id = {u.id: u for u in db.query(User).filter(User.id.in_(conflict_teacher_ids)).all()} if conflict_teacher_ids else {}
71
+ conflict_subjects_by_id = {s.id: s for s in db.query(Subject).filter(Subject.id.in_(conflict_subject_ids)).all()} if conflict_subject_ids else {}
72
+ # Raise on the first conflict (preserves original behavior)
73
+ first = conflicting_rows[0]
74
+ other = conflict_users_by_id.get(first.teacher_id)
75
+ sub = conflict_subjects_by_id.get(first.subject_id)
76
+ other_name = f"{other.lastname}, {other.name}" if other else f"#{first.teacher_id}"
77
+ sub_name = sub.name if sub else f"#{first.subject_id}"
78
+ raise HTTPException(
79
+ status_code=status.HTTP_409_CONFLICT,
80
+ detail=f"La materia '{sub_name}' ya está asignada al docente {other_name}"
81
+ )
82
+
83
+ # Batch-validate subjects belong to the account (single query)
84
+ valid_subjects_by_id = {s.id: s for s in db.query(Subject).filter(
85
+ Subject.id.in_(subject_ids),
86
+ Subject.account_id == current_user.account_id,
87
+ ).all()} if subject_ids else {}
88
+
89
  db.query(TeacherSubject).filter(TeacherSubject.teacher_id == teacher_id).delete()
90
  for sid in subject_ids:
91
+ if sid in valid_subjects_by_id:
 
 
 
92
  db.add(TeacherSubject(
93
  teacher_id=teacher_id, subject_id=sid,
94
  account_id=current_user.account_id