Added account member API.

This commit is contained in:
2026-10-11 10:00:04 +02:00
parent 6ee0317186
commit 21f0c17af9
27 changed files with 994 additions and 8 deletions
+129
View File
@@ -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)
}
+2
View File
@@ -1,4 +1,5 @@
mod account;
mod account_member;
mod recurring_transaction;
mod results;
mod token;
@@ -8,6 +9,7 @@ mod user_app;
mod user_app_secret;
pub use account::*;
pub use account_member::*;
pub use recurring_transaction::*;
pub use results::*;
pub use token::*;
@@ -0,0 +1,7 @@
use uuid::Uuid;
pub struct AccountMemberRow {
pub id: Uuid,
pub account_id: Uuid,
pub user_id: Uuid,
}
+1
View File
@@ -1 +1,2 @@
pub const PG_FOREIGN_KEY_VIOLATION: &str = "23503";
pub const PG_UNIQUE_VIOLATION: &str = "23505";