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_question.rs134 lines, 4.7 KiBHistoryRawDownload
1//! Exam-question authoring rules: the freeze pre-flight every authoring path
2//! runs, the question-lookup and option-addressing gates, and the read and
3//! write funnels the web layer goes through. The queries and their
4//! transactions live in [`crate::db::exam_question`]; the HTTP shaping stays
5//! in the web layer.
6
7use crate::database::Database;
8use crate::domain::bank_question::BankQuestionId;
9use crate::domain::exam::ExamId;
10use crate::domain::exam_question::{
11 ChoiceId, ExamQuestion, ExamQuestionId, QuestionPoints, QuestionSpec, QuestionText,
12};
13use crate::domain::subject::SubjectId;
14use crate::error::{AppError, ValidationError};
15
16/// The questions freeze once anyone has started an attempt — editing them
17/// under a student mid-exam would fork what "the exam" means.
18pub async fn ensure_questions_editable(exam: &ExamId, db: &Database) -> Result<(), AppError> {
19 if crate::service::exam_attempt::any_for_exam(db, exam).await? {
20 return Err(AppError::Conflict(
21 "cannot change questions after attempts have started",
22 ));
23 }
24 Ok(())
25}
26
27/// The question's option named by `choice_id` — a 400 for a text question or an
28/// id the question doesn't have, so an option picture can only ever be
29/// addressed through an option that exists.
30pub fn choice_slot(question: &ExamQuestion, choice_id: &str) -> Result<ChoiceId, AppError> {
31 let Some(choices) = question.get_choices() else {
32 return Err(AppError::Validation(ValidationError::Invalid {
33 field: "choice_id",
34 reason: "only choice questions take option pictures",
35 }));
36 };
37 choices
38 .iter()
39 .find(|choice| choice.get_id().as_str() == choice_id)
40 .map(|choice| choice.get_id().clone())
41 .ok_or(AppError::Validation(ValidationError::Invalid {
42 field: "choice_id",
43 reason: "must name one of the choices",
44 }))
45}
46
47/// The question, provided it belongs to `exam` — a qid under someone else's
48/// exam is a plain 404, not a leak.
49pub async fn question_of_exam(
50 exam: &ExamId,
51 qid: &str,
52 db: &Database,
53) -> Result<ExamQuestion, AppError> {
54 let question = crate::db::exam_question::read(db, &ExamQuestionId::from_key(qid))
55 .await?
56 .ok_or(AppError::NotFound)?;
57 if question.get_exam() != exam {
58 return Err(AppError::NotFound);
59 }
60 Ok(question)
61}
62
63/// Add a question to an exam. The freeze gate and the subject's reference
64/// counter ride in the write's own transaction.
65pub async fn create(
66 db: &Database,
67 exam: &ExamId,
68 subject: SubjectId,
69 text: QuestionText,
70 points: QuestionPoints,
71 spec: QuestionSpec,
72) -> Result<ExamQuestion, AppError> {
73 crate::db::exam_question::create(db, exam, subject, text, points, spec).await
74}
75
76/// Like [`create`], but records the bank template the question was
77/// instantiated from.
78pub async fn create_from_bank(
79 db: &Database,
80 exam: &ExamId,
81 subject: SubjectId,
82 text: QuestionText,
83 points: QuestionPoints,
84 spec: QuestionSpec,
85 source: BankQuestionId,
86) -> Result<ExamQuestion, AppError> {
87 crate::db::exam_question::create_from_bank(db, exam, subject, text, points, spec, source).await
88}
89
90/// The exam's questions in presentation order — the web layer's paging read.
91pub async fn list_for_exam(
92 db: &Database,
93 exam: &ExamId,
94 limit: Option<i64>,
95 offset: i64,
96) -> Result<(Vec<ExamQuestion>, i64), AppError> {
97 crate::db::exam_question::list_for_exam(db, exam, limit, offset).await
98}
99
100/// The question ids of `exam` sharing a bank template with a question under
101/// one of `live` — the review hiding key.
102pub async fn list_shared_with(
103 db: &Database,
104 exam: &ExamId,
105 live: &[ExamId],
106) -> Result<std::collections::HashSet<String>, AppError> {
107 crate::db::exam_question::list_shared_with(db, exam, live).await
108}
109
110/// Write the editable fields; a stale read is a 409, nothing was written.
111pub async fn update(
112 db: &Database,
113 question: ExamQuestion,
114 subject: SubjectId,
115 text: QuestionText,
116 points: QuestionPoints,
117 spec: QuestionSpec,
118) -> Result<ExamQuestion, AppError> {
119 crate::db::exam_question::update(db, question, subject, text, points, spec).await
120}
121
122/// Delete the question and cascade its answers and image rows.
123pub async fn delete(db: &Database, question: ExamQuestion) -> Result<ExamQuestion, AppError> {
124 crate::db::exam_question::delete(db, question).await
125}
126
127/// Point the question's `banked_as` at the bank template it was saved into.
128pub async fn link_banked_as(
129 db: &Database,
130 question: ExamQuestion,
131 template: BankQuestionId,
132) -> Result<ExamQuestion, AppError> {
133 crate::db::exam_question::link_banked_as(db, question, template).await
134}