Skip to main content

headless_lms_server/controllers/main_frontend/
playground_examples.rs

1use models::playground_examples::{PlaygroundExample, PlaygroundExampleData};
2use utoipa::OpenApi;
3
4use crate::{domain::authorization::skip_authorize, prelude::*};
5
6#[derive(OpenApi)]
7#[openapi(paths(
8    get_playground_examples,
9    insert_playground_example,
10    update_playground_example,
11    delete_playground_example
12))]
13pub(crate) struct MainFrontendPlaygroundExamplesApiDoc;
14
15/**
16GET `/api/v0/main-frontend/playground_examples` - Returns all playground examples that are not deleted.
17*/
18#[utoipa::path(
19    get,
20    path = "",
21    operation_id = "getPlaygroundExamples",
22    tag = "playground-examples",
23    responses(
24        (status = 200, description = "Playground examples", body = Vec<PlaygroundExample>)
25    )
26)]
27#[instrument(skip(pool))]
28async fn get_playground_examples(
29    pool: web::Data<PgPool>,
30) -> ControllerResult<web::Json<Vec<PlaygroundExample>>> {
31    let mut conn = pool.acquire().await?;
32    let res = models::playground_examples::get_all_playground_examples(&mut conn).await?;
33
34    let token = skip_authorize();
35    token.authorized_ok(web::Json(res))
36}
37
38/**
39POST `/api/v0/main-frontend/playground_examples` - Saves a playground example.
40*/
41#[utoipa::path(
42    post,
43    path = "",
44    operation_id = "createPlaygroundExample",
45    tag = "playground-examples",
46    request_body = PlaygroundExampleData,
47    responses(
48        (status = 200, description = "Created playground example", body = PlaygroundExample)
49    )
50)]
51#[instrument(skip(pool))]
52async fn insert_playground_example(
53    pool: web::Data<PgPool>,
54    payload: web::Json<PlaygroundExampleData>,
55    user: AuthUser,
56) -> ControllerResult<web::Json<PlaygroundExample>> {
57    let mut conn = pool.acquire().await?;
58    let new_example = payload.0;
59    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
60    let res =
61        models::playground_examples::insert_playground_example(&mut conn, new_example).await?;
62
63    token.authorized_ok(web::Json(res))
64}
65
66/**
67PUT `/api/v0/main-frontend/playground_examples` - Updates existing playground example.
68*/
69#[utoipa::path(
70    put,
71    path = "",
72    operation_id = "updatePlaygroundExample",
73    tag = "playground-examples",
74    request_body = PlaygroundExample,
75    responses(
76        (status = 200, description = "Updated playground example", body = PlaygroundExample)
77    )
78)]
79#[instrument(skip(pool))]
80async fn update_playground_example(
81    pool: web::Data<PgPool>,
82    payload: web::Json<PlaygroundExample>,
83    user: AuthUser,
84) -> ControllerResult<web::Json<PlaygroundExample>> {
85    let mut conn = pool.acquire().await?;
86    let example = payload.0;
87    models::playground_examples::get_by_id(&mut conn, example.id).await?;
88    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
89
90    let res = models::playground_examples::update_playground_example(&mut conn, example).await?;
91
92    token.authorized_ok(web::Json(res))
93}
94
95/**
96DELETE `/api/v0/main-frontend/playground_examples` - Deletes a playground example if exists.
97*/
98#[utoipa::path(
99    delete,
100    path = "/{playground_example_id}",
101    operation_id = "deletePlaygroundExample",
102    tag = "playground-examples",
103    params(
104        ("playground_example_id" = Uuid, Path, description = "Playground example id")
105    ),
106    responses(
107        (status = 200, description = "Deleted playground example", body = PlaygroundExample)
108    )
109)]
110#[instrument(skip(pool))]
111async fn delete_playground_example(
112    pool: web::Data<PgPool>,
113    playground_example_id: web::Path<Uuid>,
114    user: AuthUser,
115) -> ControllerResult<web::Json<PlaygroundExample>> {
116    let mut conn = pool.acquire().await?;
117    let example_id = *playground_example_id;
118    models::playground_examples::get_by_id(&mut conn, example_id).await?;
119    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
120    let res = models::playground_examples::delete_playground_example(&mut conn, example_id).await?;
121
122    token.authorized_ok(web::Json(res))
123}
124
125/**
126Add a route for each controller in this module.
127
128The name starts with an underline in order to appear before other functions in the module documentation.
129
130We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
131*/
132pub fn _add_routes(cfg: &mut ServiceConfig) {
133    cfg.route("", web::get().to(get_playground_examples))
134        .route("", web::post().to(insert_playground_example))
135        .route("", web::put().to(update_playground_example))
136        .route(
137            "/{playground_example_id}",
138            web::delete().to(delete_playground_example),
139        );
140}