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.rs91 lines, 3.0 KiBHistoryRawDownload
1//! Solution funnels: the offer (through the question's existence-move), the
2//! question-scoped read and page, the grouped counts, and the unconditional
3//! body/image writes of an unmoderated row. The queries live in
4//! [`crate::db::solution`]; the pure entity and newtypes in
5//! [`crate::domain::solution`].
6
7use std::collections::HashMap;
8
9use crate::database::Database;
10use crate::db::solution;
11use crate::domain::note_file::FileContentType;
12use crate::domain::pool_question::PoolQuestionId;
13use crate::domain::solution::{Solution, SolutionBody, SolutionId};
14use crate::error::AppError;
15
16/// Offer the solution; `NotFound` = the question is gone, and nothing was
17/// written.
18pub async fn insert(db: &Database, offer: Solution) -> Result<Solution, AppError> {
19 solution::insert(db, offer).await
20}
21
22/// Read a solution only if it belongs to `question` — keeps the nested
23/// route honest (a solution id under someone else's question 404s).
24pub async fn read_for(
25 db: &Database,
26 id: &SolutionId,
27 question: &PoolQuestionId,
28) -> Result<Option<Solution>, AppError> {
29 solution::read_for(db, id, question).await
30}
31
32/// The question's solutions, oldest first.
33pub async fn list_for(
34 db: &Database,
35 question: &PoolQuestionId,
36 limit: Option<i64>,
37 offset: i64,
38) -> Result<(Vec<Solution>, i64), AppError> {
39 solution::list_for(db, question, limit, offset).await
40}
41
42/// Per-question solution tallies for a page of questions, in one grouped
43/// query.
44pub async fn counts_for(
45 db: &Database,
46 questions: &[PoolQuestionId],
47) -> Result<HashMap<String, i64>, AppError> {
48 solution::counts_for(db, questions).await
49}
50
51/// Replace the body; `None` only means the row was deleted mid-flight.
52pub async fn set_body(
53 db: &Database,
54 id: &SolutionId,
55 body: &SolutionBody,
56) -> Result<Option<Solution>, AppError> {
57 solution::set_body(db, id, body).await
58}
59
60/// Point the solution at a freshly written image blob; `None` means the row
61/// was deleted mid-flight (the fresh blob is the caller's orphan to take
62/// back off disk). `Some(replaced)` names the blob this upload displaced —
63/// the caller's to remove.
64pub async fn set_image(
65 db: &Database,
66 id: &SolutionId,
67 file: &str,
68 content_type: &FileContentType,
69 size: i64,
70) -> Result<Option<Option<String>>, AppError> {
71 solution::set_image(db, id, file, content_type, size).await
72}
73
74/// Detach the solution's image; `None` means the row was deleted mid-flight.
75/// `Some(detached)` names the removed blob — `None` inside when there was no
76/// image.
77pub async fn clear_image(
78 db: &Database,
79 id: &SolutionId,
80) -> Result<Option<Option<String>>, AppError> {
81 solution::clear_image(db, id).await
82}
83
84/// Delete a solution; the pair carries the row and the blob name of the
85/// photo it carried (the caller's disk-GC list). `None` = already gone.
86pub async fn delete(
87 db: &Database,
88 target: Solution,
89) -> Result<Option<(Solution, Option<String>)>, AppError> {
90 solution::delete(db, target).await
91}