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

subject.rs115 lines, 4.8 KiBHistoryRawDownload
1//! Subject workflows: the id resolvers every question/homework tag goes
2//! through — the same-course rule for curriculum content, existence-only for
3//! the bank's cross-course origin metadata — over the row writes in
4//! [`crate::db::subject`]. Since the course-template system, the tag gate for
5//! instance-scoped content is [`in_instance`]: the same-course rule *plus*
6//! the section's resolved subject set (override-or-inherit), because a
7//! section teaches a selection of the course, not the whole course.
8
9use crate::database::Database;
10use crate::db::subject;
11use crate::domain::class_course::ClassCourse;
12use crate::domain::course::CourseId;
13use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName};
14use crate::error::{AppError, ValidationError};
15
16pub async fn create(
17 db: &Database,
18 course: &CourseId,
19 name: SubjectName,
20 description: SubjectDescription,
21) -> Result<Subject, AppError> {
22 subject::create(db, course, name, description).await
23}
24
25/// The subject row, for callers that only inspect it — the web layer's gates
26/// read through here.
27pub async fn read(db: &Database, id: &SubjectId) -> Result<Option<Subject>, AppError> {
28 subject::read(db, id).await
29}
30
31/// Several subjects in one query — the bulk half of a list endpoint that
32/// names each row's subject (a read per row would be an N+1).
33pub async fn list_by_ids(db: &Database, ids: &[&SubjectId]) -> Result<Vec<Subject>, AppError> {
34 subject::list_by_ids(db, ids).await
35}
36
37pub async fn list_for_course(
38 db: &Database,
39 course: &CourseId,
40 limit: Option<i64>,
41 offset: i64,
42) -> Result<(Vec<Subject>, i64), AppError> {
43 subject::list_for_course(db, course, limit, offset).await
44}
45
46pub async fn update(
47 db: &Database,
48 target: Subject,
49 name: Option<SubjectName>,
50 description: Option<SubjectDescription>,
51) -> Result<Subject, AppError> {
52 subject::update(db, target, name, description).await
53}
54
55pub async fn delete(db: &Database, target: Subject) -> Result<Subject, AppError> {
56 subject::delete(db, target).await
57}
58
59/// Turn a request-supplied subject id into a validated reference, provided the
60/// subject belongs to `course`. The *course half* of the instance tag gate —
61/// a question may only be tagged with a subject of its own exam's course.
62/// Instance-scoped writers use [`in_instance`], which adds the section's
63/// resolved subject set on top; this fn stays for the callers that have only
64/// the course at hand. Unknown or foreign subjects are a `400` naming the
65/// field. Shared by the question create/update handlers.
66pub async fn in_course(db: &Database, id: &str, course: &CourseId) -> Result<SubjectId, AppError> {
67 let subject =
68 subject::read(db, &SubjectId::from_key(id))
69 .await?
70 .ok_or(AppError::Validation(ValidationError::Invalid {
71 field: "subject_id",
72 reason: "subject does not exist",
73 }))?;
74 if subject.get_course() != course {
75 return Err(AppError::Validation(ValidationError::Invalid {
76 field: "subject_id",
77 reason: "subject belongs to a different course",
78 }));
79 }
80 Ok(*subject.get_id())
81}
82
83/// The instance-scoped tag gate: the same two 400s [`in_course`] answers for
84/// the instance's course — same order, so the first 4xx a client sees is
85/// unchanged — then the course-template half: the subject must sit in what
86/// this section actually *teaches*, its resolved set
87/// ([`crate::service::offering_subject::resolved_for_instance`]), not merely
88/// in the catalog course. A right-course-but-off-syllabus subject is a `400`
89/// naming the field. The resolved-set probe is one SQL statement that reads
90/// the section's flag and the matching table as of one snapshot
91/// ([`crate::db::offering_subject::resolves_for_class_course`]).
92pub async fn in_instance(
93 db: &Database,
94 id: &str,
95 instance: &ClassCourse,
96) -> Result<SubjectId, AppError> {
97 let subject_id = in_course(db, id, instance.get_course()).await?;
98 crate::service::offering_subject::ensure_resolved_member(db, instance, &subject_id).await?;
99 Ok(subject_id)
100}
101
102/// Turn a request-supplied subject id into a validated reference, checking only
103/// that the subject exists — no course tie. For the question bank, whose subject
104/// is cross-course origin metadata: the same-course rule applies at instantiate
105/// time, not here. An unknown subject is a `400` naming the field.
106pub async fn must_exist(db: &Database, id: &str) -> Result<SubjectId, AppError> {
107 let subject =
108 subject::read(db, &SubjectId::from_key(id))
109 .await?
110 .ok_or(AppError::Validation(ValidationError::Invalid {
111 field: "subject_id",
112 reason: "subject does not exist",
113 }))?;
114 Ok(*subject.get_id())
115}