headless_lms_server/controllers/course_material/
proposed_edits.rs

1use models::proposed_page_edits::NewProposedPageEdits;
2use utoipa::OpenApi;
3
4use crate::prelude::*;
5
6#[derive(OpenApi)]
7#[openapi(paths(post_proposed_edits))]
8pub(crate) struct CourseMaterialProposedEditsApiDoc;
9
10/**
11POST `/api/v0/course-material/proposed-edits/:course-id`
12*/
13#[utoipa::path(
14    post,
15    path = "/{course_id}",
16    operation_id = "postCourseMaterialProposedEdits",
17    tag = "course-material-proposed-edits",
18    params(
19        ("course_id" = Uuid, Path, description = "Course id")
20    ),
21    request_body = NewProposedPageEdits,
22    responses(
23        (status = 200, description = "Created proposed edit")
24    )
25)]
26#[instrument(skip(pool))]
27async fn post_proposed_edits(
28    pool: web::Data<PgPool>,
29    course_id: web::Path<Uuid>,
30    payload: web::Json<NewProposedPageEdits>,
31    user: Option<AuthUser>,
32) -> ControllerResult<HttpResponse> {
33    let mut conn = pool.acquire().await?;
34    let user_id = user.map(|u| u.id);
35    models::proposed_page_edits::insert(
36        &mut conn,
37        PKeyPolicy::Generate,
38        *course_id,
39        user_id,
40        &payload,
41    )
42    .await?;
43    let token = authorize(&mut conn, Act::View, user_id, Res::Course(*course_id)).await?;
44    token.authorized_ok(HttpResponse::Ok().finish())
45}
46
47/**
48Add a route for each controller in this module.
49
50The name starts with an underline in order to appear before other functions in the module documentation.
51
52We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
53*/
54pub fn _add_routes(cfg: &mut ServiceConfig) {
55    cfg.route("/{course_id}", web::post().to(post_proposed_edits));
56}