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_member.rs97 lines, 3.1 KiBHistoryRawDownload
1//! A student's stint in a class, and the enrollments that place implies.
2//!
3//! A history table: `left_at` NULL is the live stint, and the partial unique
4//! index `(class, app_user) WHERE left_at IS NULL` keeps one live stint per
5//! pair while preserving rejoin history (a student who leaves and comes back
6//! has two rows). Both writes are one call into [`crate::db::class_pump`]:
7//! adding a member and attaching a course to the class are the same pump seen
8//! from its two ends. The refusals-to-errors policy and the detach live in
9//! [`crate::service::class_member`]; the roster reads in
10//! [`crate::db::class_member`] — this file is the row shape and its id.
11
12use crate::domain::class_group::ClassGroupId;
13use crate::domain::monotonic_id::next_uuid;
14use crate::domain::timestamp::Timestamp;
15use crate::domain::user::UserId;
16
17/// The identity of one class-member stunt. A UUIDv7 minted by the
18/// process-wide monotonic generator, so a class's roster lists in join order.
19#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
20#[sqlx(transparent)]
21pub struct ClassMemberId(uuid::Uuid);
22
23impl ClassMemberId {
24 pub fn generate() -> Self {
25 Self(next_uuid())
26 }
27
28 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
29 /// cannot take the newtype. Static `query!` binds take `self` directly.
30 pub fn uuid(&self) -> uuid::Uuid {
31 self.0
32 }
33
34 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
35 /// which matches no row.
36 pub fn from_key(key: &str) -> Self {
37 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
38 }
39
40 /// The bare uuid wire form.
41 pub fn key(&self) -> String {
42 self.0.to_string()
43 }
44}
45
46/// One student's stint in one class. `added_by` is who put them there;
47/// `source_class_group` names the section a rollover copied them from (absent
48/// for a hand-added member).
49#[derive(Debug, Clone, sqlx::FromRow)]
50pub struct ClassMember {
51 pub(crate) id: ClassMemberId,
52 pub(crate) class: ClassGroupId,
53 /// The column is `app_user` (the reserved-word rename); the Rust name is
54 /// the domain's.
55 #[sqlx(rename = "app_user")]
56 pub(crate) user: UserId,
57 pub(crate) added_by: UserId,
58 pub(crate) joined_at: Timestamp,
59 /// When the student left; `None` while the stint is live.
60 pub(crate) left_at: Option<Timestamp>,
61 pub(crate) source_class_group: Option<ClassGroupId>,
62}
63
64impl ClassMember {
65 pub fn get_id(&self) -> &ClassMemberId {
66 &self.id
67 }
68
69 pub fn get_class(&self) -> &ClassGroupId {
70 &self.class
71 }
72
73 pub fn get_user(&self) -> &UserId {
74 &self.user
75 }
76
77 pub fn get_added_by(&self) -> &UserId {
78 &self.added_by
79 }
80
81 pub fn get_joined_at(&self) -> Timestamp {
82 self.joined_at
83 }
84
85 pub fn get_left_at(&self) -> Option<Timestamp> {
86 self.left_at
87 }
88
89 /// Whether this stint is live (the student is currently in the class).
90 pub fn is_live(&self) -> bool {
91 self.left_at.is_none()
92 }
93
94 pub fn get_source_class_group(&self) -> Option<&ClassGroupId> {
95 self.source_class_group.as_ref()
96 }
97}