Files
expenses/server/src/database/accounts.rs
T

163 lines
3.6 KiB
Rust

use uuid::Uuid;
use super::{
DatabaseError,
dtos::{AccountRow, Upsert},
};
pub struct Query {
pub name: Option<String>,
pub limit: Option<i64>,
pub offset: Option<i64>,
}
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
query: Query,
) -> Result<Vec<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE ($2::text IS NULL OR name ILIKE '%' || $2 || '%')
ORDER BY name DESC, id DESC
LIMIT $3
OFFSET $4
"#,
user_id,
query.name,
query.limit,
query.offset,
)
.fetch_all(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE id = $2
"#,
user_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn get_by_ids<'e, E>(
executor: E,
user_id: Uuid,
ids: &[Uuid],
) -> Result<Vec<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE id = ANY($2)
"#,
user_id,
ids,
)
.fetch_all(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
account: AccountRow,
) -> Result<Option<Upsert<AccountRow>>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO accounts (id, name)
VALUES ($2, $3)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name
WHERE accounts.id IN (SELECT id FROM accessible_accounts($1))
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
COALESCE(old_row.name, new_row.name) AS "old_name!",
new_row.id AS "id!",
new_row.name AS "name!"
"#,
user_id,
account.id,
account.name,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
// Nothing is written when the account is not accessible, or when the id belongs to a row in an
// account that is not accessible.
let Some(row) = row else {
return Ok(None);
};
// `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| AccountRow {
id,
name: row.old_name,
});
Ok(Some(Upsert {
previous,
current: AccountRow {
id: row.id,
name: row.name,
},
}))
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
DELETE FROM accounts
WHERE id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
RETURNING id, name
"#,
user_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)
}