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.rs58 lines, 2.1 KiBHistoryRawDownload
1//! The question-image workflows: the doors the web layer takes for every
2//! question/choice image read and write. The blob I/O and its disk ordering
3//! stay in the web layer; the row transactions (behind the question-freeze
4//! gate) live in [`crate::db::question_image`].
5
6use crate::database::Database;
7use crate::db;
8use crate::domain::exam::ExamId;
9use crate::domain::exam_question::{ChoiceId, ExamQuestionId};
10use crate::domain::question_image::QuestionImage;
11use crate::error::AppError;
12
13/// Create or replace the slot's image row, handing back what it stored plus
14/// the blob name it replaced, for the caller to take off disk.
15pub async fn upsert(
16 db: &Database,
17 image: QuestionImage,
18) -> Result<(QuestionImage, Option<String>), AppError> {
19 db::question_image::upsert(db, image).await
20}
21
22/// The slot's image row, if any (`slot = None` is the question's
23/// illustration).
24pub async fn read_slot(
25 db: &Database,
26 question: &ExamQuestionId,
27 slot: Option<&ChoiceId>,
28) -> Result<Option<QuestionImage>, AppError> {
29 db::question_image::read_slot(db, question, slot).await
30}
31
32/// Every image of the exam's questions — one query for the list views.
33pub async fn list_for_exam(db: &Database, exam: &ExamId) -> Result<Vec<QuestionImage>, AppError> {
34 db::question_image::list_for_exam(db, exam).await
35}
36
37/// Every image of one question — the illustration plus its option pictures.
38pub async fn list_for_question(
39 db: &Database,
40 question: &ExamQuestionId,
41) -> Result<Vec<QuestionImage>, AppError> {
42 db::question_image::list_for_question(db, question).await
43}
44
45/// Drop the option pictures whose choice is gone, returning the removed rows
46/// so the caller can take their blobs off disk.
47pub async fn delete_choices_not_in(
48 db: &Database,
49 question: &ExamQuestionId,
50 keep: &[ChoiceId],
51) -> Result<Vec<QuestionImage>, AppError> {
52 db::question_image::delete_choices_not_in(db, question, keep).await
53}
54
55/// Drop one image row — the returned row names the blob to unlink.
56pub async fn delete(db: &Database, image: QuestionImage) -> Result<QuestionImage, AppError> {
57 db::question_image::delete(db, image).await
58}