| 1 | use crate::constant::{MAX_COURSE_DESCRIPTION_LEN, MAX_COURSE_TITLE_LEN}; |
| 2 | use crate::domain::monotonic_id::next_uuid; |
| 3 | use crate::domain::user::UserId; |
| 4 | use crate::error::ValidationError; |
| 5 | use crate::validate::{validate_course_kind, validate_optional, validate_required}; |
| 6 | |
| 7 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 8 | #[sqlx(transparent)] |
| 9 | pub struct CourseId(uuid::Uuid); |
| 10 | |
| 11 | impl CourseId { |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | pub fn generate() -> Self { |
| 17 | Self(next_uuid()) |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub fn uuid(&self) -> uuid::Uuid { |
| 23 | self.0 |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 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)] |
| 38 | pub struct CourseTitle(String); |
| 39 | |
| 40 | impl 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)] |
| 53 | pub struct CourseDescription(String); |
| 54 | |
| 55 | impl 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 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 71 | #[sqlx(transparent)] |
| 72 | pub struct CourseKind(String); |
| 73 | |
| 74 | impl 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 | |
| 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 | |
| 90 | |
| 91 | |
| 92 | pub fn is_class_delivered(&self) -> bool { |
| 93 | self.0 == "course" |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 110 | pub 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 | |
| 120 | impl 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 | |
| 142 | pub fn get_class_course_count(&self) -> i64 { |
| 143 | self.class_course_count |
| 144 | } |
| 145 | |
| 146 | |
| 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)] |
| 157 | mod 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 | } |