| 1 | use crate::constant::MAX_SESSION_TOPIC_LEN; |
| 2 | use crate::domain::class_course::ClassCourseId; |
| 3 | use crate::domain::monotonic_id::next_uuid; |
| 4 | use crate::domain::timestamp::Timestamp; |
| 5 | use crate::domain::user::UserId; |
| 6 | use crate::error::ValidationError; |
| 7 | use crate::validate::validate_optional; |
| 8 | |
| 9 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 10 | #[sqlx(transparent)] |
| 11 | pub struct CourseSessionId(uuid::Uuid); |
| 12 | |
| 13 | impl CourseSessionId { |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | pub fn generate() -> Self { |
| 19 | Self(next_uuid()) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | |
| 24 | pub fn uuid(&self) -> uuid::Uuid { |
| 25 | self.0 |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 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)] |
| 40 | pub struct SessionTopic(String); |
| 41 | |
| 42 | impl 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 | |
| 54 | |
| 55 | |
| 56 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 57 | pub struct CourseSession { |
| 58 | pub(crate) id: CourseSessionId, |
| 59 | pub(crate) class_course: ClassCourseId, |
| 60 | |
| 61 | |
| 62 | |
| 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 | |
| 69 | impl 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)] |
| 100 | mod 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 | } |