headless_lms_server/controllers/cms/
repository_exercises.rs

1use crate::prelude::*;
2use models::repository_exercises::{self, RepositoryExercise};
3use utoipa::OpenApi;
4
5#[derive(OpenApi)]
6#[openapi(paths(get_for_course))]
7pub(crate) struct CmsRepositoryExercisesApiDoc;
8
9#[instrument(skip(pool))]
10#[utoipa::path(
11    get,
12    path = "/{course_id}",
13    operation_id = "getCmsRepositoryExercisesForCourse",
14    tag = "cms_repository_exercises",
15    params(
16        ("course_id" = Uuid, Path, description = "Course id")
17    ),
18    responses(
19        (status = 200, description = "Repository exercises for course", body = Vec<RepositoryExercise>)
20    )
21)]
22pub async fn get_for_course(
23    course_id: web::Path<Uuid>,
24    pool: web::Data<PgPool>,
25    user: Option<AuthUser>,
26) -> ControllerResult<web::Json<Vec<RepositoryExercise>>> {
27    let mut conn = pool.acquire().await?;
28    let token = authorize(
29        &mut conn,
30        Act::Teach,
31        user.map(|u| u.id),
32        Res::Course(*course_id),
33    )
34    .await?;
35
36    let exercises = repository_exercises::get_for_course(&mut conn, *course_id).await?;
37    token.authorized_ok(web::Json(exercises))
38}
39
40/**
41Add a route for each controller in this module.
42
43The name starts with an underline in order to appear before other functions in the module documentation.
44
45We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
46*/
47pub fn _add_routes(cfg: &mut ServiceConfig) {
48    cfg.route("/{course_id}", web::get().to(get_for_course));
49}