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

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