| 1 | use crate::constant::{MAX_NOTE_CONTENT_LEN, MAX_NOTE_TITLE_LEN}; |
| 2 | use crate::domain::course::CourseId; |
| 3 | use crate::domain::monotonic_id::next_uuid; |
| 4 | use crate::domain::user::UserId; |
| 5 | use crate::error::ValidationError; |
| 6 | use crate::validate::{validate_optional, validate_required}; |
| 7 | |
| 8 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 9 | #[sqlx(transparent)] |
| 10 | pub struct CourseNoteId(uuid::Uuid); |
| 11 | |
| 12 | impl CourseNoteId { |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | pub fn generate() -> Self { |
| 18 | Self(next_uuid()) |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub fn uuid(&self) -> uuid::Uuid { |
| 24 | self.0 |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub fn from_key(key: &str) -> Self { |
| 30 | Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil())) |
| 31 | } |
| 32 | |
| 33 | pub fn key(&self) -> String { |
| 34 | self.0.to_string() |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 39 | #[sqlx(transparent)] |
| 40 | pub struct CourseNoteTitle(String); |
| 41 | |
| 42 | impl CourseNoteTitle { |
| 43 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 44 | validate_required("title", value, MAX_NOTE_TITLE_LEN)?; |
| 45 | Ok(Self(value.to_string())) |
| 46 | } |
| 47 | |
| 48 | pub fn as_str(&self) -> &str { |
| 49 | &self.0 |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 54 | #[sqlx(transparent)] |
| 55 | pub struct CourseNoteContent(String); |
| 56 | |
| 57 | impl CourseNoteContent { |
| 58 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 59 | validate_optional("content", value, MAX_NOTE_CONTENT_LEN)?; |
| 60 | Ok(Self(value.to_string())) |
| 61 | } |
| 62 | |
| 63 | pub fn as_str(&self) -> &str { |
| 64 | &self.0 |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 69 | pub struct CourseNote { |
| 70 | pub(crate) id: CourseNoteId, |
| 71 | pub(crate) course: CourseId, |
| 72 | pub(crate) author: UserId, |
| 73 | pub(crate) title: CourseNoteTitle, |
| 74 | pub(crate) content: CourseNoteContent, |
| 75 | } |
| 76 | |
| 77 | impl CourseNote { |
| 78 | pub fn get_id(&self) -> &CourseNoteId { |
| 79 | &self.id |
| 80 | } |
| 81 | |
| 82 | pub fn get_course(&self) -> &CourseId { |
| 83 | &self.course |
| 84 | } |
| 85 | |
| 86 | pub fn get_author(&self) -> &UserId { |
| 87 | &self.author |
| 88 | } |
| 89 | |
| 90 | pub fn get_title(&self) -> &CourseNoteTitle { |
| 91 | &self.title |
| 92 | } |
| 93 | |
| 94 | pub fn get_content(&self) -> &CourseNoteContent { |
| 95 | &self.content |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[cfg(test)] |
| 100 | mod tests { |
| 101 | use super::*; |
| 102 | |
| 103 | #[tokio::test] |
| 104 | async fn title_is_required() { |
| 105 | assert_eq!(CourseNoteTitle::try_new("hi").unwrap().as_str(), "hi"); |
| 106 | assert!(CourseNoteTitle::try_new(" ").is_err()); |
| 107 | } |
| 108 | |
| 109 | #[tokio::test] |
| 110 | async fn content_is_optional() { |
| 111 | assert_eq!(CourseNoteContent::try_new("").unwrap().as_str(), ""); |
| 112 | assert_eq!(CourseNoteContent::try_new("body").unwrap().as_str(), "body"); |
| 113 | } |
| 114 | } |