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

answer_image.rs119 lines, 4.2 KiBHistoryRawDownload
1//! A student's drawn answer to an exam question — their own illustration for
2//! one (exam, user, question), mirroring [`crate::domain::question_image`] but
3//! keyed by the answering student instead of a choice slot. The row carries
4//! metadata; the bytes live on disk under [`crate::config::Config::files_path`]
5//! in a file named by `file` — a fresh server-generated UUID per upload, so no
6//! user input ever shapes a disk path and a replace never overwrites bytes in
7//! place. The row is keyed by (question, user, seq) — the same shape as
8//! [`crate::domain::exam_attempt::ExamAttemptId`] — so "one
9//! drawing per student per question per sitting" holds by construction and a
10//! replace within a sitting is a plain UPSERT on the composite primary key,
11//! while a retake (`seq + 1`) accumulates its own rows instead of
12//! overwriting. The queries and transactions over these rows live in
13//! [`crate::db::answer_image`]; the web layer owns the blob I/O and its
14//! ordering (new blob before row, row before old blob).
15
16use crate::domain::exam::ExamId;
17use crate::domain::exam_question::ExamQuestionId;
18use crate::domain::key;
19use crate::domain::monotonic_id::next_uuid;
20use crate::domain::note_file::FileContentType;
21use crate::domain::user::UserId;
22
23/// The identity of one (question, user, seq) triple — a sitting's drawing.
24/// Not a row column: the table's primary key *is* the (question, user, seq)
25/// triple, and this struct's job is the underscore-joined wire form at the
26/// HTTP edge.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct AnswerImageId {
29 pub(crate) question: ExamQuestionId,
30 pub(crate) user: UserId,
31 pub(crate) seq: i64,
32}
33
34impl AnswerImageId {
35 /// The one id a (question, user, seq) triple can have, so "one drawing per
36 /// student per question per sitting" needs no index. See [`key::sitting`]
37 /// for the wire shape and why the first sitting stays bare.
38 pub fn composite(question: &ExamQuestionId, user: &UserId, seq: i64) -> Self {
39 Self {
40 question: question.clone(),
41 user: *user,
42 seq,
43 }
44 }
45
46 /// The underscore-joined wire form (`{question}_{user}[_{seq}]`).
47 pub fn key(&self) -> String {
48 key::sitting(
49 self.question.key().as_str(),
50 self.user.key().as_str(),
51 self.seq,
52 )
53 }
54}
55
56/// One student's drawing for one question. `exam` is denormalized (like
57/// [`crate::domain::exam_answer::ExamAnswer`]) so per-exam reads (cascade) and
58/// per-(exam, user) reads (sitting/grading views, retake blob-GC) don't fan out
59/// through `exam_question`.
60#[derive(Debug, Clone, sqlx::FromRow)]
61pub struct AnswerImage {
62 pub(crate) exam: ExamId,
63 pub(crate) question: ExamQuestionId,
64 #[sqlx(rename = "app_user")]
65 pub(crate) user: UserId,
66 /// Which sitting this drawing belongs to — 1 for the first attempt,
67 /// counting up, so retakes accumulate instead of overwriting.
68 pub(crate) seq: i64,
69 /// The blob's on-disk name — a fresh UUID every upload.
70 pub(crate) file: String,
71 pub(crate) content_type: FileContentType,
72 pub(crate) size: i64,
73}
74
75impl AnswerImage {
76 /// Assemble a row (fresh blob name generated here) without persisting it.
77 /// The caller writes the blob under [`Self::get_file`] first, then calls
78 /// [`crate::db::answer_image::upsert`] — so a stored row always points at
79 /// a real blob.
80 pub fn new(
81 exam: &ExamId,
82 question: &ExamQuestionId,
83 user: &UserId,
84 seq: i64,
85 content_type: FileContentType,
86 size: i64,
87 ) -> Self {
88 Self {
89 exam: exam.clone(),
90 question: question.clone(),
91 user: *user,
92 seq,
93 file: next_uuid().to_string(),
94 content_type,
95 size,
96 }
97 }
98
99 pub fn get_question(&self) -> &ExamQuestionId {
100 &self.question
101 }
102
103 /// Which sitting this drawing belongs to — 1 for the first attempt.
104 pub fn get_seq(&self) -> i64 {
105 self.seq
106 }
107
108 pub fn get_file(&self) -> &str {
109 &self.file
110 }
111
112 pub fn get_content_type(&self) -> &FileContentType {
113 &self.content_type
114 }
115
116 pub fn get_size(&self) -> i64 {
117 self.size
118 }
119}