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

homework_file.rs96 lines, 3.9 KiBHistoryRawDownload
1//! Homework file workflows: the attachment of one file to the caller's
2//! submission (the graded gate plus the auto-created seat it hangs off) and
3//! the pass-through reads the download paths scope through. The blob bytes
4//! and their ordering (blob before row on upload, row before blob on delete)
5//! stay the web layer's — it owns `files_path`. The row writes live in
6//! [`crate::db::homework_file`].
7
8use crate::database::Database;
9use crate::db::homework_file;
10use crate::db::homework_submission;
11use crate::domain::homework::Homework;
12use crate::domain::homework_file::{HomeworkFile, HomeworkFileId};
13use crate::domain::homework_submission::HomeworkSubmission;
14use crate::domain::homework_submission::HomeworkSubmissionId;
15use crate::domain::user::User;
16use crate::error::AppError;
17
18/// The submission the file will hang off, created if absent — a photo-only
19/// homework never types text, so an upload auto-creates an empty submission
20/// to carry the file (an existing one's text is preserved — `upsert` is
21/// called with `preserve_text`, so even a rival hand-in landing mid-request
22/// keeps its text). A photo-only hand-in is a hand-in: it creates the row,
23/// so it moves the counters, so it earns badges exactly as a text submit
24/// does. Refused (409) when a grade has frozen the homework.
25pub async fn ensure_can_attach(
26 db: &Database,
27 user: &User,
28 homework: &Homework,
29) -> Result<HomeworkSubmission, AppError> {
30 const GRADED: AppError = AppError::Conflict(
31 "this homework has been graded — ask the teacher to remove the grade before adding files",
32 );
33 // The common-case gate; the freeze itself rides on the write below.
34 if crate::db::homework_result::read_for(db, homework.get_id(), user.get_id())
35 .await?
36 .is_some()
37 {
38 return Err(GRADED);
39 }
40 match homework_submission::read_for(db, homework.get_id(), user.get_id()).await? {
41 Some(existing) => Ok(existing),
42 None => {
43 let Some((created, fresh)) =
44 homework_submission::upsert(db, homework, user.get_id(), None, true).await?
45 else {
46 return Err(GRADED);
47 };
48 if fresh {
49 crate::service::homework::award_badges(user.get_id(), db).await;
50 }
51 Ok(created)
52 }
53 }
54}
55
56/// Persist the assembled row — the seat claim and the freeze in one
57/// guarded insert (see [`homework_file::insert`]). `Ok(None)` is the
58/// freeze; the web layer keeps its own wording and unlinks the blob it wrote.
59pub async fn insert(db: &Database, file: HomeworkFile) -> Result<Option<HomeworkFile>, AppError> {
60 homework_file::insert(db, file).await
61}
62
63/// Delete one file row and give its seat back in the same transaction.
64/// `Ok(None)` is the freeze biting (the caller answers 409); `Err(NotFound)`
65/// means the row had already vanished.
66pub async fn delete(db: &Database, file: HomeworkFile) -> Result<Option<HomeworkFile>, AppError> {
67 homework_file::delete(db, file).await
68}
69
70/// Read a file's row only if it belongs to `submission` — the student's
71/// download path, behind the submission gate.
72pub async fn read_for(
73 db: &Database,
74 id: &HomeworkFileId,
75 submission: &HomeworkSubmissionId,
76) -> Result<Option<HomeworkFile>, AppError> {
77 homework_file::read_for(db, id, submission).await
78}
79
80/// Read a file by id only if it hangs off a submission to `homework` — the
81/// grader's download scoping.
82pub async fn read_in_homework(
83 db: &Database,
84 id: &HomeworkFileId,
85 homework: &crate::domain::homework::HomeworkId,
86) -> Result<Option<HomeworkFile>, AppError> {
87 homework_file::read_in_homework(db, id, homework).await
88}
89
90/// All of `submission`'s files, newest first.
91pub async fn list_for_submission(
92 db: &Database,
93 submission: &HomeworkSubmissionId,
94) -> Result<Vec<HomeworkFile>, AppError> {
95 homework_file::list_for_submission(db, submission).await
96}