headless_lms_server/controllers/cms/
code_giveaways.rs1use 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#[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
41pub 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}