headless_lms_server/controllers/course_material/
pages.rs

1//! Controllers for requests starting with `/api/v0/course-material/pages`.
2
3use crate::{domain::authorization::skip_authorize, prelude::*};
4use models::pages::{
5    IsChapterFrontPage, Page, PageChapterAndCourseInformation, PageNavigationInformation,
6};
7
8/**
9GET /api/v0/course-material/pages/exam/{page_id}
10*/
11#[instrument(skip(pool))]
12async fn get_by_exam_id(
13    exam_id: web::Path<Uuid>,
14    pool: web::Data<PgPool>,
15) -> ControllerResult<web::Json<Page>> {
16    let mut conn = pool.acquire().await?;
17    let page = models::pages::get_by_exam_id(&mut conn, *exam_id).await?;
18    let token = skip_authorize();
19    token.authorized_ok(web::Json(page))
20}
21
22/**
23GET /api/v0/course-material/page/{page_id}
24*/
25#[instrument(skip(pool))]
26async fn get_chapter_front_page(
27    page_id: web::Path<Uuid>,
28    pool: web::Data<PgPool>,
29) -> ControllerResult<web::Json<Option<Page>>> {
30    let mut conn = pool.acquire().await?;
31    let chapter_front_page =
32        models::pages::get_chapter_front_page_by_page_id(&mut conn, *page_id).await?;
33    let token = skip_authorize();
34    token.authorized_ok(web::Json(chapter_front_page))
35}
36
37/**
38GET /api/v0/course-material/pages/:page_id/page-navigation - tells what's the next page, previous page, and the chapter front page given a page id.
39*/
40#[instrument(skip(pool))]
41async fn get_page_navigation(
42    page_id: web::Path<Uuid>,
43    pool: web::Data<PgPool>,
44) -> ControllerResult<web::Json<PageNavigationInformation>> {
45    let mut conn = pool.acquire().await?;
46    let token = skip_authorize();
47    let res = models::pages::get_page_navigation_data(&mut conn, *page_id).await?;
48
49    token.authorized_ok(web::Json(res))
50}
51
52/**
53 GET /api/v0/course-material/pages/:page_id/chapter-and-course-information - gives the page's chapter and course information -- useful for the breadcrumbs
54*/
55#[instrument(skip(pool))]
56async fn get_chapter_and_course_information(
57    page_id: web::Path<Uuid>,
58    pool: web::Data<PgPool>,
59) -> ControllerResult<web::Json<PageChapterAndCourseInformation>> {
60    let mut conn = pool.acquire().await?;
61    let res = models::pages::get_page_chapter_and_course_information(&mut conn, *page_id).await?;
62
63    let token = skip_authorize();
64    token.authorized_ok(web::Json(res))
65}
66
67/**
68 GET /api/v0/course-material/pages/:page_id/url-path - returns the page's URL path.
69 # Example
70 ```json
71 "chapter-1/page-2"
72 ```
73*/
74#[instrument(skip(pool))]
75async fn get_url_path(
76    page_id: web::Path<Uuid>,
77    pool: web::Data<PgPool>,
78) -> ControllerResult<String> {
79    let mut conn = pool.acquire().await?;
80    let page = models::pages::get_page(&mut conn, *page_id).await?;
81
82    let token = skip_authorize();
83    token.authorized_ok(page.url_path)
84}
85
86#[instrument(skip(pool))]
87async fn is_chapter_front_page(
88    page_id: web::Path<Uuid>,
89    pool: web::Data<PgPool>,
90) -> ControllerResult<web::Json<IsChapterFrontPage>> {
91    let mut conn = pool.acquire().await?;
92    let is_chapter_front_page = models::pages::is_chapter_front_page(&mut conn, *page_id).await?;
93    let token = skip_authorize();
94    token.authorized_ok(web::Json(is_chapter_front_page))
95}
96
97pub fn _add_routes(cfg: &mut ServiceConfig) {
98    cfg.route("/exam/{page_id}", web::get().to(get_by_exam_id))
99        .route(
100            "/{current_page_id}/chapter-front-page",
101            web::get().to(get_chapter_front_page),
102        )
103        .route("/{current_page_id}/url-path", web::get().to(get_url_path))
104        .route(
105            "/{current_page_id}/chapter-and-course-information",
106            web::get().to(get_chapter_and_course_information),
107        )
108        .route(
109            "/{current_page_id}/is-chapter-front-page",
110            web::get().to(is_chapter_front_page),
111        )
112        .route(
113            "/{current_page_id}/page-navigation",
114            web::get().to(get_page_navigation),
115        );
116}