Skip to main content

headless_lms_server/controllers/main_frontend/
feedback.rs

1use models::feedback;
2use utoipa::{OpenApi, ToSchema};
3
4use crate::prelude::*;
5
6#[derive(OpenApi)]
7#[openapi(paths(mark_as_read))]
8pub(crate) struct MainFrontendFeedbackApiDoc;
9
10#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
11
12pub struct MarkAsRead {
13    read: bool,
14}
15
16/**
17POST `/api/v0/main-frontend/feedback/:id` - Creates new feedback.
18*/
19#[utoipa::path(
20    post,
21    path = "/{feedback_id}",
22    operation_id = "markFeedbackAsRead",
23    tag = "feedback",
24    params(
25        ("feedback_id" = String, Path, description = "Feedback id")
26    ),
27    request_body = MarkAsRead,
28    responses(
29        (status = 200, description = "Feedback read state updated")
30    )
31)]
32#[instrument(skip(pool))]
33pub async fn mark_as_read(
34    feedback_id: web::Path<Uuid>,
35    mark_as_read: web::Json<MarkAsRead>,
36    pool: web::Data<PgPool>,
37    user: AuthUser,
38) -> ControllerResult<HttpResponse> {
39    let mut conn = pool.acquire().await?;
40    let feedback = feedback::get_by_id(&mut conn, *feedback_id).await?;
41    let course_id = feedback
42        .course_id
43        .ok_or_else(|| controller_err!(Forbidden, "Feedback is not course-scoped".to_string()))?;
44    let token = authorize(&mut conn, Act::Teach, Some(user.id), Res::Course(course_id)).await?;
45    feedback::set_read_state_by_id_and_course_id(
46        &mut conn,
47        *feedback_id,
48        course_id,
49        mark_as_read.into_inner().read,
50    )
51    .await?;
52    token.authorized_ok(HttpResponse::Ok().finish())
53}
54
55/**
56Add a route for each controller in this module.
57
58The name starts with an underline in order to appear before other functions in the module documentation.
59
60We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
61*/
62pub fn _add_routes(cfg: &mut ServiceConfig) {
63    cfg.route("/{feedback_id}", web::post().to(mark_as_read));
64}