More scoping by user ID at DB level.
This commit is contained in:
@@ -8,17 +8,19 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub user_id: Uuid,
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, RepositoryError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let query = db::Query {
|
||||
user_id: query.user_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
@@ -29,7 +31,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let apps = db::list(executor, query)
|
||||
let apps = db::list(executor, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -38,36 +40,50 @@ where
|
||||
Ok(apps)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
let app = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
pub async fn find_owner_by_id<'e, E>(
|
||||
pub async fn get_by_id_unscoped<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
) -> Result<Option<Uuid>, RepositoryError>
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let owner = db::find_owner_by_id(executor, app_id).await?;
|
||||
let app = db::get_by_id_unscoped(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(owner)
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserApp,
|
||||
) -> Result<Option<Upsert<UserApp>>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -86,11 +102,15 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::delete_by_id(executor, id)
|
||||
let app = db::delete_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Reference in New Issue
Block a user