headless_lms_server/controllers/main_frontend/
playground_examples.rs

1use models::playground_examples::{PlaygroundExample, PlaygroundExampleData};
2
3use crate::{domain::authorization::skip_authorize, prelude::*};
4
5/**
6GET `/api/v0/main-frontend/playground_examples` - Returns all playground examples that are not deleted.
7*/
8#[instrument(skip(pool))]
9async fn get_playground_examples(
10    pool: web::Data<PgPool>,
11) -> ControllerResult<web::Json<Vec<PlaygroundExample>>> {
12    let mut conn = pool.acquire().await?;
13    let res = models::playground_examples::get_all_playground_examples(&mut conn).await?;
14
15    let token = skip_authorize();
16    token.authorized_ok(web::Json(res))
17}
18
19/**
20POST `/api/v0/main-frontend/playground_examples` - Saves a playground example.
21*/
22#[instrument(skip(pool))]
23async fn insert_playground_example(
24    pool: web::Data<PgPool>,
25    payload: web::Json<PlaygroundExampleData>,
26    user: AuthUser,
27) -> ControllerResult<web::Json<PlaygroundExample>> {
28    let mut conn = pool.acquire().await?;
29    let new_example = payload.0;
30    let res =
31        models::playground_examples::insert_playground_example(&mut conn, new_example).await?;
32
33    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
34    token.authorized_ok(web::Json(res))
35}
36
37/**
38PUT `/api/v0/main-frontend/playground_examples` - Updates existing playground example.
39*/
40#[instrument(skip(pool))]
41async fn update_playground_example(
42    pool: web::Data<PgPool>,
43    payload: web::Json<PlaygroundExample>,
44    user: AuthUser,
45) -> ControllerResult<web::Json<PlaygroundExample>> {
46    let mut conn = pool.acquire().await?;
47    let example = payload.0;
48
49    let res = models::playground_examples::update_playground_example(&mut conn, example).await?;
50
51    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
52    token.authorized_ok(web::Json(res))
53}
54
55/**
56DELETE `/api/v0/main-frontend/playground_examples` - Deletes a playground example if exists.
57*/
58#[instrument(skip(pool))]
59async fn delete_playground_example(
60    pool: web::Data<PgPool>,
61    playground_example_id: web::Path<Uuid>,
62    user: AuthUser,
63) -> ControllerResult<web::Json<PlaygroundExample>> {
64    let mut conn = pool.acquire().await?;
65    let example_id = *playground_example_id;
66    let res = models::playground_examples::delete_playground_example(&mut conn, example_id).await?;
67
68    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
69    token.authorized_ok(web::Json(res))
70}
71
72/**
73Add a route for each controller in this module.
74
75The name starts with an underline in order to appear before other functions in the module documentation.
76
77We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
78*/
79pub fn _add_routes(cfg: &mut ServiceConfig) {
80    cfg.route("", web::get().to(get_playground_examples))
81        .route("", web::post().to(insert_playground_example))
82        .route("", web::put().to(update_playground_example))
83        .route("/{id}", web::delete().to(delete_playground_example));
84}