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

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