Skip to main content

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