fix: auth for admin
This commit is contained in:
parent
6fb90e42c3
commit
a2a0fe5510
9 changed files with 129 additions and 46 deletions
|
|
@ -13,6 +13,8 @@ use serde::Serialize;
|
|||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::auth::AdminConn;
|
||||
|
||||
/// List all spots for a scene.
|
||||
pub async fn list_spots(
|
||||
State(pool): State<PgPool>,
|
||||
|
|
@ -41,13 +43,16 @@ pub struct CreateSpotResponse {
|
|||
|
||||
/// Create a new spot in a scene.
|
||||
pub async fn create_spot(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(scene_id): Path<Uuid>,
|
||||
Json(req): Json<CreateSpotRequest>,
|
||||
) -> Result<Json<CreateSpotResponse>, AppError> {
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
|
||||
// Check if slug is available (if provided)
|
||||
if let Some(ref slug) = req.slug {
|
||||
let available = spots::is_spot_slug_available(&pool, scene_id, slug).await?;
|
||||
let available = spots::is_spot_slug_available(&mut *guard, scene_id, slug).await?;
|
||||
if !available {
|
||||
return Err(AppError::Conflict(format!(
|
||||
"Spot slug '{}' is already taken in this scene",
|
||||
|
|
@ -56,25 +61,28 @@ pub async fn create_spot(
|
|||
}
|
||||
}
|
||||
|
||||
let spot = spots::create_spot(&pool, scene_id, &req).await?;
|
||||
let spot = spots::create_spot(&mut *guard, scene_id, &req).await?;
|
||||
Ok(Json(CreateSpotResponse { id: spot.id }))
|
||||
}
|
||||
|
||||
/// Update a spot.
|
||||
pub async fn update_spot(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(spot_id): Path<Uuid>,
|
||||
Json(req): Json<UpdateSpotRequest>,
|
||||
) -> Result<Json<Spot>, AppError> {
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
|
||||
// If updating slug, check availability
|
||||
if let Some(ref new_slug) = req.slug {
|
||||
let existing = spots::get_spot_by_id(&pool, spot_id)
|
||||
let existing = spots::get_spot_by_id(&mut *guard, spot_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFound("Spot not found".to_string()))?;
|
||||
|
||||
if Some(new_slug.clone()) != existing.slug {
|
||||
let available =
|
||||
spots::is_spot_slug_available(&pool, existing.scene_id, new_slug).await?;
|
||||
spots::is_spot_slug_available(&mut *guard, existing.scene_id, new_slug).await?;
|
||||
if !available {
|
||||
return Err(AppError::Conflict(format!(
|
||||
"Spot slug '{}' is already taken in this scene",
|
||||
|
|
@ -84,15 +92,17 @@ pub async fn update_spot(
|
|||
}
|
||||
}
|
||||
|
||||
let spot = spots::update_spot(&pool, spot_id, &req).await?;
|
||||
let spot = spots::update_spot(&mut *guard, spot_id, &req).await?;
|
||||
Ok(Json(spot))
|
||||
}
|
||||
|
||||
/// Delete a spot.
|
||||
pub async fn delete_spot(
|
||||
State(pool): State<PgPool>,
|
||||
admin_conn: AdminConn,
|
||||
Path(spot_id): Path<Uuid>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
spots::delete_spot(&pool, spot_id).await?;
|
||||
let conn = admin_conn.0;
|
||||
let mut guard = conn.acquire().await;
|
||||
spots::delete_spot(&mut *guard, spot_id).await?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue