More scoping by user ID at DB level.
This commit is contained in:
@@ -7,10 +7,7 @@ use super::{
|
||||
dtos::user_app::{UserAppRequest, UserAppResponse},
|
||||
extractors::{Auth, Json, Path, Query},
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
use crate::{error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
axum::Router::new()
|
||||
@@ -42,12 +39,11 @@ async fn list_user_apps(
|
||||
}
|
||||
|
||||
let query = repository::user_apps::Query {
|
||||
user_id,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let apps = repository::user_apps::list(&pool, query)
|
||||
let apps = repository::user_apps::list(&pool, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(UserAppResponse::from)
|
||||
@@ -67,9 +63,8 @@ async fn get_user_app(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let app = repository::user_apps::get_by_id(&pool, app_id)
|
||||
let app = repository::user_apps::get_by_id(&pool, user_id, app_id)
|
||||
.await?
|
||||
.filter(|app| app.user_id == user_id)
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(Json(app.into()))
|
||||
@@ -88,17 +83,9 @@ async fn upsert_user_app(
|
||||
}
|
||||
|
||||
let app = request.into_domain(user_id, app_id);
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::user_apps::upsert(&mut *tx, app).await?;
|
||||
if upserted
|
||||
.previous
|
||||
.as_ref()
|
||||
.is_some_and(|previous| previous.user_id != user_id)
|
||||
{
|
||||
return Err(AppError::AppIdAlreadyExists);
|
||||
}
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::user_apps::upsert(&pool, app)
|
||||
.await?
|
||||
.ok_or(AppError::AppIdAlreadyExists)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
@@ -120,14 +107,9 @@ async fn delete_user_app(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
if repository::user_apps::delete_by_id(&mut *tx, app_id)
|
||||
repository::user_apps::delete_by_id(&pool, user_id, app_id)
|
||||
.await?
|
||||
.is_none_or(|user_app| user_app.user_id != user_id)
|
||||
{
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user