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.rs91 lines, 3.4 KiBHistoryRawDownload
1//! Pool-question funnels: ask, the two listings, the approve with its
2//! 409-vs-404 split, the pending-only image writes, and the delete whose
3//! swept solutions and collected image blob keys ride back for the web
4//! layer's blob cleanup. The queries and their transactions live in
5//! [`crate::db::pool_question`]; the pure entity and newtypes in
6//! [`crate::domain::pool_question`].
7
8use crate::database::Database;
9use crate::db::pool_question;
10use crate::domain::note_file::FileContentType;
11use crate::domain::pool_question::{PoolQuestion, PoolQuestionId};
12use crate::domain::user::UserId;
13use crate::error::AppError;
14
15pub async fn insert(db: &Database, question: PoolQuestion) -> Result<PoolQuestion, AppError> {
16 pool_question::insert(db, question).await
17}
18
19pub async fn read(db: &Database, id: &PoolQuestionId) -> Result<Option<PoolQuestion>, AppError> {
20 pool_question::read(db, id).await
21}
22
23/// One page of the caller's question list — visibility gate and optional
24/// status filter in one SQL `WHERE`, so `total` counts the filtered set.
25/// `viewer = None` is the teacher+ view (everything); `Some(me)` the pool
26/// plus the caller's own pending questions.
27pub async fn list(
28 db: &Database,
29 viewer: Option<&UserId>,
30 status: Option<&str>,
31 limit: Option<i64>,
32 offset: i64,
33) -> Result<(Vec<PoolQuestion>, i64), AppError> {
34 pool_question::list(db, viewer, status, limit, offset).await
35}
36
37/// Approve a pending question, stamping `approver`. A `None` from the
38/// guarded write is ambiguous — already approved, or gone — so this sorts it
39/// out with a read: the question missing is a 404, anything else is the 409
40/// the route's second approve answers.
41pub async fn approve(
42 db: &Database,
43 id: &PoolQuestionId,
44 approver: &UserId,
45) -> Result<PoolQuestion, AppError> {
46 match pool_question::approve(db, id, approver).await? {
47 Some(question) => Ok(question),
48 // Nothing was pending under that id: either it's already approved
49 // (409) or it never existed / was deleted (404).
50 None => {
51 if pool_question::read(db, id).await?.is_none() {
52 return Err(AppError::NotFound);
53 }
54 Err(AppError::Conflict("the question is already approved"))
55 }
56 }
57}
58
59/// Point the question at a freshly written image blob; `None` means the
60/// question was approved or deleted mid-upload (the fresh blob is the
61/// caller's orphan to take back off disk). `Some(replaced)` names the blob
62/// this upload displaced — the caller's to remove.
63pub async fn set_image(
64 db: &Database,
65 id: &PoolQuestionId,
66 file: &str,
67 content_type: &FileContentType,
68 size: i64,
69) -> Result<Option<Option<String>>, AppError> {
70 pool_question::set_image(db, id, file, content_type, size).await
71}
72
73/// Detach the question's image (pending only, like [`set_image`]); `None`
74/// means the question is no longer pending. `Some(detached)` names the
75/// removed blob — `None` inside when there was no image.
76pub async fn clear_image(
77 db: &Database,
78 id: &PoolQuestionId,
79) -> Result<Option<Option<String>>, AppError> {
80 pool_question::clear_image(db, id).await
81}
82
83/// Delete the question and cascade its solutions; the removed rows and the
84/// image blob keys they named come back so the web layer can take every
85/// blob off disk.
86pub async fn delete(
87 db: &Database,
88 id: &PoolQuestionId,
89) -> Result<Option<pool_question::Deleted>, AppError> {
90 pool_question::delete(db, id).await
91}