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

exam_answer.rs71 lines, 2.3 KiBHistoryRawDownload
1//! The sitting answer sheet's workflows: the doors the web layer takes for
2//! every answer read and write (the gate chain itself lives in
3//! [`crate::service::exam_attempt`], which funnels every save through
4//! [`save`]). The queries live in [`crate::db::exam_answer`].
5
6use crate::database::Database;
7use crate::db;
8use crate::domain::exam::ExamId;
9use crate::domain::exam_answer::ExamAnswer;
10use crate::domain::exam_question::{ExamQuestion, ExamQuestionId};
11use crate::domain::user::UserId;
12use crate::error::AppError;
13
14/// Save (or overwrite) one answer — the payload validation and the atomic
15/// exam-row-touching upsert. The caller has already checked that the attempt
16/// is in progress.
17pub async fn save(
18 db: &Database,
19 question: &ExamQuestion,
20 user: &UserId,
21 seq: i64,
22 selected: Option<String>,
23 text: Option<String>,
24) -> Result<ExamAnswer, AppError> {
25 db::exam_answer::save(db, question, user, seq, selected, text).await
26}
27
28/// One student's stored answer for a question in sitting `seq`, if any.
29pub async fn read(
30 db: &Database,
31 question: &ExamQuestionId,
32 user: &UserId,
33 seq: i64,
34) -> Result<Option<ExamAnswer>, AppError> {
35 db::exam_answer::read(db, question, user, seq).await
36}
37
38/// Drop one student's answer to a single question in sitting `seq`.
39pub async fn delete(
40 db: &Database,
41 question: &ExamQuestionId,
42 user: &UserId,
43 seq: i64,
44) -> Result<(), AppError> {
45 db::exam_answer::delete(db, question, user, seq).await
46}
47
48/// One student's answers for a single sitting (`seq`) across an exam.
49pub async fn list_for_exam_user(
50 db: &Database,
51 exam: &ExamId,
52 user: &UserId,
53 seq: i64,
54) -> Result<Vec<ExamAnswer>, AppError> {
55 db::exam_answer::list_for_exam_user(db, exam, user, seq).await
56}
57
58/// Every answer of an exam — the live monitor aggregates these per student.
59pub async fn list_for_exam(db: &Database, exam: &ExamId) -> Result<Vec<ExamAnswer>, AppError> {
60 db::exam_answer::list_for_exam(db, exam).await
61}
62
63/// The distinct sittings a student has any answer for at `exam`, ascending
64/// — the index a history view lists attempts from.
65pub async fn list_seqs_for_user(
66 db: &Database,
67 exam: &ExamId,
68 user: &UserId,
69) -> Result<Vec<i64>, AppError> {
70 db::exam_answer::list_seqs_for_user(db, exam, user).await
71}