headless_lms_server/controllers/course_material/
glossary.rs

1use models::glossary::TermUpdate;
2use utoipa::OpenApi;
3
4use crate::prelude::*;
5
6#[derive(OpenApi)]
7#[openapi(paths(update, delete))]
8pub(crate) struct CourseMaterialGlossaryApiDoc;
9
10#[utoipa::path(
11    put,
12    path = "/{id}/update",
13    operation_id = "updateCourseMaterialGlossaryTerm",
14    tag = "course-material-glossary",
15    params(
16        ("id" = Uuid, Path, description = "Glossary term id")
17    ),
18    request_body = TermUpdate,
19    responses(
20        (status = 200, description = "Glossary term updated")
21    )
22)]
23#[instrument(skip(pool))]
24async fn update(
25    pool: web::Data<PgPool>,
26    acronym_id: web::Path<Uuid>,
27    update: web::Json<TermUpdate>,
28    user: AuthUser,
29) -> ControllerResult<HttpResponse> {
30    let mut conn = pool.acquire().await?;
31    models::glossary::update(&mut conn, *acronym_id, &update.term, &update.definition).await?;
32    let token = authorize(&mut conn, Act::View, Some(user.id), Res::AnyCourse).await?;
33    token.authorized_ok(HttpResponse::Ok().finish())
34}
35
36#[utoipa::path(
37    delete,
38    path = "/{id}/delete",
39    operation_id = "deleteCourseMaterialGlossaryTerm",
40    tag = "course-material-glossary",
41    params(
42        ("id" = Uuid, Path, description = "Glossary term id")
43    ),
44    responses(
45        (status = 200, description = "Glossary term deleted")
46    )
47)]
48#[instrument(skip(pool))]
49async fn delete(
50    pool: web::Data<PgPool>,
51    acronym_id: web::Path<Uuid>,
52    user: AuthUser,
53) -> ControllerResult<HttpResponse> {
54    let mut conn = pool.acquire().await?;
55    models::glossary::delete(&mut conn, *acronym_id).await?;
56    let token = authorize(&mut conn, Act::View, Some(user.id), Res::AnyCourse).await?;
57    token.authorized_ok(HttpResponse::Ok().finish())
58}
59
60/**
61Add a route for each controller in this module.
62
63The name starts with an underline in order to appear before other functions in the module documentation.
64
65We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
66*/
67pub fn _add_routes(cfg: &mut ServiceConfig) {
68    cfg.route("/{id}/update", web::put().to(update))
69        .route("/{id}/delete", web::delete().to(delete));
70}