132 lines
3.1 KiB
Rust
132 lines
3.1 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{
|
|
DatabaseError,
|
|
dtos::{Delete, TransactionRow, Upsert},
|
|
};
|
|
|
|
pub struct Query {
|
|
pub limit: Option<i64>,
|
|
pub offset: Option<i64>,
|
|
}
|
|
|
|
impl Default for Query {
|
|
fn default() -> Self {
|
|
Self {
|
|
limit: None,
|
|
offset: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<TransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
TransactionRow,
|
|
r#"
|
|
SELECT id, account_id, amount_cents, direction, date, description
|
|
FROM transactions
|
|
ORDER BY date DESC, id DESC
|
|
LIMIT $1
|
|
OFFSET $2
|
|
"#,
|
|
query.limit,
|
|
query.offset,
|
|
)
|
|
.fetch_all(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn get_by_id<'e, E>(
|
|
executor: E,
|
|
id: Uuid,
|
|
) -> Result<Option<TransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
TransactionRow,
|
|
r#"
|
|
SELECT id, account_id, amount_cents, direction, date, description
|
|
FROM transactions
|
|
WHERE id = $1
|
|
"#,
|
|
id,
|
|
)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
transaction: TransactionRow,
|
|
) -> Result<Upsert<TransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let row = sqlx::query!(
|
|
r#"
|
|
INSERT INTO transactions (id, account_id, amount_cents, direction, date, description)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
account_id = EXCLUDED.account_id,
|
|
amount_cents = EXCLUDED.amount_cents,
|
|
direction = EXCLUDED.direction,
|
|
date = EXCLUDED.date,
|
|
description = EXCLUDED.description
|
|
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
|
old_row.id AS "old_id?",
|
|
new_row.id AS "id!",
|
|
new_row.account_id AS "account_id!",
|
|
new_row.amount_cents AS "amount_cents!",
|
|
new_row.direction AS "direction!",
|
|
new_row.date AS "date!",
|
|
new_row.description AS "description!"
|
|
"#,
|
|
transaction.id,
|
|
transaction.account_id,
|
|
transaction.amount_cents,
|
|
transaction.direction,
|
|
transaction.date,
|
|
transaction.description,
|
|
)
|
|
.fetch_one(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)?;
|
|
|
|
let transaction = TransactionRow {
|
|
id: row.id,
|
|
account_id: row.account_id,
|
|
amount_cents: row.amount_cents,
|
|
direction: row.direction,
|
|
date: row.date,
|
|
description: row.description,
|
|
};
|
|
|
|
if row.old_id.is_some() {
|
|
Ok(Upsert::Updated(transaction))
|
|
} else {
|
|
Ok(Upsert::Created(transaction))
|
|
}
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)?;
|
|
|
|
Ok(if deleted.is_some() {
|
|
Delete::Deleted
|
|
} else {
|
|
Delete::NotFound
|
|
})
|
|
}
|