Skip to main content

headless_lms_server/controllers/course_material/
page_audio_files.rs

1use models::page_audio_files::PageAudioFile;
2use utoipa::OpenApi;
3
4use crate::prelude::*;
5
6#[derive(OpenApi)]
7#[openapi(paths(get_page_audio))]
8pub(crate) struct CourseMaterialPageAudioApiDoc;
9
10#[utoipa::path(
11    get,
12    path = "/{page_id}/files",
13    operation_id = "getCourseMaterialPageAudioFiles",
14    tag = "course-material-page-audio",
15    params(
16        ("page_id" = Uuid, Path, description = "Page id")
17    ),
18    responses(
19        (status = 200, description = "Page audio files", body = Vec<PageAudioFile>)
20    )
21)]
22async fn get_page_audio(
23    page_id: web::Path<Uuid>,
24    pool: web::Data<PgPool>,
25    user: Option<AuthUser>,
26    app_conf: web::Data<ApplicationConfiguration>,
27) -> ControllerResult<web::Json<Vec<PageAudioFile>>> {
28    let mut conn = pool.acquire().await?;
29    let page = models::pages::get_page(&mut conn, *page_id).await?;
30    let user_id = user.map(|u| u.id);
31    let token = authorize_access_to_course_material(
32        &mut conn,
33        user_id,
34        page.course_id.ok_or_else(|| {
35            ControllerError::new(
36                ControllerErrorType::NotFound,
37                "Course not found".to_string(),
38                None,
39            )
40        })?,
41    )
42    .await?;
43
44    let mut page_audio_files =
45        models::page_audio_files::get_page_audio_files(&mut conn, *page_id).await?;
46
47    let base_url = &app_conf.base_url;
48    for audio in page_audio_files.iter_mut() {
49        audio.path = format!("{base_url}/api/v0/files/{}", audio.path);
50    }
51
52    token.authorized_ok(web::Json(page_audio_files))
53}
54
55/**
56Add a route for each controller in this module.
57
58The name starts with an underline in order to appear before other functions in the module documentation.
59
60We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
61*/
62pub fn _add_routes(cfg: &mut ServiceConfig) {
63    cfg.route("/{page_id}/files", web::get().to(get_page_audio));
64}