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

class_group.rs89 lines, 3.4 KiBHistoryRawDownload
1//! Class workflows: the archived-year guard every class write re-checks, and
2//! the thin route-facing wrappers the handlers call. The queries live in
3//! [`crate::db::class_group`].
4
5use crate::database::Database;
6use crate::db::class_group;
7use crate::domain::academic_year::AcademicYearId;
8use crate::domain::class_group::{ClassGroup, ClassGroupId, ClassName};
9use crate::domain::grade::GradeLevel;
10use crate::domain::user::UserId;
11use crate::error::AppError;
12
13/// Refuse the write when this class's year is archived — a pre-flight guard,
14/// accepted race (see README concurrency model): a year archived after this
15/// read still lets the write through.
16pub async fn require_open(db: &Database, class: &ClassGroup) -> Result<(), AppError> {
17 match class.get_year() {
18 None => Ok(()),
19 Some(year) => crate::service::academic_year::require_open(db, year).await,
20 }
21}
22
23/// Create a class, claiming the year reference in the same transaction —
24/// see [`class_group::create`].
25pub async fn create(
26 db: &Database,
27 creator: &UserId,
28 name: ClassName,
29 grade_level: GradeLevel,
30 year: Option<AcademicYearId>,
31 teacher: Option<UserId>,
32) -> Result<ClassGroup, AppError> {
33 class_group::create(db, creator, name, grade_level, year, teacher).await
34}
35
36/// The row, for callers that only inspect it — the web layer's
37/// `class_or_404` reads through here.
38pub async fn read(db: &Database, id: &ClassGroupId) -> Result<Option<ClassGroup>, AppError> {
39 class_group::read(db, id).await
40}
41
42/// Every class, newest first — or one rung's, when `grade_level` narrows it:
43/// the read behind the class index.
44pub async fn list_all(
45 db: &Database,
46 grade_level: Option<GradeLevel>,
47 limit: Option<i64>,
48 offset: i64,
49) -> Result<(Vec<ClassGroup>, i64), AppError> {
50 class_group::list_all(db, grade_level, limit, offset).await
51}
52
53/// The classes `ids` names, in no particular order — the join behind the two
54/// membership reads.
55pub async fn list_by_ids(db: &Database, ids: &[ClassGroupId]) -> Result<Vec<ClassGroup>, AppError> {
56 class_group::list_by_ids(db, ids).await
57}
58
59/// Every class `user` is the homeroom teacher of, newest first — the visible
60/// set a homeroom teacher reads their own instances through (D10), the read
61/// behind `visible_instances` in the web layer.
62pub async fn list_for_teacher(db: &Database, user: &UserId) -> Result<Vec<ClassGroup>, AppError> {
63 class_group::list_for_teacher(db, user).await
64}
65
66/// Write only the fields the PATCH carried; a year move claims the new year
67/// and releases the old one inside the very transaction that moves the link.
68pub async fn update(
69 db: &Database,
70 class: ClassGroup,
71 name: Option<ClassName>,
72 grade_level: Option<GradeLevel>,
73 year: Option<Option<AcademicYearId>>,
74 teacher: Option<Option<UserId>>,
75) -> Result<ClassGroup, AppError> {
76 class_group::update(db, class, name, grade_level, year, teacher).await
77}
78
79/// Delete the class and give its year reference back; `false` = refused,
80/// nothing was written (the class still holds students or courses).
81pub async fn delete(db: &Database, class: ClassGroup) -> Result<bool, AppError> {
82 class_group::delete(db, class).await
83}
84
85/// Strip `user` from every class they were the homeroom teacher of — the
86/// sweep for a user demoted below `teacher`.
87pub async fn unassign_everywhere(db: &Database, user: &UserId) -> Result<(), AppError> {
88 class_group::unassign_everywhere(db, user).await
89}