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};
4
5use crate::prelude::*;
6
7/**
8GET `/api/v0/main-frontend/regradings` - Returns a paginated list of all the regradings.
9*/
10
11#[instrument(skip(pool, user))]
12async fn get_regradings(
13    pool: web::Data<PgPool>,
14    user: AuthUser,
15    pagination: web::Query<Pagination>,
16) -> ControllerResult<web::Json<Vec<Regrading>>> {
17    let mut conn = pool.acquire().await?;
18    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
19    let res = models::regradings::get_all_paginated(&mut conn, *pagination).await?;
20    token.authorized_ok(web::Json(res))
21}
22
23/**
24GET `/api/v0/main-frontend/regradings/count` - Counts regradings
25*/
26
27#[instrument(skip(pool, user))]
28async fn get_regradings_count(
29    pool: web::Data<PgPool>,
30    user: AuthUser,
31) -> ControllerResult<web::Json<i64>> {
32    let mut conn = pool.acquire().await?;
33    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
34    let res = models::regradings::get_all_count(&mut conn).await?;
35    token.authorized_ok(web::Json(res))
36}
37
38/**
39POST `/api/v0/main-frontend/regradings` - Creates a new regrading for the supplied exercise task submission ids and returns the new regrading id.
40*/
41
42#[instrument(skip(pool, user))]
43async fn create(
44    pool: web::Data<PgPool>,
45    user: AuthUser,
46    new_regrading: web::Json<NewRegrading>,
47) -> ControllerResult<web::Json<Uuid>> {
48    let mut conn = pool.acquire().await?;
49    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
50    let res = models::regradings::insert_and_create_regradings(&mut conn, new_regrading.0, user.id)
51        .await?;
52    token.authorized_ok(web::Json(res))
53}
54
55/**
56GET `/api/v0/main-frontend/regradings/{id}` - Returns relevant information about a regrading.
57*/
58
59#[instrument(skip(pool, user))]
60async fn get_regrading_info_by_id(
61    pool: web::Data<PgPool>,
62    user: AuthUser,
63    regrading_id: web::Path<Uuid>,
64) -> ControllerResult<web::Json<RegradingInfo>> {
65    let mut conn = pool.acquire().await?;
66    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?;
67    let res = models::regradings::get_regrading_info_by_id(&mut conn, *regrading_id).await?;
68    token.authorized_ok(web::Json(res))
69}
70
71/**
72Add a route for each controller in this module.
73
74The name starts with an underline in order to appear before other functions in the module documentation.
75
76We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
77*/
78pub fn _add_routes(cfg: &mut ServiceConfig) {
79    cfg.route("", web::get().to(get_regradings))
80        .route("/count", web::get().to(get_regradings_count))
81        .route("", web::post().to(create))
82        .route("/{regrading_id}", web::get().to(get_regrading_info_by_id));
83}