headless_lms_server/controllers/cms/
chapters.rs

1//! Controllers for requests starting with `/api/v0/cms/chapters`.
2
3use models::chapters::DatabaseChapter;
4
5use crate::prelude::*;
6
7/**
8GET `/api/v0/cms/chapters/{course_id}/all-chapters-for-course` - Gets all chapters with a course_id
9*/
10#[instrument(skip(pool))]
11async fn get_all_chapters_by_course_id(
12    course_id: web::Path<Uuid>,
13    pool: web::Data<PgPool>,
14    user: AuthUser,
15) -> ControllerResult<web::Json<Vec<DatabaseChapter>>> {
16    let mut conn = pool.acquire().await?;
17    let token = authorize(&mut conn, Act::View, Some(user.id), Res::Course(*course_id)).await?;
18
19    let mut chapters = models::chapters::course_chapters(&mut conn, *course_id).await?;
20
21    chapters.sort_by(|a, b| a.chapter_number.cmp(&b.chapter_number));
22
23    token.authorized_ok(web::Json(chapters))
24}
25
26/**
27Add a route for each controller in this module.
28
29The name starts with an underline in order to appear before other functions in the module documentation.
30
31We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
32*/
33pub fn _add_routes(cfg: &mut ServiceConfig) {
34    cfg.route(
35        "/{course_id}/all-chapters-for-course",
36        web::get().to(get_all_chapters_by_course_id),
37    );
38}