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_note.rs114 lines, 3.0 KiBHistoryRawDownload
1use crate::constant::{MAX_NOTE_CONTENT_LEN, MAX_NOTE_TITLE_LEN};
2use crate::domain::course::CourseId;
3use crate::domain::monotonic_id::next_uuid;
4use crate::domain::user::UserId;
5use crate::error::ValidationError;
6use crate::validate::{validate_optional, validate_required};
7
8#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
9#[sqlx(transparent)]
10pub struct CourseNoteId(uuid::Uuid);
11
12impl CourseNoteId {
13 /// Minted from the process-wide monotonic generator, not a plain random
14 /// UUID: a course's notes list `id DESC` (newest first,
15 /// [`crate::db::course_note::list_for_course`]), and a random low half
16 /// scrambles rows minted in the same millisecond.
17 pub fn generate() -> Self {
18 Self(next_uuid())
19 }
20
21 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
22 /// cannot take the newtype. Static `query!` binds take `self` directly.
23 pub fn uuid(&self) -> uuid::Uuid {
24 self.0
25 }
26
27 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
28 /// which matches no row.
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)]
40pub struct CourseNoteTitle(String);
41
42impl 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)]
55pub struct CourseNoteContent(String);
56
57impl 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)]
69pub 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
77impl 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)]
100mod 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}