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

course_offering.rs95 lines, 3.6 KiBHistoryRawDownload
1//! Offering workflows: the one invariant the web layer cannot express alone —
2//! only a **class-delivered** course (`kind = 'course'`) has grade-level
3//! offerings, because only such a course is ever attached to a class section
4//! ([`crate::domain::course::CourseKind::is_class_delivered`]). A club or an
5//! etüt is joined individually, has no grade, and so has no template to
6//! inherit from.
7//!
8//! Everything else (the duplicate template's `offering_exists`, the in-use
9//! delete guard) is the store's own answer, mapped in
10//! [`crate::db::course_offering`]. The manager+ gate lives in the web layer.
11
12use crate::database::Database;
13use crate::db::{course, course_offering};
14use crate::domain::class_course::DersSaati;
15use crate::domain::course::{CourseDescription, CourseId, CourseTitle};
16use crate::domain::course_offering::{CourseOffering, CourseOfferingId};
17use crate::domain::grade::GradeLevel;
18use crate::domain::user::UserId;
19use crate::error::{AppError, ValidationError};
20
21/// Mint the grade-level template for a course. A course that is not
22/// class-delivered is a 400 (there is no grade an offering could hang off);
23/// a template that already exists is the db layer's 409 `offering_exists`.
24#[allow(clippy::too_many_arguments)] // the create body, spelled field by field
25pub async fn create(
26 db: &Database,
27 by: &UserId,
28 course_id: &CourseId,
29 grade: GradeLevel,
30 title: Option<CourseTitle>,
31 description: Option<CourseDescription>,
32 default_ders_saati: Option<DersSaati>,
33 default_counts_toward_karne: Option<bool>,
34) -> Result<CourseOffering, AppError> {
35 let course = course::read(db, course_id).await?.ok_or(AppError::NotFound)?;
36 if !course.get_kind().is_class_delivered() {
37 return Err(AppError::Validation(ValidationError::Invalid {
38 field: "course",
39 reason: "only a ders has grade-level offerings — a club or etüt is joined individually, not attached",
40 }));
41 }
42 course_offering::create(
43 db,
44 course.get_id(),
45 grade,
46 title,
47 description,
48 default_ders_saati,
49 default_counts_toward_karne,
50 by,
51 )
52 .await
53}
54
55/// One offering by id, or `None` when the id names no row — the read behind
56/// `GET /offerings/{id}`.
57pub async fn read(
58 db: &Database,
59 id: &CourseOfferingId,
60) -> Result<Option<CourseOffering>, AppError> {
61 course_offering::read(db, id).await
62}
63
64/// The offerings, optionally narrowed by course and/or grade ladder rung,
65/// paged — the read behind `GET /offerings?course=&grade_level=`.
66pub async fn list(
67 db: &Database,
68 course: Option<&CourseId>,
69 grade: Option<GradeLevel>,
70 limit: Option<i64>,
71 offset: i64,
72) -> Result<(Vec<CourseOffering>, i64), AppError> {
73 course_offering::list(db, course, grade, limit, offset).await
74}
75
76/// Field-scoped PATCH: absent keeps, `null` clears back to inherit, a value
77/// sets the override. Shape flows straight through to
78/// [`course_offering::update`]; a gone id is a 404 there.
79pub async fn update(
80 db: &Database,
81 id: &CourseOfferingId,
82 title: Option<Option<CourseTitle>>,
83 description: Option<Option<CourseDescription>>,
84 default_ders_saati: Option<Option<DersSaati>>,
85 default_counts_toward_karne: Option<Option<bool>>,
86) -> Result<CourseOffering, AppError> {
87 course_offering::update(db, id, title, description, default_ders_saati, default_counts_toward_karne)
88 .await
89}
90
91/// Delete the template. In use by any instance → 409 `offering_in_use`; a
92/// gone id → 404.
93pub async fn delete(db: &Database, id: &CourseOfferingId) -> Result<(), AppError> {
94 course_offering::delete(db, id).await
95}