fix: auth for admin
This commit is contained in:
parent
6fb90e42c3
commit
a2a0fe5510
9 changed files with 129 additions and 46 deletions
|
|
@ -14,6 +14,8 @@ use sqlx::PgPool;
|
|||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::auth::AdminConn;
|
||||
|
||||
// =============================================================================
|
||||
// Image Processing Helpers
|
||||
// =============================================================================
|
||||
|
|
@ -210,17 +212,20 @@ pub struct CreateSceneResponse {
|
|||
|
||||
/// Create a new scene in a realm.
|
||||
pub async fn create_scene(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(slug): Path<String>,
|
||||
Json(mut req): Json<CreateSceneRequest>,
|
||||
) -> Result<Json<CreateSceneResponse>, AppError> {
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
|
||||
// Get the realm
|
||||
let realm = realms::get_realm_by_slug(&pool, &slug)
|
||||
let realm = realms::get_realm_by_slug(&mut *guard, &slug)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFound(format!("Realm '{}' not found", slug)))?;
|
||||
|
||||
// Check if slug is available
|
||||
let available = scenes::is_scene_slug_available(&pool, realm.id, &req.slug).await?;
|
||||
let available = scenes::is_scene_slug_available(&mut *guard, realm.id, &req.slug).await?;
|
||||
if !available {
|
||||
return Err(AppError::Conflict(format!(
|
||||
"Scene slug '{}' is already taken in this realm",
|
||||
|
|
@ -249,7 +254,7 @@ pub async fn create_scene(
|
|||
}
|
||||
}
|
||||
|
||||
let scene = scenes::create_scene_with_id(&pool, scene_id, realm.id, &req).await?;
|
||||
let scene = scenes::create_scene_with_id(&mut *guard, scene_id, realm.id, &req).await?;
|
||||
Ok(Json(CreateSceneResponse {
|
||||
id: scene.id,
|
||||
slug: scene.slug,
|
||||
|
|
@ -258,12 +263,15 @@ pub async fn create_scene(
|
|||
|
||||
/// Update a scene.
|
||||
pub async fn update_scene(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(scene_id): Path<Uuid>,
|
||||
Json(mut req): Json<UpdateSceneRequest>,
|
||||
) -> Result<Json<Scene>, AppError> {
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
|
||||
// Get the existing scene to get realm_id
|
||||
let existing_scene = scenes::get_scene_by_id(&pool, scene_id)
|
||||
let existing_scene = scenes::get_scene_by_id(&mut *guard, scene_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFound("Scene not found".to_string()))?;
|
||||
|
||||
|
|
@ -296,15 +304,17 @@ pub async fn update_scene(
|
|||
}
|
||||
}
|
||||
|
||||
let scene = scenes::update_scene(&pool, scene_id, &req).await?;
|
||||
let scene = scenes::update_scene(&mut *guard, scene_id, &req).await?;
|
||||
Ok(Json(scene))
|
||||
}
|
||||
|
||||
/// Delete a scene.
|
||||
pub async fn delete_scene(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(scene_id): Path<Uuid>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
scenes::delete_scene(&pool, scene_id).await?;
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
scenes::delete_scene(&mut *guard, scene_id).await?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue