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

pool_question.rs147 lines, 4.6 KiBHistoryRawDownload
1//! A student-asked question on its way into the school-wide question pool.
2//! Born `pending` (visible to its asker and to teacher+, who run the approval
3//! queue); a teacher+ approval flips it to `approved`, which publishes it to
4//! the whole school for everyone to read and offer solutions on. Approved
5//! content is frozen — edits after approval would bypass moderation — so the
6//! only post-approval change is deletion (asker or teacher+), which takes the
7//! question's solutions with it. An optional photo of the problem is its own
8//! row in `pool_question_image` — the child-table shape of
9//! [`crate::domain::question_image`] with a single (implicit) slot — and the
10//! bytes live on disk under [`crate::config::Config::files_path`] in a file
11//! named by that row's `file` — a fresh server-generated id per upload. The
12//! web layer owns the blob I/O; the queries live in
13//! [`crate::db::pool_question`], the workflows in
14//! [`crate::service::pool_question`].
15
16use uuid::Uuid;
17
18use crate::constant::{
19 MAX_POOL_QUESTION_BODY_LEN, MAX_POOL_QUESTION_TITLE_LEN, STATUS_APPROVED, STATUS_PENDING,
20};
21use crate::domain::monotonic_id::next_uuid;
22use crate::domain::timestamp::Timestamp;
23use crate::domain::user::UserId;
24use crate::error::ValidationError;
25use crate::validate::validate_required;
26
27/// Typed pool-question row id. A UUIDv7 minted by the process-wide monotonic
28/// generator, so `id` order is mint order.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)]
30#[sqlx(transparent)]
31pub struct PoolQuestionId(Uuid);
32
33impl PoolQuestionId {
34 /// Minted from the process-wide monotonic generator, not a random v4:
35 /// the pool sorts `asked_at DESC, id DESC` and the id *is* the tie-break
36 /// ([`crate::db::pool_question::list`]),
37 /// and a random low half scrambles rows minted in the same millisecond.
38 pub fn generate() -> Self {
39 Self(next_uuid())
40 }
41
42 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
43 /// cannot take the newtype. Static `query!` binds take `self` directly.
44 pub fn uuid(&self) -> Uuid {
45 self.0
46 }
47
48 /// Parse a wire key. A key that parses as no UUID — a malformed path
49 /// segment — reads as the nil id, which matches no row: exactly the 404 a
50 /// dangling record key produced under the old store, without turning a
51 /// typo into a panic.
52 pub fn from_key(key: &str) -> Self {
53 Self(Uuid::parse_str(key).unwrap_or(Uuid::nil()))
54 }
55
56 /// The hyphenated wire form.
57 pub fn key(&self) -> String {
58 self.0.to_string()
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
63#[sqlx(transparent)]
64pub struct PoolQuestionTitle(String);
65
66impl PoolQuestionTitle {
67 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
68 validate_required("title", value, MAX_POOL_QUESTION_TITLE_LEN)?;
69 Ok(Self(value.to_string()))
70 }
71
72 pub fn as_str(&self) -> &str {
73 &self.0
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
78#[sqlx(transparent)]
79pub struct PoolQuestionBody(String);
80
81impl PoolQuestionBody {
82 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
83 validate_required("body", value, MAX_POOL_QUESTION_BODY_LEN)?;
84 Ok(Self(value.to_string()))
85 }
86
87 pub fn as_str(&self) -> &str {
88 &self.0
89 }
90}
91
92#[derive(Debug, Clone, sqlx::FromRow)]
93pub struct PoolQuestion {
94 pub(crate) id: PoolQuestionId,
95 pub(crate) asker: UserId,
96 pub(crate) title: PoolQuestionTitle,
97 pub(crate) body: PoolQuestionBody,
98 pub(crate) status: String,
99 pub(crate) asked_at: Timestamp,
100 pub(crate) approved_by: Option<UserId>,
101}
102
103impl PoolQuestion {
104 pub fn new(asker: &UserId, title: PoolQuestionTitle, body: PoolQuestionBody) -> Self {
105 Self {
106 id: PoolQuestionId::generate(),
107 asker: *asker,
108 title,
109 body,
110 status: STATUS_PENDING.to_string(),
111 asked_at: Timestamp::now(),
112 approved_by: None,
113 }
114 }
115
116 pub fn get_id(&self) -> &PoolQuestionId {
117 &self.id
118 }
119
120 pub fn get_asker(&self) -> &UserId {
121 &self.asker
122 }
123
124 pub fn get_title(&self) -> &PoolQuestionTitle {
125 &self.title
126 }
127
128 pub fn get_body(&self) -> &PoolQuestionBody {
129 &self.body
130 }
131
132 pub fn get_status(&self) -> &str {
133 &self.status
134 }
135
136 pub fn is_approved(&self) -> bool {
137 self.status == STATUS_APPROVED
138 }
139
140 pub fn get_asked_at(&self) -> Timestamp {
141 self.asked_at
142 }
143
144 pub fn get_approved_by(&self) -> Option<&UserId> {
145 self.approved_by.as_ref()
146 }
147}