Skip to main content

Subject Module

Location: backend/src/subject/

The subject module manages the catalog of subjects offered by a school (e.g., Mathematics, Language Arts, Science). Subjects are referenced throughout the system - in teacher assignments, student subject profiles, assessments, and grade calculations.

Files

FilePurpose
subject.module.tsModule definition
subject.controller.tsAPI endpoints
subject.service.tsBusiness logic
dto/create-subject.dto.tsValidation for creation
dto/update-subject.dto.tsValidation for updates
dto/reorder-subjects.dto.tsValidation for batch reordering

Data Model

FieldTypeDescription
idUUIDPrimary key
namestringSubject name (e.g., "Mathematics")
codestring?Short code (e.g., "MATH")
school_idUUIDThe school this subject belongs to
is_gradedbooleanWhether grades are recorded for this subject (default: true)
sort_ordernumber?Display ordering

Graded vs Non-Graded Subjects

When is_graded is false, the subject won't appear in grade calculations. This is useful for subjects where only remarks are recorded (e.g., Physical Education, Art).

API Endpoints

All endpoints require AuthGuard. Subjects are automatically scoped to the user's school.

GET /api/subjects

Returns all subjects for the user's school, ordered by sort_order then name.


GET /api/subjects/:id

Returns a single subject by ID.


POST /api/subjects

Creates a new subject.

Body:

{
"name": "Mathematics",
"code": "MATH",
"isGraded": true,
"sortOrder": 1
}
FieldRequiredNotes
nameYes
codeNo
isGradedNoDefaults to true
sortOrderNo

Error Handling:

  • Duplicate name or code within the same school → 409 Conflict

PATCH /api/subjects/:id

Updates a subject. All fields optional.


PATCH /api/subjects/reorder

Batch-updates the sort_order for multiple subjects at once. Used by the drag-to-sort UI.

Body:

{
"items": [
{ "id": "uuid-1", "sortOrder": 0 },
{ "id": "uuid-2", "sortOrder": 1 },
{ "id": "uuid-3", "sortOrder": 2 }
]
}

Clears the subject cache after updating.


DELETE /api/subjects/:id

Deletes a subject. Fails with 409 Conflict if the subject has existing assessments or student assignments (foreign key constraint).