Skip to main content

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    let term = glossary::get_term_by_id(&mut conn, *id).await?;
34    let token = authorize(
35        &mut conn,
36        Act::Teach,
37        Some(user.id),
38        Res::Course(term.course_id),
39    )
40    .await?;
41    glossary::update_term_by_id_and_course_id(
42        &mut conn,
43        *id,
44        term.course_id,
45        &update.term,
46        &update.definition,
47    )
48    .await?;
49    token.authorized_ok(HttpResponse::Ok().finish())
50}
51
52#[instrument(skip(pool))]
53#[utoipa::path(
54    delete,
55    path = "/{term_id}",
56    operation_id = "deleteGlossaryTerm",
57    tag = "glossary",
58    params(
59        ("term_id" = Uuid, Path, description = "Glossary term id")
60    ),
61    responses(
62        (status = 200, description = "Glossary term deleted"),
63        (status = 401, description = "Authentication required"),
64        (status = 403, description = "User is not allowed to manage glossary terms")
65    )
66)]
67pub(crate) async fn delete(
68    id: web::Path<Uuid>,
69    pool: web::Data<PgPool>,
70    user: AuthUser,
71) -> ControllerResult<HttpResponse> {
72    let mut conn = pool.acquire().await?;
73    let term = glossary::get_term_by_id(&mut conn, *id).await?;
74    let token = authorize(
75        &mut conn,
76        Act::Teach,
77        Some(user.id),
78        Res::Course(term.course_id),
79    )
80    .await?;
81    glossary::delete_term_by_id_and_course_id(&mut conn, *id, term.course_id).await?;
82    token.authorized_ok(HttpResponse::Ok().finish())
83}
84
85/**
86Add a route for each controller in this module.
87
88The name starts with an underline in order to appear before other functions in the module documentation.
89
90We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
91*/
92pub fn _add_routes(cfg: &mut ServiceConfig) {
93    cfg.route("/{term_id}", web::put().to(update))
94        .route("/{term_id}", web::delete().to(delete));
95}