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

instance_resolve.rs185 lines, 8.2 KiBHistoryRawDownload
1//! The composed resolved view of one instance — the one call every
2//! instance-scoped display rides.
3//!
4//! Wave 1/2 landed each axis's own resolver; this module composes them, so a
5//! read surface never re-implements a fallback chain. The scalar content runs
6//! instance → offering → catalog (`title`/`description`) or → constant
7//! (`ders_saati` 1, `counts_toward_karne` TRUE); the set axes stay
8//! flag-driven, exactly as their own resolvers rule — **override-or-inherit,
9//! never merge**.
10//!
11//! Everything here is composition: [`resolve`] calls
12//! [`crate::service::class_course::resolve_policy`],
13//! [`crate::service::offering_subject::resolved_for_instance`],
14//! [`crate::service::exam_weight::resolved_for_class`] and
15//! [`crate::service::weekly_slot::resolved_for_instance`] and adds only the
16//! title/description chain (whose two upper levels are a plain row read).
17//! [`resolved_content`] is the batch twin of that chain for report surfaces
18//! that already batch (karne, marks, attendance): two queries per batch,
19//! never one per instance.
20
21use std::collections::HashMap;
22
23use crate::database::Database;
24use crate::domain::class_course::{ClassCourse, DersSaati};
25use crate::domain::course::Course;
26use crate::domain::course_offering::{CourseOffering, CourseOfferingId};
27use crate::domain::exam_weight::ExamWeight;
28use crate::domain::subject::Subject;
29use crate::domain::weekly_slot::WeeklySlot;
30use crate::error::AppError;
31
32/// One instance's whole effective content: the resolved scalars plus *how*
33/// each axis was sourced. A client renders this verbatim and never
34/// re-implements the chain — the `*_overridden` flags tell an override from
35/// an inherited value, and the three `*_inherited` flags tell whose set
36/// (this section's own, possibly empty, or the offering template's) each
37/// resolved set answers from.
38#[derive(Debug, Clone)]
39pub struct ResolvedInstance {
40 /// The display title: this section's override, else its offering's, else
41 /// the catalog course's.
42 pub title: String,
43 /// `true` when `title` is this section's own override.
44 pub title_overridden: bool,
45 /// The display description, same chain as [`Self::title`].
46 pub description: String,
47 /// `true` when `description` is this section's own override.
48 pub description_overridden: bool,
49 /// The effective weekly hours: override, else offering default, else 1.
50 pub ders_saati: DersSaati,
51 /// `true` when the hours are this section's own override.
52 pub ders_saati_overridden: bool,
53 /// The effective report-card policy: override, else offering default,
54 /// else counted.
55 pub counts_toward_karne: bool,
56 /// `true` when the policy is this section's own override.
57 pub counts_toward_karne_overridden: bool,
58 /// `false` = the section's own subject table is authoritative, including
59 /// when empty; `true` = the offering's set applies.
60 pub subjects_inherited: bool,
61 /// The syllabus the section teaches, resolved override-or-inherit.
62 pub subjects: Vec<Subject>,
63 /// The same switch for the section's exam-kind weights.
64 pub exam_weights_inherited: bool,
65 /// The section's effective weight map — every kind at its resolved
66 /// weight (own rows in force, else the offering → settings → 1 chain).
67 pub exam_weights: Vec<ExamWeight>,
68 /// The same switch for the section's weekly plan.
69 pub weekly_plan_inherited: bool,
70 /// The resolved plan: the offering's template week while inheriting, the
71 /// section's own rows (possibly empty) once it overrode.
72 pub weekly_plan: Vec<WeeklySlot>,
73}
74
75/// The title/description a batch of instances should display, keyed by
76/// instance id key — the chain run once per batch: one offering read and one
77/// catalog read for the whole set, then the instance's own override judged
78/// in memory. An instance whose catalog row is unreachable renders an empty
79/// string (the delete cascade makes that unreachable too; the karne lines
80/// degrade rather than 500, the same as before this module existed).
81pub async fn resolved_content(
82 db: &Database,
83 instances: &[&ClassCourse],
84) -> Result<HashMap<String, ResolvedContent>, AppError> {
85 let mut offering_ids: Vec<CourseOfferingId> = Vec::new();
86 let mut course_ids: Vec<crate::domain::course::CourseId> = Vec::new();
87 for instance in instances {
88 if !offering_ids.contains(instance.get_offering()) {
89 offering_ids.push(instance.get_offering().clone());
90 }
91 if !course_ids.contains(instance.get_course()) {
92 course_ids.push(instance.get_course().clone());
93 }
94 }
95 let offerings = crate::db::course_offering::list_by_ids(db, &offering_ids).await?;
96 let courses = crate::db::course::list_by_ids(db, &course_ids).await?;
97 let offering_by_key: HashMap<String, &CourseOffering> = offerings
98 .iter()
99 .map(|offering| (offering.get_id().key(), offering))
100 .collect();
101 let course_by_key: HashMap<String, &Course> = courses
102 .iter()
103 .map(|course| (course.get_id().key(), course))
104 .collect();
105
106 Ok(instances
107 .iter()
108 .map(|instance| {
109 let template = offering_by_key
110 .get(&instance.get_offering().key())
111 .copied();
112 let catalog = course_by_key.get(&instance.get_course().key()).copied();
113 let title = instance
114 .get_title()
115 .or(template.and_then(|o| o.get_title()))
116 .or_else(|| catalog.map(|c| c.get_title()))
117 .map(|title| title.as_str().to_string())
118 .unwrap_or_default();
119 let description = instance
120 .get_description()
121 .or(template.and_then(|o| o.get_description()))
122 .or_else(|| catalog.map(|c| c.get_description()))
123 .map(|description| description.as_str().to_string())
124 .unwrap_or_default();
125 (
126 instance.get_id().key().to_string(),
127 ResolvedContent { title, description },
128 )
129 })
130 .collect())
131}
132
133/// The display strings of one instance: its resolved title and description.
134pub struct ResolvedContent {
135 pub title: String,
136 pub description: String,
137}
138
139/// Compose the whole resolved view of `instance` — the one call a read
140/// surface pays instead of layering its own fallbacks. A gone offering or
141/// catalog row is a `NotFound` (the `RESTRICT` foreign keys make that
142/// unreachable; a missing row is never silently read as an empty template).
143pub async fn resolve(
144 db: &Database,
145 instance: &ClassCourse,
146) -> Result<ResolvedInstance, AppError> {
147 let offering = crate::db::course_offering::read(db, instance.get_offering())
148 .await?
149 .ok_or(AppError::NotFound)?;
150 let course = crate::db::course::read(db, instance.get_course())
151 .await?
152 .ok_or(AppError::NotFound)?;
153 let policy = crate::service::class_course::resolve_policy(db, instance).await?;
154 let subjects =
155 crate::service::offering_subject::resolved_for_instance(db, instance).await?;
156 let (exam_weights_inherited, exam_weights) =
157 crate::service::exam_weight::resolved_for_class(db, instance).await?;
158 let weekly_plan = crate::service::weekly_slot::resolved_for_instance(db, instance).await?;
159
160 let title = instance
161 .get_title()
162 .or(offering.get_title())
163 .unwrap_or_else(|| course.get_title());
164 let description = instance
165 .get_description()
166 .or(offering.get_description())
167 .unwrap_or_else(|| course.get_description());
168
169 Ok(ResolvedInstance {
170 title: title.as_str().to_string(),
171 title_overridden: instance.get_title().is_some(),
172 description: description.as_str().to_string(),
173 description_overridden: instance.get_description().is_some(),
174 ders_saati: policy.ders_saati,
175 ders_saati_overridden: instance.get_ders_saati().is_some(),
176 counts_toward_karne: policy.counts_toward_karne,
177 counts_toward_karne_overridden: instance.counts_toward_karne().is_some(),
178 subjects_inherited: instance.subjects_inherited(),
179 subjects,
180 exam_weights_inherited,
181 exam_weights,
182 weekly_plan_inherited: instance.weekly_plan_inherited(),
183 weekly_plan,
184 })
185}