headless_lms_server/controllers/course_material/
chapters.rs

1//! Controllers for requests starting with `/api/v0/course_material/chapters`.
2
3use models::pages::{Page, PageVisibility, PageWithExercises};
4
5use crate::prelude::*;
6
7/**
8GET `/api/v0/course-material/chapters/:chapter_id/pages` - Returns a list of pages in chapter.
9*/
10#[instrument(skip(pool))]
11async fn get_public_chapter_pages(
12    chapter_id: web::Path<Uuid>,
13    pool: web::Data<PgPool>,
14    auth: Option<AuthUser>,
15) -> ControllerResult<web::Json<Vec<Page>>> {
16    let mut conn = pool.acquire().await?;
17    let token = authorize(
18        &mut conn,
19        Act::View,
20        auth.map(|u| u.id),
21        Res::Chapter(*chapter_id),
22    )
23    .await?;
24    let chapter_pages: Vec<Page> = models::pages::get_course_pages_by_chapter_id_and_visibility(
25        &mut conn,
26        *chapter_id,
27        PageVisibility::Public,
28    )
29    .await?;
30    token.authorized_ok(web::Json(chapter_pages))
31}
32
33/**
34GET `/api/v0/course-material/chapters/:chapter_id/exercises` - Returns a list of pages and its exercises in chapter.
35*/
36#[instrument(skip(pool))]
37async fn get_chapters_exercises(
38    chapter_id: web::Path<Uuid>,
39    pool: web::Data<PgPool>,
40    auth: Option<AuthUser>,
41) -> ControllerResult<web::Json<Vec<PageWithExercises>>> {
42    let mut conn = pool.acquire().await?;
43    let token = authorize(
44        &mut conn,
45        Act::View,
46        auth.map(|u| u.id),
47        Res::Chapter(*chapter_id),
48    )
49    .await?;
50
51    let chapter_pages_with_exercises =
52        models::pages::get_chapters_pages_with_exercises(&mut conn, *chapter_id).await?;
53    token.authorized_ok(web::Json(chapter_pages_with_exercises))
54}
55
56/**
57GET `/api/v0/course-material/chapters/:chapter_id/pages-exclude-mainfrontpage` - Returns a list of pages in chapter mainfrontpage excluded.
58*/
59#[instrument(skip(pool))]
60async fn get_chapters_pages_without_main_frontpage(
61    chapter_id: web::Path<Uuid>,
62    pool: web::Data<PgPool>,
63    auth: Option<AuthUser>,
64) -> ControllerResult<web::Json<Vec<Page>>> {
65    let mut conn = pool.acquire().await?;
66    let token = authorize(
67        &mut conn,
68        Act::View,
69        auth.map(|u| u.id),
70        Res::Chapter(*chapter_id),
71    )
72    .await?;
73    let chapter_pages =
74        models::pages::get_chapters_visible_pages_exclude_main_frontpage(&mut conn, *chapter_id)
75            .await?
76            .into_iter()
77            .collect();
78    token.authorized_ok(web::Json(chapter_pages))
79}
80
81/**
82Add a route for each controller in this module.
83
84The name starts with an underline in order to appear before other functions in the module documentation.
85
86We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
87*/
88pub fn _add_routes(cfg: &mut ServiceConfig) {
89    cfg.route(
90        "/{chapter_id}/pages",
91        web::get().to(get_public_chapter_pages),
92    )
93    .route(
94        "/{chapter_id}/exercises",
95        web::get().to(get_chapters_exercises),
96    )
97    .route(
98        "/{chapter_id}/pages-exclude-mainfrontpage",
99        web::get().to(get_chapters_pages_without_main_frontpage),
100    );
101}