headless_lms_server/controllers/main_frontend/
glossary.rs

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