headless_lms_server/controllers/main_frontend/
regradings.rs

1//! Controllers for requests starting with `/api/v0/main-frontend/regradings/`.
2
3use models::regradings::{NewRegrading, Regrading, RegradingInfo};
4use utoipa::OpenApi;
5
6use crate::prelude::*;
7
8#[derive(OpenApi)]
9#[openapi(paths(get_regradings, get_regradings_count, create, get_regrading_info_by_id))]
10pub(crate) struct MainFrontendRegradingsApiDoc;
11
12/**
13GET `/api/v0/main-frontend/regradings` - Returns a paginated list of all the regradings.
14*/
15
16#[utoipa::path(
17    get,
18    path = "",
19    operation_id = "getRegradings",
20    tag = "regradings",
21    params(
22        ("page" = Option<i64>, Query, description = "Page number"),
23        ("limit" = Option<i64>, Query, description = "Page size")
24    ),
25    responses(
26        (status = 200, description = "Regradings", body = [Regrading])
27    )
28)]
29#[instrument(skip(pool, user))]
30async fn get_regradings(
31    pool: web::Data<PgPool>,
32    user: AuthUser,
33    pagination: web::Query<Pagination>,
34) -> ControllerResult<web::Json<Vec<Regrading>>> {
35    let mut conn = pool.acquire().await?;
36    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
37    let res = models::regradings::get_all_paginated(&mut conn, *pagination).await?;
38    token.authorized_ok(web::Json(res))
39}
40
41/**
42GET `/api/v0/main-frontend/regradings/count` - Counts regradings
43*/
44
45#[utoipa::path(
46    get,
47    path = "/count",
48    operation_id = "getRegradingsCount",
49    tag = "regradings",
50    responses(
51        (status = 200, description = "Regradings count", body = i64)
52    )
53)]
54#[instrument(skip(pool, user))]
55async fn get_regradings_count(
56    pool: web::Data<PgPool>,
57    user: AuthUser,
58) -> ControllerResult<web::Json<i64>> {
59    let mut conn = pool.acquire().await?;
60    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
61    let res = models::regradings::get_all_count(&mut conn).await?;
62    token.authorized_ok(web::Json(res))
63}
64
65/**
66POST `/api/v0/main-frontend/regradings` - Creates a new regrading for the supplied exercise task submission ids and returns the new regrading id.
67*/
68
69#[utoipa::path(
70    post,
71    path = "",
72    operation_id = "createRegrading",
73    tag = "regradings",
74    request_body = NewRegrading,
75    responses(
76        (status = 200, description = "Created regrading id", body = String)
77    )
78)]
79#[instrument(skip(pool, user))]
80async fn create(
81    pool: web::Data<PgPool>,
82    user: AuthUser,
83    new_regrading: web::Json<NewRegrading>,
84) -> ControllerResult<web::Json<Uuid>> {
85    let mut conn = pool.acquire().await?;
86    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
87    let res = models::regradings::insert_and_create_regradings(&mut conn, new_regrading.0, user.id)
88        .await?;
89    token.authorized_ok(web::Json(res))
90}
91
92/**
93GET `/api/v0/main-frontend/regradings/{id}` - Returns relevant information about a regrading.
94*/
95
96#[utoipa::path(
97    get,
98    path = "/{regrading_id}",
99    operation_id = "getRegradingInfo",
100    tag = "regradings",
101    params(
102        ("regrading_id" = Uuid, Path, description = "Regrading id")
103    ),
104    responses(
105        (status = 200, description = "Regrading info", body = RegradingInfo)
106    )
107)]
108#[instrument(skip(pool, user))]
109async fn get_regrading_info_by_id(
110    pool: web::Data<PgPool>,
111    user: AuthUser,
112    regrading_id: web::Path<Uuid>,
113) -> ControllerResult<web::Json<RegradingInfo>> {
114    let mut conn = pool.acquire().await?;
115    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
116    let res = models::regradings::get_regrading_info_by_id(&mut conn, *regrading_id).await?;
117    token.authorized_ok(web::Json(res))
118}
119
120/**
121Add a route for each controller in this module.
122
123The name starts with an underline in order to appear before other functions in the module documentation.
124
125We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
126*/
127pub fn _add_routes(cfg: &mut ServiceConfig) {
128    cfg.route("", web::get().to(get_regradings))
129        .route("/count", web::get().to(get_regradings_count))
130        .route("", web::post().to(create))
131        .route("/{regrading_id}", web::get().to(get_regrading_info_by_id));
132}