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

question_image.rs131 lines, 4.4 KiBHistoryRawDownload
1//! An image pinned to an exam question — the question's own illustration
2//! (`slot = NULL`, any kind: a map above the prompt) or one choice's picture
3//! (`slot` = that choice's stable id, choice questions only). The row carries
4//! metadata; the bytes live on disk under
5//! [`crate::config::Config::files_path`] in a file named by `file` — a fresh
6//! server-generated id per upload, so no user input ever shapes a disk path
7//! and a replace never overwrites bytes in place. The row's identity is the
8//! natural pair of its question and slot — "one image per slot" holds by
9//! construction and a replace is a plain upsert. The queries and
10//! transactions over these rows live in [`crate::db::question_image`]; the
11//! web layer owns the blob I/O and its ordering (new blob before row, row
12//! before old blob).
13
14use crate::domain::exam::ExamId;
15use crate::domain::exam_question::{ChoiceId, ExamQuestionId};
16use crate::domain::monotonic_id::next_uuid;
17use crate::domain::note_file::FileContentType;
18
19/// The (question, slot) pair — the table's natural unique identity. The same
20/// trick as the exam id types' composite keys: the slot sentinel `"q"` keeps
21/// the illustration apart from every choice's picture, and a choice's stable
22/// id can never be `"q"`, so the two shapes can never collide.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct QuestionImageId {
25 question: ExamQuestionId,
26 slot: Option<ChoiceId>,
27}
28
29impl QuestionImageId {
30 /// The one identity a (question, slot) pair can have — see
31 /// [`crate::domain::key::slot`] for the key shape and why the two forms
32 /// can never collide.
33 pub fn for_slot(question: &ExamQuestionId, slot: Option<&ChoiceId>) -> Self {
34 Self {
35 question: question.clone(),
36 slot: slot.cloned(),
37 }
38 }
39
40 /// The wire form, `None` spelled with the `"q"` sentinel — the same
41 /// spelling the stored key uses.
42 pub fn key(&self) -> String {
43 crate::domain::key::slot(
44 &self.question.key(),
45 self.slot.as_ref().map(|id| id.as_str()),
46 )
47 }
48
49 pub fn question(&self) -> &ExamQuestionId {
50 &self.question
51 }
52
53 pub fn slot(&self) -> Option<&ChoiceId> {
54 self.slot.as_ref()
55 }
56}
57
58#[derive(Debug, Clone, sqlx::FromRow)]
59pub struct QuestionImage {
60 pub(crate) exam: ExamId,
61 pub(crate) question: ExamQuestionId,
62 /// `NULL` = the question's illustration; otherwise the id of the option
63 /// this picture belongs to. Together with `question` this is the row's
64 /// `UNIQUE NULLS NOT DISTINCT` identity.
65 pub(crate) slot: Option<ChoiceId>,
66 /// The blob's on-disk name — a fresh server-generated id every upload.
67 pub(crate) file: String,
68 pub(crate) content_type: FileContentType,
69 pub(crate) size: i64,
70}
71
72impl QuestionImage {
73 /// Assemble a row (fresh blob name generated here) without persisting it.
74 /// The caller writes the blob under [`Self::get_file`] first, then calls
75 /// [`crate::db::question_image::upsert`] — so a stored row always points
76 /// at a real blob.
77 pub fn new(
78 exam: &ExamId,
79 question: &ExamQuestionId,
80 slot: Option<&ChoiceId>,
81 content_type: FileContentType,
82 size: i64,
83 ) -> Self {
84 Self {
85 exam: exam.clone(),
86 question: question.clone(),
87 slot: slot.cloned(),
88 file: next_uuid().to_string(),
89 content_type,
90 size,
91 }
92 }
93
94 pub fn get_question(&self) -> &ExamQuestionId {
95 &self.question
96 }
97
98 pub fn get_slot(&self) -> Option<&ChoiceId> {
99 self.slot.as_ref()
100 }
101
102 pub fn get_file(&self) -> &str {
103 &self.file
104 }
105
106 pub fn get_content_type(&self) -> &FileContentType {
107 &self.content_type
108 }
109
110 pub fn get_size(&self) -> i64 {
111 self.size
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 /// The illustration (`None`) and a choice picture key apart — the `"q"`
120 /// sentinel can never be a choice's id.
121 #[test]
122 fn the_illustration_and_a_choice_never_collide() {
123 let question = ExamQuestionId::from_key("018f1a00-0000-7000-8000-000000000001");
124 let illustration = QuestionImageId::for_slot(&question, None);
125 assert_eq!(
126 illustration.key(),
127 format!("{}_q", question.key()),
128 "the sentinel spelling is the stored key's spelling"
129 );
130 }
131}