Per-section course lists behind the catalogue row
Browse files at this commit6ecaebc6323cb906c798b0747712c3007227768eParent: 7c481f7- feat
- per-section course lists behind the catalogue row
6 changed files +469 -82
- .sqlx/query-2577d62a94114c67d37fed2a9dd48c4396bbe0cc51a34b91a5d9ab9e066df099.json +76 -0
- src/db/enrollment.rs +23 -0
- src/service/course_session.rs +9 -6
- src/web/courses.rs +233 -41
- src/web/dto.rs +60 -0
- src/web/users.rs +68 -35
.sqlx/query-2577d62a94114c67d37fed2a9dd48c4396bbe0cc51a34b91a5d9ab9e066df099.jsonadded+76 -0
| @@ -0,0 +1,76 @@ | ||
| 1 | +{ | |
| 2 | + "db_name": "PostgreSQL", | |
| 3 | + "query": "SELECT class_course AS \"class_course: ClassCourseId\", app_user AS \"user: UserId\",\n enrolled_by AS \"enrolled_by: UserId\", source AS \"source: ClassGroupId\",\n created_at AS \"created_at: Timestamp\"\n FROM enrollment WHERE app_user = $1 ORDER BY class_course", | |
| 4 | + "describe": { | |
| 5 | + "columns": [ | |
| 6 | + { | |
| 7 | + "ordinal": 0, | |
| 8 | + "name": "class_course: ClassCourseId", | |
| 9 | + "type_info": "Uuid", | |
| 10 | + "origin": { | |
| 11 | + "Table": { | |
| 12 | + "table": "enrollment", | |
| 13 | + "name": "class_course" | |
| 14 | + } | |
| 15 | + } | |
| 16 | + }, | |
| 17 | + { | |
| 18 | + "ordinal": 1, | |
| 19 | + "name": "user: UserId", | |
| 20 | + "type_info": "Uuid", | |
| 21 | + "origin": { | |
| 22 | + "Table": { | |
| 23 | + "table": "enrollment", | |
| 24 | + "name": "app_user" | |
| 25 | + } | |
| 26 | + } | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + "ordinal": 2, | |
| 30 | + "name": "enrolled_by: UserId", | |
| 31 | + "type_info": "Uuid", | |
| 32 | + "origin": { | |
| 33 | + "Table": { | |
| 34 | + "table": "enrollment", | |
| 35 | + "name": "enrolled_by" | |
| 36 | + } | |
| 37 | + } | |
| 38 | + }, | |
| 39 | + { | |
| 40 | + "ordinal": 3, | |
| 41 | + "name": "source: ClassGroupId", | |
| 42 | + "type_info": "Uuid", | |
| 43 | + "origin": { | |
| 44 | + "Table": { | |
| 45 | + "table": "enrollment", | |
| 46 | + "name": "source" | |
| 47 | + } | |
| 48 | + } | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + "ordinal": 4, | |
| 52 | + "name": "created_at: Timestamp", | |
| 53 | + "type_info": "Int8", | |
| 54 | + "origin": { | |
| 55 | + "Table": { | |
| 56 | + "table": "enrollment", | |
| 57 | + "name": "created_at" | |
| 58 | + } | |
| 59 | + } | |
| 60 | + } | |
| 61 | + ], | |
| 62 | + "parameters": { | |
| 63 | + "Left": [ | |
| 64 | + "Uuid" | |
| 65 | + ] | |
| 66 | + }, | |
| 67 | + "nullable": [ | |
| 68 | + false, | |
| 69 | + false, | |
| 70 | + false, | |
| 71 | + true, | |
| 72 | + false | |
| 73 | + ] | |
| 74 | + }, | |
| 75 | + "hash": "2577d62a94114c67d37fed2a9dd48c4396bbe0cc51a34b91a5d9ab9e066df099" | |
| 76 | +} |
src/db/enrollment.rsmodified+23 -0
| @@ -224,6 +224,29 @@ pub async fn read_for_user( | ||
| 224 | 224 | Ok(row) |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | +/// Every instance `user` holds an enrollment row on, in one statement for the | |
| 228 | +/// whole set — the read behind the surfaces that must list a section a **hand** | |
| 229 | +/// enrollment reaches. A hand-placed row (and a class row an operator later | |
| 230 | +/// disowned, `source` NULL) is on no class roster, so | |
| 231 | +/// [`crate::service::instance::visible_instances`] — keyed on the class — never | |
| 232 | +/// sees it; `/courses/me` and the profile course block listed it before the | |
| 233 | +/// section split, so they union this in. Ordered by instance id for a stable | |
| 234 | +/// answer; a user with no enrollment reads an empty list, and the caller | |
| 235 | +/// resolves the ids through [`crate::db::class_course`] (one batch read). | |
| 236 | +pub async fn list_for_user(db: &Database, user: &UserId) -> Result<Vec<Enrollment>, AppError> { | |
| 237 | + let rows = sqlx::query_as!( | |
| 238 | + Enrollment, | |
| 239 | + r#"SELECT class_course AS "class_course: ClassCourseId", app_user AS "user: UserId", | |
| 240 | + enrolled_by AS "enrolled_by: UserId", source AS "source: ClassGroupId", | |
| 241 | + created_at AS "created_at: Timestamp" | |
| 242 | + FROM enrollment WHERE app_user = $1 ORDER BY class_course"#, | |
| 243 | + user.uuid(), | |
| 244 | + ) | |
| 245 | + .fetch_all(db) | |
| 246 | + .await?; | |
| 247 | + Ok(rows) | |
| 248 | +} | |
| 249 | + | |
| 227 | 250 | /// The roster an exam is sat by: every enrollment of every instance the exam |
| 228 | 251 | /// is addressed to (`exam_audience`, D2), deduped by student — the live |
| 229 | 252 | /// monitor's roster read, and the one that makes an announced-to section's |
src/service/course_session.rsmodified+9 -6
| @@ -178,16 +178,18 @@ pub async fn materialize( | ||
| 178 | 178 | // the generated rows are written on that same connection: a rival |
| 179 | 179 | // materialize run waits here instead of racing the existence read, and a |
| 180 | 180 | // refusal rolls back whatever it had collected. |
| 181 | + let pool = db.clone(); | |
| 182 | + let instance = instance.clone(); | |
| 181 | 183 | tx_with_retry(db, false, async move |tx| { |
| 182 | 184 | // The archived-year wall every instance write passes: a past year is |
| 183 | 185 | // read-only, and this run mints lesson rows. |
| 184 | - crate::service::class_course::require_open(db, &instance_id).await?; | |
| 186 | + crate::service::class_course::require_open(&pool, &instance_id).await?; | |
| 185 | 187 | // The serialization point. `FOR NO KEY UPDATE` rather than |
| 186 | 188 | // `FOR UPDATE`: the batch insert below takes this same row's |
| 187 | 189 | // foreign-key `FOR KEY SHARE`, which `FOR UPDATE` would block on. |
| 188 | 190 | crate::db::weekly_slot::lock_class_tx(tx, &instance_id).await?; |
| 189 | 191 | |
| 190 | - let slots = crate::service::weekly_slot::resolved_for_instance(db, instance).await?; | |
| 192 | + let slots = crate::service::weekly_slot::resolved_for_instance(&pool, &instance).await?; | |
| 191 | 193 | if slots.is_empty() { |
| 192 | 194 | return Err(AppError::ConflictCoded { |
| 193 | 195 | code: "instance_has_no_weekly_plan", |
| @@ -195,7 +197,7 @@ pub async fn materialize( | ||
| 195 | 197 | }); |
| 196 | 198 | } |
| 197 | 199 | let teachers = |
| 198 | - crate::db::class_course_teacher::list_for_instance(db, &instance_id).await?; | |
| 200 | + crate::db::class_course_teacher::list_for_instance(&pool, &instance_id).await?; | |
| 199 | 201 | let Some(teacher) = teachers.first().copied() else { |
| 200 | 202 | return Err(AppError::ConflictCoded { |
| 201 | 203 | code: "instance_has_no_teacher", |
| @@ -207,7 +209,8 @@ pub async fn materialize( | ||
| 207 | 209 | // title second (the one display chain every surface rides), the |
| 208 | 210 | // literal `"Ders"` last. The resolved title is bounded by |
| 209 | 211 | // `MAX_COURSE_TITLE_LEN`, so it always fits a `SessionTopic`. |
| 210 | - let resolved = crate::service::instance_resolve::resolved_content(db, &[instance]).await?; | |
| 212 | + let resolved = | |
| 213 | + crate::service::instance_resolve::resolved_content(&pool, &[&instance]).await?; | |
| 211 | 214 | let fallback_topic = resolved |
| 212 | 215 | .get(&instance_id.key()) |
| 213 | 216 | .map(|content| content.title.as_str()) |
| @@ -215,8 +218,8 @@ pub async fn materialize( | ||
| 215 | 218 | .and_then(|title| SessionTopic::try_new(title).ok()) |
| 216 | 219 | .unwrap_or(SessionTopic::try_new("Ders")?); |
| 217 | 220 | |
| 218 | - let holidays = crate::service::holiday::blocked_days(db, from, to).await?; | |
| 219 | - let existing = course_session::existing_starts(db, &instance_id).await?; | |
| 221 | + let holidays = crate::service::holiday::blocked_days(&pool, from, to).await?; | |
| 222 | + let existing = course_session::existing_starts(&pool, &instance_id).await?; | |
| 220 | 223 | |
| 221 | 224 | let mut rows: Vec<NewSession> = Vec::new(); |
| 222 | 225 | let mut blocked: Vec<BlockedDay> = Vec::new(); |
src/web/courses.rsmodified+233 -41
| @@ -7,8 +7,14 @@ use utoipa::ToSchema; | ||
| 7 | 7 | use utoipa_axum::router::OpenApiRouter; |
| 8 | 8 | use utoipa_axum::routes; |
| 9 | 9 | |
| 10 | +use std::collections::{HashMap, HashSet}; | |
| 11 | + | |
| 12 | +use crate::constant::MAX_COURSE_SECTIONS; | |
| 10 | 13 | use crate::database::Database; |
| 14 | +use crate::domain::class_course::{ClassCourse, DersSaati}; | |
| 15 | +use crate::domain::class_group::{ClassGroup, ClassGroupId}; | |
| 11 | 16 | use crate::domain::course::{Course, CourseDescription, CourseId, CourseKind, CourseTitle}; |
| 17 | +use crate::domain::course_offering::CourseOfferingId; | |
| 12 | 18 | use crate::domain::role::Role; |
| 13 | 19 | use crate::domain::subject::{SubjectDescription, SubjectName}; |
| 14 | 20 | use crate::domain::user::{User, UserId}; |
| @@ -17,6 +23,7 @@ use crate::service; | ||
| 17 | 23 | use crate::service::course::{can_manage_course, can_view_course}; |
| 18 | 24 | use crate::state::AppState; |
| 19 | 25 | |
| 26 | +use super::dto::CourseSectionRef; | |
| 20 | 27 | use super::{ |
| 21 | 28 | CourseResponse, CurrentUser, Page, PageParams, PersonRef, RequireTeacher, SubjectResponse, |
| 22 | 29 | course_people, paginate, person_map, remove_blob, |
| @@ -150,6 +157,198 @@ pub(crate) async fn visible_courses(user: &User, db: &Database) -> Result<Vec<Co | ||
| 150 | 157 | Ok(courses) |
| 151 | 158 | } |
| 152 | 159 | |
| 160 | +/// The class sections teaching each of `courses`, keyed by course id — the | |
| 161 | +/// builder every catalogue row and every `/courses/me` row rides. | |
| 162 | +/// | |
| 163 | +/// Batched end to end: one query per resource for the *whole* page, never one | |
| 164 | +/// per row and never one per section — the instance read | |
| 165 | +/// ([`crate::db::class_course::list_for_course_ids`]), the teacher links | |
| 166 | +/// ([`crate::db::class_course_teacher::into_instances`]), the classes | |
| 167 | +/// ([`crate::db::class_group::list_by_ids`]), the offerings behind the resolved | |
| 168 | +/// hours ([`crate::db::course_offering::list_by_ids`]), the resolved titles | |
| 169 | +/// ([`crate::service::instance_resolve::resolved_content`], itself two queries) | |
| 170 | +/// and the teachers' `PersonRef`s ([`person_map`], one query). | |
| 171 | +/// | |
| 172 | +/// `viewer` is the narrowing rule: `Some(user)` for a non-manager keeps only | |
| 173 | +/// the sections that caller already reaches — [`reached_instances`], the same | |
| 174 | +/// set `GET /instances/me` serves plus their own enrollments — so a section | |
| 175 | +/// they cannot reach, and its roster size, teacher and class, never leak here. | |
| 176 | +/// `None` (manager+) sees every section. Sections per course are truncated to | |
| 177 | +/// [`MAX_COURSE_SECTIONS`]; the course row's own `class_course_count` carries | |
| 178 | +/// the untruncated total. | |
| 179 | +pub(crate) async fn sections_of( | |
| 180 | + db: &Database, | |
| 181 | + courses: &[Course], | |
| 182 | + viewer: Option<&User>, | |
| 183 | +) -> Result<HashMap<String, Vec<CourseSectionRef>>, AppError> { | |
| 184 | + let mut by_course: HashMap<String, Vec<CourseSectionRef>> = HashMap::new(); | |
| 185 | + if courses.is_empty() { | |
| 186 | + return Ok(by_course); | |
| 187 | + } | |
| 188 | + let ids: Vec<CourseId> = courses.iter().map(|course| course.get_id().clone()).collect(); | |
| 189 | + let mut instances = crate::db::class_course::list_for_course_ids(db, &ids).await?; | |
| 190 | + if let Some(viewer) = viewer { | |
| 191 | + let reachable: HashSet<String> = reached_instances(db, viewer) | |
| 192 | + .await? | |
| 193 | + .into_iter() | |
| 194 | + .map(|instance| instance.get_id().key()) | |
| 195 | + .collect(); | |
| 196 | + instances.retain(|instance| reachable.contains(&instance.get_id().key())); | |
| 197 | + } | |
| 198 | + let with_teachers = crate::db::class_course_teacher::into_instances(db, instances).await?; | |
| 199 | + for section in section_refs(db, with_teachers).await? { | |
| 200 | + by_course | |
| 201 | + .entry(section.course.clone()) | |
| 202 | + .or_default() | |
| 203 | + .push(section); | |
| 204 | + } | |
| 205 | + for sections in by_course.values_mut() { | |
| 206 | + sections.truncate(MAX_COURSE_SECTIONS); | |
| 207 | + } | |
| 208 | + Ok(by_course) | |
| 209 | +} | |
| 210 | + | |
| 211 | +/// The class sections the caller is reached by, one flat list — the shape | |
| 212 | +/// `/courses/me` serves (it lists sections, not catalog rows, so there is no | |
| 213 | +/// grouping to do). Rides [`reached_instances`] (class roster, homeroom, | |
| 214 | +/// taught, or their own enrollment) and [`section_refs`] like [`sections_of`] | |
| 215 | +/// does, so the surfaces cannot disagree about a section's resolved title or | |
| 216 | +/// hours. | |
| 217 | +pub(crate) async fn reached_sections( | |
| 218 | + db: &Database, | |
| 219 | + user: &User, | |
| 220 | +) -> Result<Vec<CourseSectionRef>, AppError> { | |
| 221 | + let instances = reached_instances(db, user).await?; | |
| 222 | + let with_teachers = crate::db::class_course_teacher::into_instances(db, instances).await?; | |
| 223 | + section_refs(db, with_teachers).await | |
| 224 | +} | |
| 225 | + | |
| 226 | +/// The instances `user` reaches, as rows: the class-roster / homeroom / taught | |
| 227 | +/// set ([`crate::service::instance::visible_instances`]) unioned with the | |
| 228 | +/// instances their own **enrollment** rows place them on, de-duplicated by | |
| 229 | +/// instance id and kept newest-first. | |
| 230 | +/// | |
| 231 | +/// The second half is not redundant. An enrollment row is the *instance* | |
| 232 | +/// roster, and a hand-placed one carries no class membership at all (a class | |
| 233 | +/// row an operator later disowned is the same shape), so the class-keyed | |
| 234 | +/// visibility set never sees it — yet that section is theirs, and both | |
| 235 | +/// `/courses/me` and the profile course block listed it before the section | |
| 236 | +/// split. Two reads for the union (the enrollment list plus one batched | |
| 237 | +/// instance read of whatever it added), never one per row. | |
| 238 | +pub(crate) async fn reached_instances( | |
| 239 | + db: &Database, | |
| 240 | + user: &User, | |
| 241 | +) -> Result<Vec<ClassCourse>, AppError> { | |
| 242 | + let mut instances: Vec<ClassCourse> = crate::service::instance::visible_instances(user, db) | |
| 243 | + .await? | |
| 244 | + .into_iter() | |
| 245 | + .map(|(instance, _)| instance) | |
| 246 | + .collect(); | |
| 247 | + let known: HashSet<String> = instances | |
| 248 | + .iter() | |
| 249 | + .map(|instance| instance.get_id().key()) | |
| 250 | + .collect(); | |
| 251 | + let missing: Vec<crate::domain::class_course::ClassCourseId> = | |
| 252 | + crate::db::enrollment::list_for_user(db, user.get_id()) | |
| 253 | + .await? | |
| 254 | + .iter() | |
| 255 | + .map(|enrollment| enrollment.get_class_course().clone()) | |
| 256 | + .filter(|id| !known.contains(&id.key())) | |
| 257 | + .collect(); | |
| 258 | + // `list_by_ids` orders `attached_at DESC, id DESC` — the same order the | |
| 259 | + // class arm of `visible_instances` produces, so the union reads newest-first | |
| 260 | + // end to end. | |
| 261 | + instances.extend(crate::db::class_course::list_by_ids(db, &missing).await?); | |
| 262 | + Ok(instances) | |
| 263 | +} | |
| 264 | + | |
| 265 | +/// Build one [`CourseSectionRef`] per `(instance, teachers)` pair — the shared | |
| 266 | +/// tail of [`sections_of`] and [`reached_sections`]: the classes, offerings, | |
| 267 | +/// resolved titles and people are read once for the whole batch, then each | |
| 268 | +/// section is assembled in memory. | |
| 269 | +pub(crate) async fn section_refs( | |
| 270 | + db: &Database, | |
| 271 | + rows: Vec<(ClassCourse, Vec<UserId>)>, | |
| 272 | +) -> Result<Vec<CourseSectionRef>, AppError> { | |
| 273 | + if rows.is_empty() { | |
| 274 | + return Ok(Vec::new()); | |
| 275 | + } | |
| 276 | + let mut class_ids: Vec<ClassGroupId> = Vec::new(); | |
| 277 | + let mut offering_ids: Vec<CourseOfferingId> = Vec::new(); | |
| 278 | + for (instance, _) in &rows { | |
| 279 | + if !class_ids.contains(instance.get_class()) { | |
| 280 | + class_ids.push(instance.get_class().clone()); | |
| 281 | + } | |
| 282 | + if !offering_ids.contains(instance.get_offering()) { | |
| 283 | + offering_ids.push(instance.get_offering().clone()); | |
| 284 | + } | |
| 285 | + } | |
| 286 | + let classes = crate::db::class_group::list_by_ids(db, &class_ids).await?; | |
| 287 | + let class_by_key: HashMap<String, &ClassGroup> = classes | |
| 288 | + .iter() | |
| 289 | + .map(|class| (class.get_id().key(), class)) | |
| 290 | + .collect(); | |
| 291 | + // The resolved weekly hours chain is `resolve_policy`'s — instance | |
| 292 | + // override, else the offering's default, else the floor — but its offering | |
| 293 | + // half is read once for the batch instead of once per section. | |
| 294 | + let offerings = crate::db::course_offering::list_by_ids(db, &offering_ids).await?; | |
| 295 | + let hours_by_offering: HashMap<String, Option<DersSaati>> = offerings | |
| 296 | + .iter() | |
| 297 | + .map(|offering| { | |
| 298 | + ( | |
| 299 | + offering.get_id().key(), | |
| 300 | + offering.get_default_ders_saati(), | |
| 301 | + ) | |
| 302 | + }) | |
| 303 | + .collect(); | |
| 304 | + let refs: Vec<&ClassCourse> = | |
| 305 | + rows.iter().map(|(instance, _)| instance).collect(); | |
| 306 | + let content = crate::service::instance_resolve::resolved_content(db, &refs).await?; | |
| 307 | + let people = person_map( | |
| 308 | + rows.iter() | |
| 309 | + .flat_map(|(_, teachers)| teachers.iter().cloned()), | |
| 310 | + db, | |
| 311 | + ) | |
| 312 | + .await?; | |
| 313 | + let mut sections = Vec::with_capacity(rows.len()); | |
| 314 | + for (instance, teachers) in &rows { | |
| 315 | + let key = instance.get_id().key(); | |
| 316 | + let resolved = content | |
| 317 | + .get(&key) | |
| 318 | + .expect("resolved_content covers every instance it is given"); | |
| 319 | + let class = class_by_key | |
| 320 | + .get(&instance.get_class().key()) | |
| 321 | + .expect("every section's class is in the batch read"); | |
| 322 | + let ders_saati = instance | |
| 323 | + .get_ders_saati() | |
| 324 | + .or_else(|| { | |
| 325 | + hours_by_offering | |
| 326 | + .get(&instance.get_offering().key()) | |
| 327 | + .copied() | |
| 328 | + .flatten() | |
| 329 | + }) | |
| 330 | + .unwrap_or_else(|| { | |
| 331 | + DersSaati::try_new(crate::constant::MIN_DERS_SAATI) | |
| 332 | + .expect("the floor is a valid weekly-hours count") | |
| 333 | + }); | |
| 334 | + sections.push(CourseSectionRef { | |
| 335 | + id: key, | |
| 336 | + course: instance.get_course().key(), | |
| 337 | + class: instance.get_class().key(), | |
| 338 | + class_name: class.get_name().as_str().to_string(), | |
| 339 | + title: resolved.title.clone(), | |
| 340 | + grade_level: class.get_grade_level().get(), | |
| 341 | + ders_saati: ders_saati.as_i64(), | |
| 342 | + teachers: teachers | |
| 343 | + .iter() | |
| 344 | + .map(|teacher| PersonRef::resolve(&people, teacher)) | |
| 345 | + .collect(), | |
| 346 | + enrollment_count: instance.get_enrollment_count(), | |
| 347 | + }); | |
| 348 | + } | |
| 349 | + Ok(sections) | |
| 350 | +} | |
| 351 | + | |
| 153 | 352 | // ---- courses ------------------------------------------------------------ |
| 154 | 353 | |
| 155 | 354 | /// Create a catalog course owned by the current user. Requires the `teacher` |
| @@ -220,14 +419,23 @@ async fn list_courses( | ||
| 220 | 419 | let window = paginate(&courses, limit, offset); |
| 221 | 420 | // Join creators onto the page alone — the lookup shrinks with the window. |
| 222 | 421 | let people = person_map(window.iter().flat_map(course_people), &st.db).await?; |
| 422 | + // The sections of every row on the page, batched: one round of queries for | |
| 423 | + // the whole page, never one per row. A non-manager sees only the sections | |
| 424 | + // they already reach (their şubeler, what they teach, their enrollments). | |
| 425 | + let viewer = (!user.get_role().at_least(Role::Manager)).then_some(&user); | |
| 426 | + let mut sections = sections_of(&st.db, window, viewer).await?; | |
| 223 | 427 | // Deliberate exception to the instance-resolved display rule: this |
| 224 | - // surface IS the ders catalog, so it shows each row's own catalog | |
| 225 | - // title/description. The grade templates (`/offerings`) and the | |
| 226 | - // per-class instances (`/instances`) resolve from it — they never edit | |
| 227 | - // it. | |
| 428 | + // surface IS the ders catalog, so each *row* shows its own catalog | |
| 429 | + // title/description — but every row now carries the class sections | |
| 430 | + // teaching it, each with the section's own resolved title and hours (the | |
| 431 | + // grade templates (`/offerings`) and the per-class instances | |
| 432 | + // (`/instances`) resolve from it; they never edit it). | |
| 228 | 433 | let items = window |
| 229 | 434 | .iter() |
| 230 | - .map(|course| CourseResponse::new(course, &people)) | |
| 435 | + .map(|course| { | |
| 436 | + let own = sections.remove(&course.get_id().key()).unwrap_or_default(); | |
| 437 | + CourseResponse::with_sections(course, &people, own) | |
| 438 | + }) | |
| 231 | 439 | .collect(); |
| 232 | 440 | Ok(Json(Page::new(items, total, limit, offset))) |
| 233 | 441 | } |
| @@ -236,9 +444,12 @@ async fn list_courses( | ||
| 236 | 444 | /// `?limit=&offset=` (omit `limit` for all of them); returns a |
| 237 | 445 | /// `{items, total, limit, offset}` envelope. |
| 238 | 446 | /// |
| 239 | -/// Both membership tiers are here: a student's enrollments in the instances | |
| 240 | -/// their class sections teach, and an individual club/study membership. The | |
| 241 | -/// *instances* themselves are `GET /instances/me`. | |
| 447 | +/// One row per **class section** the caller is reached by — the resolved | |
| 448 | +/// title, hours, class and teachers of that section, with the *instance* id. | |
| 449 | +/// The set is `GET /instances/me`'s (class roster, homeroom, taught) unioned | |
| 450 | +/// with the caller's own enrollment rows, so a section is listed exactly when | |
| 451 | +/// they reach it at all: by their şube, by running it, or by being placed on | |
| 452 | +/// its roster by hand. | |
| 242 | 453 | #[utoipa::path( |
| 243 | 454 | get, |
| 244 | 455 | path = "/me", |
| @@ -246,7 +457,7 @@ async fn list_courses( | ||
| 246 | 457 | security(("session_cookie" = [])), |
| 247 | 458 | params(PageParams), |
| 248 | 459 | responses( |
| 249 | - (status = 200, description = "A page of the caller's courses (all of them when unpaged)", body = Page<CourseResponse>), | |
| 460 | + (status = 200, description = "A page of the caller's sections (all of them when unpaged)", body = Page<CourseSectionRef>), | |
| 250 | 461 | (status = 400, description = "Invalid limit or offset", body = ErrorResponse), |
| 251 | 462 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 252 | 463 | ), |
| @@ -255,38 +466,14 @@ async fn my_courses( | ||
| 255 | 466 | State(st): State<AppState>, |
| 256 | 467 | CurrentUser(user): CurrentUser, |
| 257 | 468 | Query(page): Query<PageParams>, |
| 258 | -) -> Result<Json<Page<CourseResponse>>, AppError> { | |
| 469 | +) -> Result<Json<Page<CourseSectionRef>>, AppError> { | |
| 259 | 470 | let (limit, offset) = page.resolve()?; |
| 260 | - // Two sources, one list: the courses reached through the instances the | |
| 261 | - // caller is enrolled in, and the ones they joined individually. Either | |
| 262 | - // read is unpaged — the union is a Rust list, so it is paged here (the | |
| 263 | - // `visible_courses` shape). | |
| 264 | - let mut courses = service::course::list_enrolled(&st.db, user.get_id(), None, 0) | |
| 265 | - .await? | |
| 266 | - .0; | |
| 267 | - let (joined, _) = | |
| 268 | - crate::db::course_membership::list_for_user(&st.db, user.get_id(), None, 0).await?; | |
| 269 | - let known: Vec<CourseId> = courses | |
| 270 | - .iter() | |
| 271 | - .map(|course| course.get_id().clone()) | |
| 272 | - .collect(); | |
| 273 | - let mut missing: Vec<CourseId> = joined | |
| 274 | - .iter() | |
| 275 | - .map(|membership| membership.get_course().clone()) | |
| 276 | - .filter(|id| !known.contains(id)) | |
| 277 | - .collect(); | |
| 278 | - missing.sort_by_key(|id| std::cmp::Reverse(id.key())); | |
| 279 | - missing.dedup(); | |
| 280 | - courses.extend(service::course::list_by_ids(&st.db, &missing).await?); | |
| 281 | - courses.sort_by_key(|course| std::cmp::Reverse(course.get_id().key())); | |
| 282 | - let total = courses.len() as i64; | |
| 283 | - let window = paginate(&courses, limit, offset); | |
| 284 | - let people = person_map(window.iter().flat_map(course_people), &st.db).await?; | |
| 285 | - let items = window | |
| 286 | - .iter() | |
| 287 | - .map(|course| CourseResponse::new(course, &people)) | |
| 288 | - .collect(); | |
| 289 | - Ok(Json(Page::new(items, total, limit, offset))) | |
| 471 | + // The section set is a live Rust list (the visibility union), so it is | |
| 472 | + // paged here — the `visible_courses` shape. | |
| 473 | + let sections = reached_sections(&st.db, &user).await?; | |
| 474 | + let total = sections.len() as i64; | |
| 475 | + let window = paginate(§ions, limit, offset); | |
| 476 | + Ok(Json(Page::new(window.to_vec(), total, limit, offset))) | |
| 290 | 477 | } |
| 291 | 478 | |
| 292 | 479 | /// Fetch a single catalog course by id. Visible to the people it reaches — |
| @@ -320,7 +507,12 @@ async fn get_course( | ||
| 320 | 507 | )); |
| 321 | 508 | } |
| 322 | 509 | let people = person_map(course_people(&course), &st.db).await?; |
| 323 | - Ok(Json(CourseResponse::new(&course, &people))) | |
| 510 | + let viewer = (!user.get_role().at_least(Role::Manager)).then_some(&user); | |
| 511 | + let mut sections = sections_of(&st.db, std::slice::from_ref(&course), viewer).await?; | |
| 512 | + let own = sections | |
| 513 | + .remove(&course.get_id().key()) | |
| 514 | + .unwrap_or_default(); | |
| 515 | + Ok(Json(CourseResponse::with_sections(&course, &people, own))) | |
| 324 | 516 | } |
| 325 | 517 | |
| 326 | 518 | /// Update a catalog course. Requires teacher+ and catalog rights — its |
src/web/dto.rsmodified+60 -0
| @@ -276,6 +276,51 @@ pub struct CourseResponse { | ||
| 276 | 276 | /// How many individual club/study memberships the course carries. |
| 277 | 277 | #[schema(example = 12)] |
| 278 | 278 | pub course_membership_count: i64, |
| 279 | + /// The class sections teaching this course right now — one entry per | |
| 280 | + /// instance, each with its own resolved title and hours. Populated by the | |
| 281 | + /// catalogue reads (`GET /courses`, `GET /courses/{id}`); `[]` everywhere | |
| 282 | + /// else, including the CRUD responses and every report that embeds a | |
| 283 | + /// course row. Truncated to `MAX_COURSE_SECTIONS`; `class_course_count` is | |
| 284 | + /// the untruncated total. | |
| 285 | + pub sections: Vec<CourseSectionRef>, | |
| 286 | +} | |
| 287 | + | |
| 288 | +/// One class section teaching a catalog course. Shared by the catalogue rows | |
| 289 | +/// and the profile course block; `id` is the **instance** id | |
| 290 | +/// (`GET /instances/{id}`), never the catalog row's — the 9-A and 12-A | |
| 291 | +/// sections of one ders are two entries here, with their own resolved titles | |
| 292 | +/// and weekly hours. | |
| 293 | +#[derive(Serialize, Clone, ToSchema)] | |
| 294 | +pub struct CourseSectionRef { | |
| 295 | + /// The instance id (`GET /instances/{id}`). | |
| 296 | + #[schema(example = "019732e3-7b00-7000-8000-00000000dead")] | |
| 297 | + pub id: String, | |
| 298 | + /// The catalog course this section teaches (`GET /courses/{id}`). | |
| 299 | + #[schema(example = "019732e3-7b00-7000-8000-00000000beef")] | |
| 300 | + pub course: String, | |
| 301 | + /// The class this section belongs to (`GET /classes/{id}`). | |
| 302 | + #[schema(example = "019732e3-7b00-7000-8000-00000000cafe")] | |
| 303 | + pub class: String, | |
| 304 | + /// The class's own name, e.g. `9-A`. | |
| 305 | + #[schema(example = "9-A")] | |
| 306 | + pub class_name: String, | |
| 307 | + /// The **resolved** display title of this section: its own override, else | |
| 308 | + /// its offering's, else the catalog course's — the same chain every | |
| 309 | + /// instance-scoped surface shows. | |
| 310 | + #[schema(example = "Matematik")] | |
| 311 | + pub title: String, | |
| 312 | + /// The class's rung on the grade ladder (`0` = anaokulu). | |
| 313 | + #[schema(example = 9)] | |
| 314 | + pub grade_level: i16, | |
| 315 | + /// The **resolved** weekly lesson hours of this section: its own override, | |
| 316 | + /// else its offering's default, else the floor. | |
| 317 | + #[schema(example = 5)] | |
| 318 | + pub ders_saati: i64, | |
| 319 | + /// The staff assigned to run this section. | |
| 320 | + pub teachers: Vec<PersonRef>, | |
| 321 | + /// How many students are enrolled in this section right now. | |
| 322 | + #[schema(example = 28)] | |
| 323 | + pub enrollment_count: i64, | |
| 279 | 324 | } |
| 280 | 325 | |
| 281 | 326 | /// Every person a [`CourseResponse`] names: its creator. Feed this into |
| @@ -295,6 +340,21 @@ impl CourseResponse { | ||
| 295 | 340 | kind: course.get_kind().as_str().to_string(), |
| 296 | 341 | class_course_count: course.get_class_course_count(), |
| 297 | 342 | course_membership_count: course.get_course_membership_count(), |
| 343 | + sections: Vec::new(), | |
| 344 | + } | |
| 345 | + } | |
| 346 | + | |
| 347 | + /// The same row plus its sections — the catalogue reads' constructor | |
| 348 | + /// (`GET /courses`, `GET /courses/{id}`), where every row carries the | |
| 349 | + /// class sections teaching it. | |
| 350 | + pub fn with_sections( | |
| 351 | + course: &Course, | |
| 352 | + people: &HashMap<String, PersonRef>, | |
| 353 | + sections: Vec<CourseSectionRef>, | |
| 354 | + ) -> Self { | |
| 355 | + Self { | |
| 356 | + sections, | |
| 357 | + ..Self::new(course, people) | |
| 298 | 358 | } |
| 299 | 359 | } |
| 300 | 360 | } |
src/web/users.rsmodified+68 -35
| @@ -964,10 +964,12 @@ struct ProfileResponse { | ||
| 964 | 964 | /// At most `max_profile_classes` sections — the full list is at |
| 965 | 965 | /// `GET /classes/me`. |
| 966 | 966 | classes: Vec<ProfileClassRef>, |
| 967 | - /// At most `max_profile_courses` courses, and only the ones the *reader* | |
| 968 | - /// may already read at `GET /courses/{id}` — a stranger sees an empty | |
| 969 | - /// block, the owner and manager+ see it whole. The full list is at | |
| 970 | - /// `GET /courses/me`. | |
| 967 | + /// At most `max_profile_courses` **sections** the person is reached by — | |
| 968 | + /// `GET /instances/me`'s set unioned with their own enrollment rows — each | |
| 969 | + /// with its own resolved title, and only the ones the *reader* may already | |
| 970 | + /// read the catalog course of at `GET /courses/{id}`, so a stranger sees an | |
| 971 | + /// empty block while the owner and manager+ see it whole. The full list is | |
| 972 | + /// at `GET /courses/me`. | |
| 971 | 973 | courses: Vec<ProfileCourseRef>, |
| 972 | 974 | /// Every badge this account has earned, oldest first. Ids and stamps only — |
| 973 | 975 | /// the label and the icon are the client's, keyed by id off the `badges` |
| @@ -1013,13 +1015,32 @@ struct ProfileClassRef { | ||
| 1013 | 1015 | grade_level: i16, |
| 1014 | 1016 | } |
| 1015 | 1017 | |
| 1016 | -/// A course as a profile shows it — a label, nothing more. | |
| 1018 | +/// A course as a profile shows it — one row per **class section** the person | |
| 1019 | +/// is reached by, never the catalog row: the 9-A and 12-A sections of one ders | |
| 1020 | +/// carry their own resolved titles and hours, and the profile lists both. `id` | |
| 1021 | +/// is the instance id (`GET /instances/{id}`); `kind` is the catalog row's, | |
| 1022 | +/// which is the same for every section of one course. | |
| 1017 | 1023 | #[derive(Serialize, ToSchema)] |
| 1018 | 1024 | struct ProfileCourseRef { |
| 1025 | + /// The instance id (`GET /instances/{id}`). | |
| 1019 | 1026 | #[schema(example = "019732e3-7b00-7000-8000-00000000dead")] |
| 1020 | 1027 | id: String, |
| 1028 | + /// The catalog course this section teaches (`GET /courses/{id}`). | |
| 1029 | + #[schema(example = "019732e3-7b00-7000-8000-00000000beef")] | |
| 1030 | + course: String, | |
| 1031 | + /// The class this section belongs to (`GET /classes/{id}`). | |
| 1032 | + #[schema(example = "019732e3-7b00-7000-8000-00000000cafe")] | |
| 1033 | + class: String, | |
| 1034 | + /// The class's own name, e.g. `9-A`. | |
| 1035 | + #[schema(example = "9-A")] | |
| 1036 | + class_name: String, | |
| 1037 | + /// The **resolved** display title of this section: its own override, else | |
| 1038 | + /// its offering's, else the catalog course's. | |
| 1021 | 1039 | #[schema(example = "Matematik")] |
| 1022 | 1040 | title: String, |
| 1041 | + /// The class's rung on the grade ladder (`0` = anaokulu). | |
| 1042 | + #[schema(example = 9)] | |
| 1043 | + grade_level: i16, | |
| 1023 | 1044 | /// `course`, `study` (supervised study), or `club`. |
| 1024 | 1045 | #[schema(example = "course")] |
| 1025 | 1046 | kind: String, |
| @@ -1054,7 +1075,7 @@ struct ProfileStatsResponse { | ||
| 1054 | 1075 | pomodoro_sessions: i64, |
| 1055 | 1076 | /// Total focused milliseconds across those stints. |
| 1056 | 1077 | pomodoro_focus_ms: i64, |
| 1057 | - /// Every course behind the `courses` block, not just the embedded window. | |
| 1078 | + /// Every section behind the `courses` block, not just the embedded window. | |
| 1058 | 1079 | courses: i64, |
| 1059 | 1080 | /// Every class behind the `classes` block, not just the embedded window. |
| 1060 | 1081 | classes: i64, |
| @@ -1100,11 +1121,15 @@ struct ProfileStatsResponse { | ||
| 1100 | 1121 | |
| 1101 | 1122 | /// Build one profile as `viewer` may see it. The course block is chosen off the |
| 1102 | 1123 | /// owner's **live** role, not off the `creator`/`teachers` columns: those are |
| 1103 | -/// historical and no demotion sweeps them, so a demoted ex-teacher lists what | |
| 1104 | -/// they are enrolled in, like any other student. | |
| 1124 | +/// historical and no demotion sweeps them, so a demoted ex-teacher lists the | |
| 1125 | +/// sections they are reached by, like any other student. It lists one row per | |
| 1126 | +/// *section* and rides [`crate::web::courses::reached_instances`] — the class | |
| 1127 | +/// roster / homeroom / taught set `GET /instances/me` serves, unioned with the | |
| 1128 | +/// owner's own enrollment rows — so a section that is theirs to sit or run is | |
| 1129 | +/// the only thing that appears. | |
| 1105 | 1130 | /// |
| 1106 | -/// A course carries a title, and `GET /courses/{id}` hands that title only to | |
| 1107 | -/// the enrolled, the staff who run it, and manager+ — so the block is | |
| 1131 | +/// A section carries a resolved title, and `GET /courses/{id}` hands that title | |
| 1132 | +/// only to the enrolled, the staff who run it, and manager+ — so the block is | |
| 1108 | 1133 | /// intersected with [`visible_courses`], the same catalog `GET /courses` |
| 1109 | 1134 | /// serves. Reading your own profile, or reading as manager+, needs no |
| 1110 | 1135 | /// intersection: both already see the whole list. |
| @@ -1150,25 +1175,33 @@ async fn profile_of( | ||
| 1150 | 1175 | .collect::<Vec<_>>(), |
| 1151 | 1176 | ), |
| 1152 | 1177 | }; |
| 1178 | + // The class sections the person is reached by — the same set | |
| 1179 | + // `GET /instances/me` serves, each with its own resolved title and hours. | |
| 1180 | + // The block is one row per section, and `stats.courses` counts sections. | |
| 1181 | + let mut sections = super::courses::reached_sections(&st.db, user).await?; | |
| 1182 | + let course_total = sections.len() as i64; | |
| 1153 | 1183 | // The window is cut after the intersection, so a filtered viewer still gets |
| 1154 | - // up to `MAX_PROFILE_COURSES` courses they can actually see. | |
| 1155 | - let (mut courses, course_total) = match user.get_role().at_least(Role::Teacher) { | |
| 1156 | - // The teacher read is unpaged, so the total is what came back. | |
| 1157 | - true => { | |
| 1158 | - let courses = crate::service::course::list_for_teacher(&st.db, id).await?; | |
| 1159 | - let total = courses.len() as i64; | |
| 1160 | - (courses, total) | |
| 1161 | - } | |
| 1162 | - // Unfiltered readers can take the window from the database. | |
| 1163 | - false => { | |
| 1164 | - let window = readable.is_none().then_some(MAX_PROFILE_COURSES as i64); | |
| 1165 | - crate::service::course::list_enrolled(&st.db, id, window, 0).await? | |
| 1166 | - } | |
| 1167 | - }; | |
| 1184 | + // up to `MAX_PROFILE_COURSES` sections they can actually see. | |
| 1168 | 1185 | if let Some(readable) = &readable { |
| 1169 | - courses.retain(|course| readable.contains(course.get_id())); | |
| 1186 | + sections.retain(|section| readable.iter().any(|id| id.key() == section.course)); | |
| 1170 | 1187 | } |
| 1171 | - courses.truncate(MAX_PROFILE_COURSES); | |
| 1188 | + sections.truncate(MAX_PROFILE_COURSES); | |
| 1189 | + // The catalog row's `kind` — one read for the window, not one per row. | |
| 1190 | + let course_ids: Vec<crate::domain::course::CourseId> = sections | |
| 1191 | + .iter() | |
| 1192 | + .map(|section| crate::domain::course::CourseId::from_key(§ion.course)) | |
| 1193 | + .collect(); | |
| 1194 | + let kinds: std::collections::HashMap<String, String> = | |
| 1195 | + crate::service::course::list_by_ids(&st.db, &course_ids) | |
| 1196 | + .await? | |
| 1197 | + .iter() | |
| 1198 | + .map(|course| { | |
| 1199 | + ( | |
| 1200 | + course.get_id().key(), | |
| 1201 | + course.get_kind().as_str().to_string(), | |
| 1202 | + ) | |
| 1203 | + }) | |
| 1204 | + .collect(); | |
| 1172 | 1205 | let (mut members, class_total) = crate::service::class_member::list_for_user( |
| 1173 | 1206 | &st.db, |
| 1174 | 1207 | id, |
| @@ -1211,16 +1244,16 @@ async fn profile_of( | ||
| 1211 | 1244 | grade_level: class.get_grade_level().get(), |
| 1212 | 1245 | }) |
| 1213 | 1246 | .collect(), |
| 1214 | - courses: courses | |
| 1247 | + courses: sections | |
| 1215 | 1248 | .iter() |
| 1216 | - .map(|course| ProfileCourseRef { | |
| 1217 | - id: course.get_id().key().to_string(), | |
| 1218 | - // Catalog-tier surface, like `GET /courses`: a profile lists | |
| 1219 | - // the person's courses by their catalog identity (a person | |
| 1220 | - // may sit two sections of one ders), so the catalog title is | |
| 1221 | - // the deliberate exception to the instance-resolved display. | |
| 1222 | - title: course.get_title().as_str().to_string(), | |
| 1223 | - kind: course.get_kind().as_str().to_string(), | |
| 1249 | + .map(|section| ProfileCourseRef { | |
| 1250 | + id: section.id.clone(), | |
| 1251 | + course: section.course.clone(), | |
| 1252 | + class: section.class.clone(), | |
| 1253 | + class_name: section.class_name.clone(), | |
| 1254 | + title: section.title.clone(), | |
| 1255 | + grade_level: section.grade_level, | |
| 1256 | + kind: kinds.get(§ion.course).cloned().unwrap_or_default(), | |
| 1224 | 1257 | }) |
| 1225 | 1258 | .collect(), |
| 1226 | 1259 | badges: badges |