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

course.rs183 lines, 5.6 KiBHistoryRawDownload
1use crate::constant::{MAX_COURSE_DESCRIPTION_LEN, MAX_COURSE_TITLE_LEN};
2use crate::domain::monotonic_id::next_uuid;
3use crate::domain::user::UserId;
4use crate::error::ValidationError;
5use crate::validate::{validate_course_kind, validate_optional, validate_required};
6
7#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
8#[sqlx(transparent)]
9pub struct CourseId(uuid::Uuid);
10
11impl CourseId {
12 /// Minted from the process-wide monotonic generator, not a plain random
13 /// UUID: courses list `id DESC` (newest first,
14 /// [`crate::db::course::list_all`]),
15 /// and a random low half scrambles rows minted in the same millisecond.
16 pub fn generate() -> Self {
17 Self(next_uuid())
18 }
19
20 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
21 /// cannot take the newtype. Static `query!` binds take `self` directly.
22 pub fn uuid(&self) -> uuid::Uuid {
23 self.0
24 }
25
26 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
27 /// which matches no row.
28 pub fn from_key(key: &str) -> Self {
29 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
30 }
31
32 pub fn key(&self) -> String {
33 self.0.to_string()
34 }
35}
36#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
37#[sqlx(transparent)]
38pub struct CourseTitle(String);
39
40impl CourseTitle {
41 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
42 validate_required("title", value, MAX_COURSE_TITLE_LEN)?;
43 Ok(Self(value.to_string()))
44 }
45
46 pub fn as_str(&self) -> &str {
47 &self.0
48 }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
52#[sqlx(transparent)]
53pub struct CourseDescription(String);
54
55impl CourseDescription {
56 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
57 validate_optional("description", value, MAX_COURSE_DESCRIPTION_LEN)?;
58 Ok(Self(value.to_string()))
59 }
60
61 pub fn as_str(&self) -> &str {
62 &self.0
63 }
64}
65
66/// A validated course kind: `course` (a regular class), `study` (a supervised
67/// study session), or `club` (a student club).
68/// Only `course` is class-delivered (a section attaches it as an instance);
69/// `study`/`club` are school-scoped and joined individually.
70#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
71#[sqlx(transparent)]
72pub struct CourseKind(String);
73
74impl CourseKind {
75 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
76 validate_course_kind(value)?;
77 Ok(Self(value.to_string()))
78 }
79
80 /// The classic kind — what every course is unless said otherwise.
81 pub fn course() -> Self {
82 Self("course".to_string())
83 }
84
85 pub fn as_str(&self) -> &str {
86 &self.0
87 }
88
89 /// Whether a section attaches this kind as an instance. Only a regular
90 /// course (`course`) is class-delivered; clubs and supervised studies are
91 /// school-scoped and joined individually ([`crate::domain::course_membership`]).
92 pub fn is_class_delivered(&self) -> bool {
93 self.0 == "course"
94 }
95}
96
97/// A course: a school-level catalog entry. It is a *template* — the academic
98/// work (roster, exams, timetable) lives on the instances a class attaches
99/// ([`crate::domain::class_course::ClassCourse`]), so this row carries only
100/// what every instance shares: title, description, kind.
101///
102/// `creator` is who minted it; the catalog is Manager+-owned, so there is no
103/// per-teacher ownership. The two counters are the delete guard: a course may
104/// only be dropped when no class attaches it and no one holds an individual
105/// membership.
106///
107/// Fields are crate-visible: [`crate::db::course`] mints the rows on create
108/// and reads the id when updating and cascading a delete.
109#[derive(Debug, Clone, sqlx::FromRow)]
110pub struct Course {
111 pub(crate) id: CourseId,
112 pub(crate) creator: UserId,
113 pub(crate) title: CourseTitle,
114 pub(crate) description: CourseDescription,
115 pub(crate) kind: CourseKind,
116 pub(crate) class_course_count: i64,
117 pub(crate) course_membership_count: i64,
118}
119
120impl Course {
121 pub fn get_id(&self) -> &CourseId {
122 &self.id
123 }
124
125 pub fn get_creator(&self) -> &UserId {
126 &self.creator
127 }
128
129 pub fn get_title(&self) -> &CourseTitle {
130 &self.title
131 }
132
133 pub fn get_description(&self) -> &CourseDescription {
134 &self.description
135 }
136
137 pub fn get_kind(&self) -> &CourseKind {
138 &self.kind
139 }
140
141 /// How many class instances attach this catalog course.
142 pub fn get_class_course_count(&self) -> i64 {
143 self.class_course_count
144 }
145
146 /// How many users hold an individual (club/study) membership.
147 pub fn get_course_membership_count(&self) -> i64 {
148 self.course_membership_count
149 }
150
151 pub fn is_creator(&self, user: &UserId) -> bool {
152 &self.creator == user
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[tokio::test]
161 async fn title_is_required() {
162 assert!(CourseTitle::try_new("algebra").is_ok());
163 assert!(CourseTitle::try_new("").is_err());
164 assert!(CourseTitle::try_new(" ").is_err());
165 }
166
167 #[tokio::test]
168 async fn description_is_optional() {
169 assert!(CourseDescription::try_new("").is_ok());
170 }
171
172 #[tokio::test]
173 async fn kind_is_course_study_or_club() {
174 assert!(CourseKind::try_new("course").is_ok());
175 assert!(CourseKind::try_new("study").is_ok());
176 assert!(CourseKind::try_new("club").is_ok());
177 assert!(CourseKind::try_new("etut").is_err());
178 assert_eq!(CourseKind::course().as_str(), "course");
179 assert!(CourseKind::course().is_class_delivered());
180 assert!(!CourseKind::try_new("club").unwrap().is_class_delivered());
181 assert!(!CourseKind::try_new("study").unwrap().is_class_delivered());
182 }
183}