Check account membership on DB level.
This commit is contained in:
@@ -15,7 +15,11 @@ pub struct Query {
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<AccountMember>, RepositoryError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<AccountMember>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -31,7 +35,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let members = db::list(executor, query)
|
||||
let members = db::list(executor, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -42,13 +46,14 @@ where
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<AccountMember>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let member = db::get_by_id(executor, account_id, id)
|
||||
let member = db::get_by_id(executor, user_id, account_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
@@ -58,20 +63,37 @@ where
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
member: AccountMember,
|
||||
) -> Result<Upsert<AccountMember>, RepositoryError>
|
||||
) -> Result<Option<Upsert<AccountMember>>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let member = db::upsert(executor, encode(member))
|
||||
let member = db::upsert(executor, user_id, encode(member))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
|
||||
Ok(member)
|
||||
}
|
||||
|
||||
/// Adds the first member of a newly created account, without checking account access.
|
||||
pub async fn insert_initial<'e, E>(
|
||||
executor: E,
|
||||
member: AccountMember,
|
||||
) -> Result<AccountMember, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = db::insert_initial(executor, encode(member))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?;
|
||||
|
||||
Ok(decode(row)?)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
@@ -103,13 +125,14 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<AccountMember>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let member = db::delete_by_id(executor, account_id, id)
|
||||
let member = db::delete_by_id(executor, user_id, account_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
@@ -62,6 +62,22 @@ where
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn get_by_id_locking<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Account>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let account = db::get_by_id_locking(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn get_by_ids<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
|
||||
Reference in New Issue
Block a user