Added account member API.
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{AccountMemberRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub account_id: Uuid,
|
||||
pub limit: Option<i64>,
|
||||
pub offset: Option<i64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<AccountMemberRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
AccountMemberRow,
|
||||
r#"
|
||||
SELECT id, account_id, user_id
|
||||
FROM account_members
|
||||
WHERE account_id = $1
|
||||
ORDER BY id
|
||||
LIMIT $2
|
||||
OFFSET $3
|
||||
"#,
|
||||
query.account_id,
|
||||
query.limit,
|
||||
query.offset,
|
||||
)
|
||||
.fetch_all(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
account_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
AccountMemberRow,
|
||||
r#"
|
||||
SELECT id, account_id, user_id
|
||||
FROM account_members
|
||||
WHERE account_id = $1 AND id = $2
|
||||
"#,
|
||||
account_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
member: AccountMemberRow,
|
||||
) -> Result<Upsert<AccountMemberRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO account_members (id, account_id, user_id)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (id, account_id) DO UPDATE SET
|
||||
user_id = EXCLUDED.user_id
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
|
||||
COALESCE(old_row.user_id, new_row.user_id) AS "old_user_id!",
|
||||
new_row.id AS "id!",
|
||||
new_row.account_id AS "account_id!",
|
||||
new_row.user_id AS "user_id!"
|
||||
"#,
|
||||
member.id,
|
||||
member.account_id,
|
||||
member.user_id,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| AccountMemberRow {
|
||||
id,
|
||||
account_id: row.old_account_id,
|
||||
user_id: row.old_user_id,
|
||||
});
|
||||
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: AccountMemberRow {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
user_id: row.user_id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
account_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
AccountMemberRow,
|
||||
r#"
|
||||
DELETE FROM account_members
|
||||
WHERE account_id = $1 AND id = $2
|
||||
RETURNING id, account_id, user_id
|
||||
"#,
|
||||
account_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
Reference in New Issue
Block a user