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_session.rs110 lines, 3.3 KiBHistoryRawDownload
1use crate::constant::MAX_SESSION_TOPIC_LEN;
2use crate::domain::class_course::ClassCourseId;
3use crate::domain::monotonic_id::next_uuid;
4use crate::domain::timestamp::Timestamp;
5use crate::domain::user::UserId;
6use crate::error::ValidationError;
7use crate::validate::validate_optional;
8
9#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
10#[sqlx(transparent)]
11pub struct CourseSessionId(uuid::Uuid);
12
13impl CourseSessionId {
14 /// Minted from the process-wide monotonic generator, not a plain random
15 /// UUID: a course's sessions sort `starts_at DESC, id DESC` and the id
16 /// breaks the tie between two sessions starting at the same instant,
17 /// and a random low half sorts arbitrarily inside one millisecond.
18 pub fn generate() -> Self {
19 Self(next_uuid())
20 }
21
22 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
23 /// cannot take the newtype. Static `query!` binds take `self` directly.
24 pub fn uuid(&self) -> uuid::Uuid {
25 self.0
26 }
27
28 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
29 /// which matches no row.
30 pub fn from_key(key: &str) -> Self {
31 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
32 }
33
34 pub fn key(&self) -> String {
35 self.0.to_string()
36 }
37}
38#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
39#[sqlx(transparent)]
40pub struct SessionTopic(String);
41
42impl SessionTopic {
43 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
44 validate_optional("topic", value, MAX_SESSION_TOPIC_LEN)?;
45 Ok(Self(value.to_string()))
46 }
47
48 pub fn as_str(&self) -> &str {
49 &self.0
50 }
51}
52
53/// One scheduled lesson of a course: the unit roll call is taken on. Unlike a
54/// generic event, a session always has a start instant and belongs to a course,
55/// so its roster is the course's enrollment plus the assigned teacher.
56#[derive(Debug, Clone, sqlx::FromRow)]
57pub struct CourseSession {
58 pub(crate) id: CourseSessionId,
59 pub(crate) class_course: ClassCourseId,
60 /// Who teaches this session. Must hold the `teacher` role or higher —
61 /// enforced by [`crate::service::course_session::resolve_session_teacher`],
62 /// where the target user row is at hand.
63 pub(crate) teacher: UserId,
64 pub(crate) topic: SessionTopic,
65 pub(crate) starts_at: Timestamp,
66 pub(crate) ends_at: Option<Timestamp>,
67}
68
69impl CourseSession {
70 pub fn get_id(&self) -> &CourseSessionId {
71 &self.id
72 }
73
74 pub fn get_class_course(&self) -> &ClassCourseId {
75 &self.class_course
76 }
77
78 pub fn get_teacher(&self) -> &UserId {
79 &self.teacher
80 }
81
82 pub fn get_topic(&self) -> &SessionTopic {
83 &self.topic
84 }
85
86 pub fn get_starts_at(&self) -> Timestamp {
87 self.starts_at
88 }
89
90 pub fn get_ends_at(&self) -> Option<Timestamp> {
91 self.ends_at
92 }
93
94 pub fn is_teacher(&self, user: &UserId) -> bool {
95 &self.teacher == user
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[tokio::test]
104 async fn topic_is_optional_but_bounded() {
105 assert!(SessionTopic::try_new("").is_ok());
106 assert!(SessionTopic::try_new("limits and continuity").is_ok());
107 assert!(SessionTopic::try_new(&"x".repeat(MAX_SESSION_TOPIC_LEN)).is_ok());
108 assert!(SessionTopic::try_new(&"x".repeat(MAX_SESSION_TOPIC_LEN + 1)).is_err());
109 }
110}