forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

class_course.rs228 lines, 8.6 KiBHistoryRawDownload
1//! A course as taught in one class: the academic *instance*.
2//!
3//! The anchor of the K12 model. A course in the school catalog is a template;
4//! attaching it to a class section mints one of these rows, and exams, sessions,
5//! roll-call, homework and enrollments all key on the instance — so two
6//! sections teaching the same course have separate rosters, exams, timetables
7//! and teachers.
8//!
9//! The row teaches **from its offering** — the
10//! [`crate::domain::course_offering::CourseOffering`] template for the
11//! course × the class's grade level, resolved (and auto-created empty) by the
12//! attach pump. Every content field on the instance is an *override*: `title`,
13//! `description`, `ders_saati` and `counts_toward_karne` are nullable, and a
14//! `NULL` means inherit — from the offering, and through it from the catalog
15//! row / the constants (1 weekly hour, counted toward the karne). The three
16//! `*_inherited` flags carry the same rule for the set-valued policies
17//! (subjects, exam weights, weekly plan), which cannot be `NULL`: `TRUE` =
18//! follow the offering's set, `FALSE` = this section's own table is
19//! authoritative, including when it is empty. Override-or-inherit, never
20//! merge.
21//!
22//! The attach/detach workflows live in [`crate::service::class_course`] (which
23//! also owns [`crate::service::class_course::resolve_policy`], the effective
24//! `ders_saati`/`counts_toward_karne` a read should use); the row's reads in
25//! [`crate::db::class_course`] — this file is the row shape and its id.
26
27use crate::constant::{MAX_DERS_SAATI, MIN_DERS_SAATI};
28use crate::domain::class_blueprint::ClassBlueprintId;
29use crate::domain::class_group::ClassGroupId;
30use crate::domain::course::{CourseDescription, CourseId, CourseTitle};
31use crate::domain::course_offering::CourseOfferingId;
32use crate::domain::monotonic_id::next_uuid;
33use crate::domain::timestamp::Timestamp;
34use crate::domain::user::UserId;
35use crate::error::ValidationError;
36
37/// The identity of one class×course instance. A UUIDv7 minted by the
38/// process-wide monotonic generator, so a class's instances list in creation
39/// order.
40#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
41#[sqlx(transparent)]
42pub struct ClassCourseId(uuid::Uuid);
43
44impl ClassCourseId {
45 pub fn generate() -> Self {
46 Self(next_uuid())
47 }
48
49 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
50 /// cannot take the newtype. Static `query!` binds take `self` directly.
51 pub fn uuid(&self) -> uuid::Uuid {
52 self.0
53 }
54
55 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
56 /// which matches no row.
57 pub fn from_key(key: &str) -> Self {
58 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
59 }
60
61 /// The bare uuid wire form.
62 pub fn key(&self) -> String {
63 self.0.to_string()
64 }
65}
66
67/// Weekly lesson hours of an instance: the weight the instance carries in the
68/// year's report-card average, and the count a timetable grid would lay out.
69/// As a *column* it is the instance's own override — `NULL` inherits from the
70/// offering's `default_ders_saati`, and the offering's `NULL` means the
71/// constant floor ([`crate::constant::MIN_DERS_SAATI`]).
72#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)]
73#[sqlx(transparent)]
74pub struct DersSaati(i64);
75
76impl DersSaati {
77 pub fn try_new(value: i64) -> Result<Self, ValidationError> {
78 if !(MIN_DERS_SAATI..=MAX_DERS_SAATI).contains(&value) {
79 return Err(ValidationError::Invalid {
80 field: "ders_saati",
81 reason: "must be between 1 and 40 weekly hours",
82 });
83 }
84 Ok(Self(value))
85 }
86
87 pub fn as_i64(self) -> i64 {
88 self.0
89 }
90}
91
92/// A named instance override the reset door can clear back to inherit. The
93/// scalars go `NULL` (the offering's default — then the catalog row or the
94/// constant — applies again); the three set flags flip back to `TRUE` (the
95/// offering's set is authoritative again). The wire spellings live with the
96/// reset route's parser in [`crate::service::class_course`]; this enum is the
97/// closed set both sides answer to.
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub enum OverrideField {
100 /// The section's own title.
101 Title,
102 /// The section's own description.
103 Description,
104 /// The section's own weekly hours.
105 DersSaati,
106 /// The section's own report-card policy.
107 CountsTowardKarne,
108 /// The section's own subject set (the flag; set-row deletion rides the
109 /// subject lane's reset).
110 Subjects,
111 /// The section's own exam-kind weights (the flag).
112 ExamWeights,
113 /// The section's own weekly plan (the flag).
114 WeeklyPlan,
115}
116
117/// One course taught in one class. `attached_by` is who attached it, `source`
118/// the blueprint that placed it (absent for a hand attach). The content
119/// fields are the instance's own *overrides* — `None` inherits from the
120/// offering ([`Self::get_offering`]), which is what two sections teaching the
121/// same catalog course at the same grade usually want to differ from only
122/// deliberately.
123#[derive(Debug, Clone, sqlx::FromRow)]
124pub struct ClassCourse {
125 pub(crate) id: ClassCourseId,
126 pub(crate) class: ClassGroupId,
127 pub(crate) course: CourseId,
128 /// The grade-level template this instance teaches from — always set: the
129 /// attach pump resolves it (auto-creating the empty template when the
130 /// grade is untouched) in the same transaction as the insert.
131 pub(crate) offering: CourseOfferingId,
132 pub(crate) attached_by: UserId,
133 /// The grade blueprint that placed this attachment, absent when a human
134 /// attached the course to this class directly — read the same way as
135 /// [`crate::domain::enrollment`]'s `source`: only a row carrying the key
136 /// is a blueprint's to take back, so a hand-attached course survives every
137 /// blueprint sweep.
138 pub(crate) source: Option<ClassBlueprintId>,
139 pub(crate) title: Option<CourseTitle>,
140 pub(crate) description: Option<CourseDescription>,
141 pub(crate) ders_saati: Option<DersSaati>,
142 pub(crate) counts_toward_karne: Option<bool>,
143 pub(crate) subjects_inherited: bool,
144 pub(crate) exam_weights_inherited: bool,
145 pub(crate) weekly_plan_inherited: bool,
146 pub(crate) enrollment_count: i64,
147 pub(crate) attached_at: Timestamp,
148}
149
150impl ClassCourse {
151 pub fn get_id(&self) -> &ClassCourseId {
152 &self.id
153 }
154
155 pub fn get_class(&self) -> &ClassGroupId {
156 &self.class
157 }
158
159 pub fn get_course(&self) -> &CourseId {
160 &self.course
161 }
162
163 /// The grade-level template this instance teaches from. The effective
164 /// values the instance inherits ride
165 /// [`crate::service::class_course::resolve_policy`].
166 pub fn get_offering(&self) -> &CourseOfferingId {
167 &self.offering
168 }
169
170 pub fn get_attached_by(&self) -> &UserId {
171 &self.attached_by
172 }
173
174 /// The blueprint that placed this attachment, or `None` for a hand attach.
175 pub fn get_source(&self) -> Option<&ClassBlueprintId> {
176 self.source.as_ref()
177 }
178
179 /// The instance's own title override, `None` = inherit.
180 pub fn get_title(&self) -> Option<&CourseTitle> {
181 self.title.as_ref()
182 }
183
184 /// The instance's own description override, `None` = inherit.
185 pub fn get_description(&self) -> Option<&CourseDescription> {
186 self.description.as_ref()
187 }
188
189 /// The instance's own weekly-hours override, `None` = inherit (the
190 /// offering's default, else 1). The *effective* value — what a read
191 /// should present — is [`crate::service::class_course::resolve_policy`]'s
192 /// answer, not this one.
193 pub fn get_ders_saati(&self) -> Option<DersSaati> {
194 self.ders_saati
195 }
196
197 /// The instance's own report-card policy override, `None` = inherit (the
198 /// offering's default, else counted).
199 pub fn counts_toward_karne(&self) -> Option<bool> {
200 self.counts_toward_karne
201 }
202
203 /// Whether this section follows the offering's subject set (`TRUE`) or
204 /// keeps its own `class_course_subject` table authoritative (`FALSE`,
205 /// including when that table is empty — that is how a section clears its
206 /// syllabus).
207 pub fn subjects_inherited(&self) -> bool {
208 self.subjects_inherited
209 }
210
211 /// The same override switch for the section's exam-kind weights.
212 pub fn exam_weights_inherited(&self) -> bool {
213 self.exam_weights_inherited
214 }
215
216 /// The same override switch for the section's weekly plan.
217 pub fn weekly_plan_inherited(&self) -> bool {
218 self.weekly_plan_inherited
219 }
220
221 pub fn get_enrollment_count(&self) -> i64 {
222 self.enrollment_count
223 }
224
225 pub fn get_attached_at(&self) -> Timestamp {
226 self.attached_at
227 }
228}