1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use models::playground_examples::{PlaygroundExample, PlaygroundExampleData};

use crate::{domain::authorization::skip_authorize, prelude::*};

/**
GET `/api/v0/main-frontend/playground_examples` - Returns all playground examples that are not deleted.
*/
#[instrument(skip(pool))]
async fn get_playground_examples(
    pool: web::Data<PgPool>,
) -> ControllerResult<web::Json<Vec<PlaygroundExample>>> {
    let mut conn = pool.acquire().await?;
    let res = models::playground_examples::get_all_playground_examples(&mut conn).await?;

    let token = skip_authorize();
    token.authorized_ok(web::Json(res))
}

/**
POST `/api/v0/main-frontend/playground_examples` - Saves a playground example.
*/
#[instrument(skip(pool))]
async fn insert_playground_example(
    pool: web::Data<PgPool>,
    payload: web::Json<PlaygroundExampleData>,
    user: AuthUser,
) -> ControllerResult<web::Json<PlaygroundExample>> {
    let mut conn = pool.acquire().await?;
    let new_example = payload.0;
    let res =
        models::playground_examples::insert_playground_example(&mut conn, new_example).await?;

    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
    token.authorized_ok(web::Json(res))
}

/**
PUT `/api/v0/main-frontend/playground_examples` - Updates existing playground example.
*/
#[instrument(skip(pool))]
async fn update_playground_example(
    pool: web::Data<PgPool>,
    payload: web::Json<PlaygroundExample>,
    user: AuthUser,
) -> ControllerResult<web::Json<PlaygroundExample>> {
    let mut conn = pool.acquire().await?;
    let example = payload.0;

    let res = models::playground_examples::update_playground_example(&mut conn, example).await?;

    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
    token.authorized_ok(web::Json(res))
}

/**
DELETE `/api/v0/main-frontend/playground_examples` - Deletes a playground example if exists.
*/
#[instrument(skip(pool))]
async fn delete_playground_example(
    pool: web::Data<PgPool>,
    playground_example_id: web::Path<Uuid>,
    user: AuthUser,
) -> ControllerResult<web::Json<PlaygroundExample>> {
    let mut conn = pool.acquire().await?;
    let example_id = *playground_example_id;
    let res = models::playground_examples::delete_playground_example(&mut conn, example_id).await?;

    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::PlaygroundExample).await?;
    token.authorized_ok(web::Json(res))
}

/**
Add a route for each controller in this module.

The name starts with an underline in order to appear before other functions in the module documentation.

We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
*/
pub fn _add_routes(cfg: &mut ServiceConfig) {
    cfg.route("", web::get().to(get_playground_examples))
        .route("", web::post().to(insert_playground_example))
        .route("", web::put().to(update_playground_example))
        .route("/{id}", web::delete().to(delete_playground_example));
}