Refactored DB operations into dedicated module.
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::{
|
||||
common::{Delete, Upsert},
|
||||
dtos::recurring_transaction::RecurringTransactionRow,
|
||||
};
|
||||
|
||||
pub async fn query_scheduled_transactions<'e, E>(
|
||||
executor: E,
|
||||
) -> Result<Vec<RecurringTransactionRow>, sqlx::Error>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
RecurringTransactionRow,
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
weekend_adjustment
|
||||
FROM recurring_transactions
|
||||
ORDER BY starts_at DESC, id DESC
|
||||
"#,
|
||||
)
|
||||
.fetch_all(executor)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_scheduled_transaction_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransactionRow>, sqlx::Error>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
RecurringTransactionRow,
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
weekend_adjustment
|
||||
FROM recurring_transactions
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn upsert_scheduled_transaction<'e, E>(
|
||||
executor: E,
|
||||
transaction: RecurringTransactionRow,
|
||||
) -> Result<Upsert<RecurringTransactionRow>, sqlx::Error>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO recurring_transactions (
|
||||
id,
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
weekend_adjustment
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
amount_cents = EXCLUDED.amount_cents,
|
||||
direction = EXCLUDED.direction,
|
||||
starts_at = EXCLUDED.starts_at,
|
||||
ends_at = EXCLUDED.ends_at,
|
||||
recurrence_every = EXCLUDED.recurrence_every,
|
||||
recurrence_unit = EXCLUDED.recurrence_unit,
|
||||
weekend_adjustment = EXCLUDED.weekend_adjustment
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name!",
|
||||
new_row.amount_cents AS "amount_cents!",
|
||||
new_row.direction AS "direction!",
|
||||
new_row.starts_at AS "starts_at!",
|
||||
new_row.ends_at AS "ends_at?",
|
||||
new_row.recurrence_every AS "recurrence_every!",
|
||||
new_row.recurrence_unit AS "recurrence_unit!",
|
||||
new_row.weekend_adjustment AS "weekend_adjustment?"
|
||||
"#,
|
||||
transaction.id,
|
||||
transaction.name,
|
||||
transaction.amount_cents,
|
||||
transaction.direction,
|
||||
transaction.starts_at,
|
||||
transaction.ends_at,
|
||||
transaction.recurrence_every,
|
||||
transaction.recurrence_unit,
|
||||
transaction.weekend_adjustment,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
|
||||
let transaction = RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
recurrence_every: row.recurrence_every,
|
||||
recurrence_unit: row.recurrence_unit,
|
||||
weekend_adjustment: row.weekend_adjustment,
|
||||
};
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(transaction))
|
||||
} else {
|
||||
Ok(Upsert::Created(transaction))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_scheduled_transaction_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Delete, sqlx::Error>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user