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_group.rs137 lines, 4.4 KiBHistoryRawDownload
1//! A class section: a named set of students the school manages as one, so a
2//! course attach enrolls the whole set at once. The membership and the
3//! attachments live in their own tables (`class_member`, `class_course`) and are
4//! counted on this row — a class may only be deleted at zero on both, the same
5//! stored guard shape courses and terms use.
6//!
7//! A class belongs to an academic year ([`crate::domain::academic_year`]), not
8//! to a term: the section keeps the same roster all year and the term is a
9//! grading slice inside it. It claims a reference on its year before the link
10//! is written; the reference is its *own* column
11//! ([`crate::constant::ACADEMIC_YEAR_CLASS_COUNT_FIELD`]) rather than the
12//! courses' own count, so a class's share is never confused with another
13//! roster's.
14//!
15//! The queries live in [`crate::db::class_group`]; the archived-year guard and
16//! the route-facing wrappers in [`crate::service::class_group`] — this file is
17//! the row shape, its newtypes and getters.
18
19use crate::constant::MAX_CLASS_NAME_LEN;
20use crate::domain::academic_year::AcademicYearId;
21use crate::domain::grade::GradeLevel;
22use crate::domain::monotonic_id::next_uuid;
23use crate::domain::user::UserId;
24use crate::error::ValidationError;
25use crate::validate::validate_required;
26
27#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
28#[sqlx(transparent)]
29pub struct ClassGroupId(uuid::Uuid);
30
31impl ClassGroupId {
32 pub fn generate() -> Self {
33 Self(next_uuid())
34 }
35
36 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
37 /// cannot take the newtype. Static `query!` binds take `self` directly.
38 pub fn uuid(&self) -> uuid::Uuid {
39 self.0
40 }
41
42 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
43 /// which matches no row.
44 pub fn from_key(key: &str) -> Self {
45 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
46 }
47
48 pub fn key(&self) -> String {
49 self.0.to_string()
50 }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
54#[sqlx(transparent)]
55pub struct ClassName(String);
56
57impl ClassName {
58 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
59 validate_required("name", value, MAX_CLASS_NAME_LEN)?;
60 Ok(Self(value.to_string()))
61 }
62
63 pub fn as_str(&self) -> &str {
64 &self.0
65 }
66}
67
68/// One class. `creator` is who made it; the two refcounts behind the delete
69/// guard are database-side columns only, so no whole-row save can clobber one
70/// (see [`crate::db::cap`]).
71///
72/// `teacher` is the section's homeroom teacher: optional, not
73/// refcounted, and merely a label pointing at a teacher-or-higher account — the
74/// web layer holds that bar, and a demotion sweeps the column
75/// ([`crate::db::class_group::unassign_everywhere`]).
76///
77/// `grade_level` is the section's rung on the shared grade ladder
78/// ([`GradeLevel`]) — required, not a label the school makes up: a club-shaped
79/// *class* still sits at a grade, and it is the school-wide club/study
80/// *courses* that belong to no section at all.
81#[derive(Debug, Clone, sqlx::FromRow)]
82pub struct ClassGroup {
83 pub(crate) id: ClassGroupId,
84 pub(crate) creator: UserId,
85 pub(crate) name: ClassName,
86 pub(crate) grade_level: GradeLevel,
87 pub(crate) year: Option<AcademicYearId>,
88 pub(crate) teacher: Option<UserId>,
89}
90
91impl ClassGroup {
92 pub fn get_id(&self) -> &ClassGroupId {
93 &self.id
94 }
95
96 pub fn get_creator(&self) -> &UserId {
97 &self.creator
98 }
99
100 pub fn get_name(&self) -> &ClassName {
101 &self.name
102 }
103
104 pub fn get_grade_level(&self) -> GradeLevel {
105 self.grade_level
106 }
107
108 pub fn get_year(&self) -> Option<&AcademicYearId> {
109 self.year.as_ref()
110 }
111
112 /// The homeroom teacher, if one is assigned.
113 pub fn get_teacher(&self) -> Option<&UserId> {
114 self.teacher.as_ref()
115 }
116
117 pub fn is_creator(&self, user: &UserId) -> bool {
118 &self.creator == user
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn name_is_required_and_the_grade_is_a_ladder_rung() {
128 assert!(ClassName::try_new("9-A").is_ok());
129 assert!(ClassName::try_new("").is_err());
130 assert!(ClassName::try_new(" ").is_err());
131 assert!(ClassName::try_new(&"x".repeat(MAX_CLASS_NAME_LEN + 1)).is_err());
132 assert!(GradeLevel::new(0).is_ok());
133 assert!(GradeLevel::new(12).is_ok());
134 assert!(GradeLevel::new(13).is_err());
135 assert!(GradeLevel::new(-1).is_err());
136 }
137}