forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiBaf331ecAhmet Kaan Gümüş2 months ago451 commits

solution.rs80 lines, 2.6 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; the *before* row names
61/// the replaced blob the caller must remove.
62pub async fn set_image(
63 db: &Database,
64 id: &SolutionId,
65 file: &str,
66 content_type: &FileContentType,
67 size: i64,
68) -> Result<Option<Solution>, AppError> {
69 solution::set_image(db, id, file, content_type, size).await
70}
71
72/// Detach the solution's image; the *before* row names the blob the caller
73/// must remove.
74pub async fn clear_image(db: &Database, id: &SolutionId) -> Result<Option<Solution>, AppError> {
75 solution::clear_image(db, id).await
76}
77
78pub async fn delete(db: &Database, target: Solution) -> Result<Solution, AppError> {
79 solution::delete(db, target).await
80}