184 lines
4.7 KiB
Rust
184 lines
4.7 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{
|
|
DatabaseError,
|
|
dtos::{Delete, RecurringTransactionRow, 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<RecurringTransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
RecurringTransactionRow,
|
|
r#"
|
|
SELECT
|
|
id,
|
|
name,
|
|
account_id,
|
|
amount_cents,
|
|
direction,
|
|
starts_at,
|
|
ends_at,
|
|
recurrence_every,
|
|
recurrence_unit,
|
|
weekend_adjustment
|
|
FROM recurring_transactions
|
|
ORDER BY starts_at 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<RecurringTransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
RecurringTransactionRow,
|
|
r#"
|
|
SELECT
|
|
id,
|
|
name,
|
|
account_id,
|
|
amount_cents,
|
|
direction,
|
|
starts_at,
|
|
ends_at,
|
|
recurrence_every,
|
|
recurrence_unit,
|
|
weekend_adjustment
|
|
FROM recurring_transactions
|
|
WHERE id = $1
|
|
"#,
|
|
id,
|
|
)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
transaction: RecurringTransactionRow,
|
|
) -> Result<Upsert<RecurringTransactionRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let row = sqlx::query!(
|
|
r#"
|
|
INSERT INTO recurring_transactions (
|
|
id,
|
|
name,
|
|
account_id,
|
|
amount_cents,
|
|
direction,
|
|
starts_at,
|
|
ends_at,
|
|
recurrence_every,
|
|
recurrence_unit,
|
|
weekend_adjustment
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
account_id = EXCLUDED.account_id,
|
|
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.account_id AS "account_id!",
|
|
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.account_id,
|
|
transaction.amount_cents,
|
|
transaction.direction,
|
|
transaction.starts_at,
|
|
transaction.ends_at,
|
|
transaction.recurrence_every,
|
|
transaction.recurrence_unit,
|
|
transaction.weekend_adjustment,
|
|
)
|
|
.fetch_one(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)?;
|
|
|
|
let transaction = RecurringTransactionRow {
|
|
id: row.id,
|
|
name: row.name,
|
|
account_id: row.account_id,
|
|
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_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 recurring_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
|
|
})
|
|
}
|