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

parent_link.rs77 lines, 2.4 KiBHistoryRawDownload
1use crate::domain::user::UserId;
2
3/// The (parent, student) pair — the table's natural composite primary key.
4/// The same pair always maps to the same row, so linking is a single atomic
5/// upsert with no find-then-insert race and one-row-per-pair by construction.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct ParentLinkId {
8 parent: UserId,
9 student: UserId,
10}
11
12impl ParentLinkId {
13 pub fn composite(parent: &UserId, student: &UserId) -> Self {
14 Self {
15 parent: *parent,
16 student: *student,
17 }
18 }
19
20 /// Parse the `{parent}_{student}` wire form. A key that parses as no pair
21 /// reads as the nil pair, which matches no row — exactly the 404 a
22 /// dangling composite key produced under the old store, without turning a
23 /// typo into a panic. UUID strings carry only `-`, so `_` is an
24 /// unambiguous joiner.
25 pub fn from_key(key: &str) -> Self {
26 let (parent, student) = key.rsplit_once('_').unwrap_or(("", ""));
27 Self {
28 parent: UserId::from_key(parent),
29 student: UserId::from_key(student),
30 }
31 }
32
33 /// The `{parent}_{student}` wire form.
34 pub fn key(&self) -> String {
35 format!("{}_{}", self.parent.key(), self.student.key())
36 }
37
38 pub fn parent(&self) -> UserId {
39 self.parent
40 }
41
42 pub fn student(&self) -> UserId {
43 self.student
44 }
45}
46
47/// A parent account's tie to one student it may observe. Almost entirely a
48/// read grant: it gates the parent's access to the student's reports (marks,
49/// attendance, pomodoro). The single write it authorizes is the food program —
50/// a parent books and cancels a linked child's meals, since paying for lunch
51/// is a parent's job and a small child cannot do it themselves.
52#[derive(Debug, Clone, sqlx::FromRow)]
53pub struct ParentLink {
54 pub(crate) parent: UserId,
55 pub(crate) student: UserId,
56 pub(crate) linked_by: UserId,
57}
58
59impl ParentLink {
60 pub fn get_parent(&self) -> &UserId {
61 &self.parent
62 }
63
64 pub fn get_student(&self) -> &UserId {
65 &self.student
66 }
67
68 pub fn get_linked_by(&self) -> &UserId {
69 &self.linked_by
70 }
71
72 // Both role-change sweeps (the observed student leaving `student`, the
73 // observing parent leaving `parent`) live in
74 // [`crate::service::user::set_role`]: they commit with the role write
75 // itself, so there is no window where a link outlives the role that
76 // justified it.
77}