Embed the creator as a person ref
Browse files at this commitc996ce4fb6cc274c3c0f7bb65d1dacb55a39cf23Parent: f658a4e- feat
- embed the creator as a person ref
5 changed files +37 -16
- src/web/attendance.rs +3 -2
- src/web/courses.rs +19 -7
- src/web/dto.rs +4 -3
- src/web/marks.rs +3 -2
- tests/integration.rs +8 -2
src/web/attendance.rsmodified+3 -2
| @@ -17,7 +17,7 @@ use crate::error::{AppError, ErrorResponse}; | |||
| 17 | use crate::state::AppState; | 17 | use crate::state::AppState; |
| 18 | 18 | ||
| 19 | use super::courses::can_manage_course; | 19 | use super::courses::can_manage_course; |
| 20 | -use super::{CourseResponse, CurrentUser, RequireTeacher}; | 20 | +use super::{CourseResponse, CurrentUser, RequireTeacher, person_map}; |
| 21 | 21 | ||
| 22 | pub fn routes() -> OpenApiRouter<AppState> { | 22 | pub fn routes() -> OpenApiRouter<AppState> { |
| 23 | OpenApiRouter::new() | 23 | OpenApiRouter::new() |
| @@ -119,6 +119,7 @@ async fn build_report( | |||
| 119 | let courses = Course::list_by_ids(&course_ids, db).await?; | 119 | let courses = Course::list_by_ids(&course_ids, db).await?; |
| 120 | let course_by_key: HashMap<&str, &Course> = | 120 | let course_by_key: HashMap<&str, &Course> = |
| 121 | courses.iter().map(|c| (c.get_id().key(), c)).collect(); | 121 | courses.iter().map(|c| (c.get_id().key(), c)).collect(); |
| 122 | + let people = person_map(courses.iter().map(|c| c.get_creator().clone()), db).await?; | ||
| 122 | 123 | ||
| 123 | let mut blocks = Vec::with_capacity(course_ids.len()); | 124 | let mut blocks = Vec::with_capacity(course_ids.len()); |
| 124 | let mut visible_rows: Vec<&SessionAttendance> = Vec::with_capacity(sessions.len()); | 125 | let mut visible_rows: Vec<&SessionAttendance> = Vec::with_capacity(sessions.len()); |
| @@ -134,7 +135,7 @@ async fn build_report( | |||
| 134 | let rows = &by_course[course_id.key()]; | 135 | let rows = &by_course[course_id.key()]; |
| 135 | visible_rows.extend(rows.iter().copied()); | 136 | visible_rows.extend(rows.iter().copied()); |
| 136 | blocks.push(CourseAttendance { | 137 | blocks.push(CourseAttendance { |
| 137 | - course: CourseResponse::new(course), | 138 | + course: CourseResponse::new(course, &people), |
| 138 | counts: StatusCounts::tally(rows.iter().map(|r| r.get_status())), | 139 | counts: StatusCounts::tally(rows.iter().map(|r| r.get_status())), |
| 139 | }); | 140 | }); |
| 140 | } | 141 | } |
src/web/courses.rsmodified+19 -7
| @@ -249,7 +249,12 @@ async fn create_course( | |||
| 249 | &st.db, | 249 | &st.db, |
| 250 | ) | 250 | ) |
| 251 | .await?; | 251 | .await?; |
| 252 | - Ok((StatusCode::CREATED, Json(CourseResponse::new(&course)))) | 252 | + // The creator is the caller — already loaded, no extra lookup. |
| 253 | + let people = PersonRef::map_of(&[&user]); | ||
| 254 | + Ok(( | ||
| 255 | + StatusCode::CREATED, | ||
| 256 | + Json(CourseResponse::new(&course, &people)), | ||
| 257 | + )) | ||
| 253 | } | 258 | } |
| 254 | 259 | ||
| 255 | /// List the courses visible to the caller: every course for manager+, | 260 | /// List the courses visible to the caller: every course for manager+, |
| @@ -276,9 +281,12 @@ async fn list_courses( | |||
| 276 | let (limit, offset) = page.resolve()?; | 281 | let (limit, offset) = page.resolve()?; |
| 277 | let courses = visible_courses(&user, &st.db).await?; | 282 | let courses = visible_courses(&user, &st.db).await?; |
| 278 | let total = courses.len() as i64; | 283 | let total = courses.len() as i64; |
| 279 | - let items = paginate(&courses, limit, offset) | 284 | + let window = paginate(&courses, limit, offset); |
| 285 | + // Join creators onto the page alone — the lookup shrinks with the window. | ||
| 286 | + let people = person_map(window.iter().map(|c| c.get_creator().clone()), &st.db).await?; | ||
| 287 | + let items = window | ||
| 280 | .iter() | 288 | .iter() |
| 281 | - .map(CourseResponse::new) | 289 | + .map(|course| CourseResponse::new(course, &people)) |
| 282 | .collect(); | 290 | .collect(); |
| 283 | Ok(Json(Page::new(items, total, limit, offset))) | 291 | Ok(Json(Page::new(items, total, limit, offset))) |
| 284 | } | 292 | } |
| @@ -306,9 +314,11 @@ async fn my_courses( | |||
| 306 | let (limit, offset) = page.resolve()?; | 314 | let (limit, offset) = page.resolve()?; |
| 307 | let courses = Course::list_enrolled(user.get_id(), &st.db).await?; | 315 | let courses = Course::list_enrolled(user.get_id(), &st.db).await?; |
| 308 | let total = courses.len() as i64; | 316 | let total = courses.len() as i64; |
| 309 | - let items = paginate(&courses, limit, offset) | 317 | + let window = paginate(&courses, limit, offset); |
| 318 | + let people = person_map(window.iter().map(|c| c.get_creator().clone()), &st.db).await?; | ||
| 319 | + let items = window | ||
| 310 | .iter() | 320 | .iter() |
| 311 | - .map(CourseResponse::new) | 321 | + .map(|course| CourseResponse::new(course, &people)) |
| 312 | .collect(); | 322 | .collect(); |
| 313 | Ok(Json(Page::new(items, total, limit, offset))) | 323 | Ok(Json(Page::new(items, total, limit, offset))) |
| 314 | } | 324 | } |
| @@ -341,7 +351,8 @@ async fn get_course( | |||
| 341 | "only enrolled users, the course creator, or a manager/admin can view this course", | 351 | "only enrolled users, the course creator, or a manager/admin can view this course", |
| 342 | )); | 352 | )); |
| 343 | } | 353 | } |
| 344 | - Ok(Json(CourseResponse::new(&course))) | 354 | + let people = person_map([course.get_creator().clone()], &st.db).await?; |
| 355 | + Ok(Json(CourseResponse::new(&course, &people))) | ||
| 345 | } | 356 | } |
| 346 | 357 | ||
| 347 | /// Update a course. Requires teacher+; the creator may edit their own course | 358 | /// Update a course. Requires teacher+; the creator may edit their own course |
| @@ -405,7 +416,8 @@ async fn update_course( | |||
| 405 | let updated = course | 416 | let updated = course |
| 406 | .update(title, description, kind, term, capacity, &st.db) | 417 | .update(title, description, kind, term, capacity, &st.db) |
| 407 | .await?; | 418 | .await?; |
| 408 | - Ok(Json(CourseResponse::new(&updated))) | 419 | + let people = person_map([updated.get_creator().clone()], &st.db).await?; |
| 420 | + Ok(Json(CourseResponse::new(&updated, &people))) | ||
| 409 | } | 421 | } |
| 410 | 422 | ||
| 411 | /// Delete a course. Requires teacher+; the creator may delete their own course | 423 | /// Delete a course. Requires teacher+; the creator may delete their own course |
src/web/dto.rsmodified+4 -3
| @@ -157,7 +157,8 @@ impl UserResponse { | |||
| 157 | pub struct CourseResponse { | 157 | pub struct CourseResponse { |
| 158 | #[schema(example = "01J8XZ0K3Q8G7X2M4N5P6R7S8T")] | 158 | #[schema(example = "01J8XZ0K3Q8G7X2M4N5P6R7S8T")] |
| 159 | pub id: String, | 159 | pub id: String, |
| 160 | - pub creator: String, | 160 | + /// Who created (and owns) the course. |
| 161 | + pub creator: PersonRef, | ||
| 161 | #[schema(example = "Algebra")] | 162 | #[schema(example = "Algebra")] |
| 162 | pub title: String, | 163 | pub title: String, |
| 163 | pub description: String, | 164 | pub description: String, |
| @@ -175,10 +176,10 @@ pub struct CourseResponse { | |||
| 175 | } | 176 | } |
| 176 | 177 | ||
| 177 | impl CourseResponse { | 178 | impl CourseResponse { |
| 178 | - pub fn new(course: &Course) -> Self { | 179 | + pub fn new(course: &Course, people: &HashMap<String, PersonRef>) -> Self { |
| 179 | Self { | 180 | Self { |
| 180 | id: course.get_id().key().to_string(), | 181 | id: course.get_id().key().to_string(), |
| 181 | - creator: course.get_creator().key().to_string(), | 182 | + creator: PersonRef::resolve(people, course.get_creator()), |
| 182 | title: course.get_title().as_str().to_string(), | 183 | title: course.get_title().as_str().to_string(), |
| 183 | description: course.get_description().as_str().to_string(), | 184 | description: course.get_description().as_str().to_string(), |
| 184 | kind: course.get_kind().as_str().to_string(), | 185 | kind: course.get_kind().as_str().to_string(), |
src/web/marks.rsmodified+3 -2
| @@ -18,7 +18,7 @@ use crate::error::{AppError, ErrorResponse}; | |||
| 18 | use crate::state::AppState; | 18 | use crate::state::AppState; |
| 19 | 19 | ||
| 20 | use super::courses::can_manage_course; | 20 | use super::courses::can_manage_course; |
| 21 | -use super::{CourseResponse, CurrentUser, RequireTeacher}; | 21 | +use super::{CourseResponse, CurrentUser, RequireTeacher, person_map}; |
| 22 | 22 | ||
| 23 | pub fn routes() -> OpenApiRouter<AppState> { | 23 | pub fn routes() -> OpenApiRouter<AppState> { |
| 24 | OpenApiRouter::new() | 24 | OpenApiRouter::new() |
| @@ -102,6 +102,7 @@ async fn build_report( | |||
| 102 | 102 | ||
| 103 | // One settings read weighs and labels the whole report. | 103 | // One settings read weighs and labels the whole report. |
| 104 | let school = Settings::load(db).await?; | 104 | let school = Settings::load(db).await?; |
| 105 | + let people = person_map(courses.iter().map(|c| c.get_creator().clone()), db).await?; | ||
| 105 | 106 | ||
| 106 | let mut blocks = Vec::with_capacity(courses.len()); | 107 | let mut blocks = Vec::with_capacity(courses.len()); |
| 107 | for course in &courses { | 108 | for course in &courses { |
| @@ -138,7 +139,7 @@ async fn build_report( | |||
| 138 | 139 | ||
| 139 | let average = weighted_average(&pairs); | 140 | let average = weighted_average(&pairs); |
| 140 | blocks.push(CourseMarks { | 141 | blocks.push(CourseMarks { |
| 141 | - course: CourseResponse::new(course), | 142 | + course: CourseResponse::new(course, &people), |
| 142 | average, | 143 | average, |
| 143 | average_grade: average.and_then(|a| school.grade_label(a).map(str::to_string)), | 144 | average_grade: average.and_then(|a| school.grade_label(a).map(str::to_string)), |
| 144 | results: entries, | 145 | results: entries, |
tests/integration.rsmodified+8 -2
| @@ -2073,6 +2073,9 @@ async fn courses_are_owner_scoped_and_creator_guarded() { | |||
| 2073 | .await; | 2073 | .await; |
| 2074 | assert_eq!(res.status, StatusCode::CREATED); | 2074 | assert_eq!(res.status, StatusCode::CREATED); |
| 2075 | assert_eq!(res.body["title"], "algebra"); | 2075 | assert_eq!(res.body["title"], "algebra"); |
| 2076 | + // The creator comes back as an embedded person ref, not a bare id — the | ||
| 2077 | + // catalog renders names without a second users request. | ||
| 2078 | + assert_eq!(res.body["creator"]["username"], "ali"); | ||
| 2076 | let course_id = id_of(&res.body); | 2079 | let course_id = id_of(&res.body); |
| 2077 | 2080 | ||
| 2078 | // Another teacher is not enrolled and doesn't manage it: no read, and the | 2081 | // Another teacher is not enrolled and doesn't manage it: no read, and the |
| @@ -2108,9 +2111,12 @@ async fn courses_are_owner_scoped_and_creator_guarded() { | |||
| 2108 | .status, | 2111 | .status, |
| 2109 | StatusCode::OK | 2112 | StatusCode::OK |
| 2110 | ); | 2113 | ); |
| 2114 | + let catalog = send(&app, "GET", "/courses", Some(caller), None).await.body; | ||
| 2115 | + let rows = common::items(&catalog); | ||
| 2116 | + assert_eq!(rows.len(), 1); | ||
| 2111 | assert_eq!( | 2117 | assert_eq!( |
| 2112 | - common::items(&send(&app, "GET", "/courses", Some(caller), None).await.body).len(), | 2118 | + rows[0]["creator"]["username"], "ali", |
| 2113 | - 1 | 2119 | + "the catalog embeds the creator ref on every row" |
| 2114 | ); | 2120 | ); |
| 2115 | } | 2121 | } |
| 2116 | 2122 | ||