forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiBaf331ecAhmet Kaan Gümüş2 months ago451 commits

solution.rs139 lines, 3.9 KiBHistoryRawDownload
1use uuid::Uuid;
2
3use crate::constant::MAX_SOLUTION_BODY_LEN;
4use crate::domain::monotonic_id::next_uuid;
5use crate::domain::note_file::FileContentType;
6use crate::domain::pool_question::PoolQuestionId;
7use crate::domain::timestamp::Timestamp;
8use crate::domain::user::UserId;
9use crate::error::ValidationError;
10use crate::validate::validate_required;
11
12/// Typed solution row id. A UUIDv7 minted by the process-wide monotonic
13/// generator, so `id` order is mint order.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)]
15#[sqlx(transparent)]
16pub struct SolutionId(Uuid);
17
18impl SolutionId {
19 /// Minted from the process-wide monotonic generator, not a random v4:
20 /// solutions sort `offered_at ASC, id ASC` and the id *is* the tie-break
21 /// ([`crate::db::solution::list_for`]),
22 /// and a random low half scrambles rows minted in the same millisecond.
23 pub fn generate() -> Self {
24 Self(next_uuid())
25 }
26
27 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
28 /// cannot take the newtype. Static `query!` binds take `self` directly.
29 pub fn uuid(&self) -> Uuid {
30 self.0
31 }
32
33 /// Parse a wire key. A key that parses as no UUID — a malformed path
34 /// segment — reads as the nil id, which matches no row: exactly the 404 a
35 /// dangling record key produced under the old store, without turning a
36 /// typo into a panic.
37 pub fn from_key(key: &str) -> Self {
38 Self(Uuid::parse_str(key).unwrap_or(Uuid::nil()))
39 }
40
41 /// The hyphenated wire form.
42 pub fn key(&self) -> String {
43 self.0.to_string()
44 }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
48#[sqlx(transparent)]
49pub struct SolutionBody(String);
50
51impl SolutionBody {
52 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
53 validate_required("body", value, MAX_SOLUTION_BODY_LEN)?;
54 Ok(Self(value.to_string()))
55 }
56
57 pub fn as_str(&self) -> &str {
58 &self.0
59 }
60}
61
62#[derive(Debug, Clone, sqlx::FromRow)]
63pub struct Solution {
64 pub(crate) id: SolutionId,
65 pub(crate) question: PoolQuestionId,
66 pub(crate) author: UserId,
67 pub(crate) body: SolutionBody,
68 pub(crate) offered_at: Timestamp,
69 /// The photo's on-disk blob name — a fresh server-generated id every
70 /// upload; `None` when the solution carries no image.
71 pub(crate) image_file: Option<String>,
72 pub(crate) image_content_type: Option<FileContentType>,
73 pub(crate) image_size: Option<i64>,
74}
75
76impl Solution {
77 pub fn new(question: &PoolQuestionId, author: &UserId, body: SolutionBody) -> Self {
78 Self {
79 id: SolutionId::generate(),
80 question: question.clone(),
81 author: author.clone(),
82 body,
83 offered_at: Timestamp::now(),
84 image_file: None,
85 image_content_type: None,
86 image_size: None,
87 }
88 }
89
90 pub fn get_id(&self) -> &SolutionId {
91 &self.id
92 }
93
94 pub fn get_question(&self) -> &PoolQuestionId {
95 &self.question
96 }
97
98 pub fn get_author(&self) -> &UserId {
99 &self.author
100 }
101
102 pub fn get_body(&self) -> &SolutionBody {
103 &self.body
104 }
105
106 pub fn get_offered_at(&self) -> Timestamp {
107 self.offered_at
108 }
109
110 pub fn get_image_file(&self) -> Option<&str> {
111 self.image_file.as_deref()
112 }
113
114 pub fn get_image_content_type(&self) -> Option<&FileContentType> {
115 self.image_content_type.as_ref()
116 }
117
118 pub fn get_image_size(&self) -> Option<i64> {
119 self.image_size
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[test]
128 fn body_rules() {
129 assert!(SolutionBody::try_new("").is_err());
130 assert!(SolutionBody::try_new(" ").is_err());
131 assert!(SolutionBody::try_new(&"x".repeat(10_001)).is_err());
132 assert_eq!(
133 SolutionBody::try_new("Kısmi integrasyon.")
134 .unwrap()
135 .as_str(),
136 "Kısmi integrasyon."
137 );
138 }
139}