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_image.rs53 lines, 2.0 KiBHistoryRawDownload
1//! The `pool_question_image` table: the reads behind the pool photo
2//! endpoints. One row per question at most (`question` is the primary key);
3//! the writes ride the question's own guarded transactions in
4//! [`crate::db::pool_question`] (`set_image`/`clear_image`/`delete`), where
5//! the pending-freeze discipline and the replaced-blob accounting live. The
6//! row's pure half lives in [`crate::domain::pool_question_image`]; the blob
7//! bytes stay the web layer's.
8
9use crate::database::Database;
10use crate::domain::note_file::FileContentType;
11use crate::domain::pool_question::PoolQuestionId;
12use crate::domain::pool_question_image::PoolQuestionImage;
13use crate::error::AppError;
14
15/// The question's photo row, if it carries one — the blob-serving read.
16pub async fn read(
17 db: &Database,
18 question: &PoolQuestionId,
19) -> Result<Option<PoolQuestionImage>, AppError> {
20 let row = sqlx::query_as!(
21 PoolQuestionImage,
22 r#"SELECT question AS "question: PoolQuestionId", file,
23 content_type AS "content_type: FileContentType", size
24 FROM pool_question_image WHERE question = $1"#,
25 question.uuid()
26 )
27 .fetch_optional(db)
28 .await?;
29 Ok(row)
30}
31
32/// The photo rows of several questions in one query — for bucketing onto a
33/// listing's *page* (one image per question at most, so a page of `n`
34/// questions reads at most `n` rows).
35pub async fn list_for_questions(
36 db: &Database,
37 questions: &[&PoolQuestionId],
38) -> Result<Vec<PoolQuestionImage>, AppError> {
39 if questions.is_empty() {
40 return Ok(Vec::new());
41 }
42 let ids: Vec<uuid::Uuid> = questions.iter().map(|q| q.uuid()).collect();
43 let rows = sqlx::query_as!(
44 PoolQuestionImage,
45 r#"SELECT question AS "question: PoolQuestionId", file,
46 content_type AS "content_type: FileContentType", size
47 FROM pool_question_image WHERE question = ANY($1)"#,
48 &ids
49 )
50 .fetch_all(db)
51 .await?;
52 Ok(rows)
53}