Skip to main content

headless_lms_server/controllers/cms/
code_giveaways.rs

1//! Controllers for requests starting with `/api/v0/cms/code-giveaways`.
2use headless_lms_models::code_giveaways::CodeGiveaway;
3use utoipa::OpenApi;
4
5use crate::prelude::*;
6
7#[derive(OpenApi)]
8#[openapi(paths(get_code_giveaways_by_course))]
9pub(crate) struct CmsCodeGiveawaysApiDoc;
10
11/**
12GET `/api/v0/cms/code-giveaways/by-course/:course_id` - Returns code giveaways for a course.
13 */
14#[instrument(skip(pool))]
15#[utoipa::path(
16    get,
17    path = "/by-course/{course_id}",
18    operation_id = "getCmsCodeGiveawaysByCourse",
19    tag = "cms_code_giveaways",
20    params(
21        ("course_id" = Uuid, Path, description = "Course id")
22    ),
23    responses(
24        (status = 200, description = "Code giveaways for course", body = Vec<CodeGiveaway>)
25    )
26)]
27async fn get_code_giveaways_by_course(
28    pool: web::Data<PgPool>,
29    course_id: web::Path<Uuid>,
30    user: AuthUser,
31) -> ControllerResult<web::Json<Vec<CodeGiveaway>>> {
32    let mut conn = pool.acquire().await?;
33
34    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(*course_id)).await?;
35
36    let code_giveaways = models::code_giveaways::get_all_for_course(&mut conn, *course_id).await?;
37
38    token.authorized_ok(web::Json(code_giveaways))
39}
40
41/**
42Add a route for each controller in this module.
43
44The name starts with an underline in order to appear before other functions in the module documentation.
45
46We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
47*/
48pub fn _add_routes(cfg: &mut ServiceConfig) {
49    cfg.route(
50        "by-course/{course_id}",
51        web::get().to(get_code_giveaways_by_course),
52    );
53}