headless_lms_server/controllers/main_frontend/
playground_examples.rsuse models::playground_examples::{PlaygroundExample, PlaygroundExampleData};
use crate::{domain::authorization::skip_authorize, prelude::*};
#[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))
}
#[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))
}
#[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))
}
#[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))
}
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));
}