Refactored DB operations into dedicated module.
This commit is contained in:
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::dtos::scheduled_transaction::RecurringTransactionRow,
|
||||
database::dtos::recurring_transaction::RecurringTransactionRow,
|
||||
routes::dtos::direction::{Direction, DirectionConversionError},
|
||||
};
|
||||
|
||||
|
||||
@@ -8,10 +8,17 @@ use uuid::Uuid;
|
||||
|
||||
use super::validation::scheduled_transaction::ValidatedRecurringTransaction;
|
||||
use crate::{
|
||||
database::dtos::scheduled_transaction::RecurringTransactionRow,
|
||||
database::{
|
||||
common::{Delete, Upsert},
|
||||
dtos::recurring_transaction::RecurringTransactionRow,
|
||||
recurring_transactions::{
|
||||
delete_scheduled_transaction_by_id, query_scheduled_transactions,
|
||||
upsert_scheduled_transaction,
|
||||
},
|
||||
},
|
||||
error::AppError,
|
||||
routes::dtos::scheduled_transaction::{
|
||||
RecurringTransaction, ScheduledTransaction, ScheduledTransactionData, WeekendAdjustment,
|
||||
RecurringTransaction, ScheduledTransaction, ScheduledTransactionData,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -26,25 +33,7 @@ pub fn create_router() -> axum::Router<PgPool> {
|
||||
async fn get_scheduled_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<Json<Vec<ScheduledTransaction>>, AppError> {
|
||||
let rows = 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(&pool)
|
||||
.await?;
|
||||
let rows = query_scheduled_transactions(&pool).await?;
|
||||
|
||||
let transactions = rows
|
||||
.into_iter()
|
||||
@@ -64,73 +53,25 @@ async fn put_scheduled_transaction(
|
||||
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
||||
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
||||
|
||||
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?"
|
||||
"#,
|
||||
let transaction = RecurringTransactionRow {
|
||||
id,
|
||||
transaction.name,
|
||||
transaction.amount_cents.get(),
|
||||
transaction.direction.as_str(),
|
||||
transaction.starts_at,
|
||||
transaction.ends_at,
|
||||
transaction.recurrence_every.get(),
|
||||
transaction.recurrence_unit.as_str(),
|
||||
transaction
|
||||
name: transaction.name,
|
||||
amount_cents: transaction.amount_cents.get(),
|
||||
direction: transaction.direction.as_str().to_owned(),
|
||||
starts_at: transaction.starts_at,
|
||||
ends_at: transaction.ends_at,
|
||||
recurrence_every: transaction.recurrence_every.get(),
|
||||
recurrence_unit: transaction.recurrence_unit.as_str().to_owned(),
|
||||
weekend_adjustment: transaction
|
||||
.weekend_adjustment
|
||||
.as_ref()
|
||||
.map(WeekendAdjustment::as_str),
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
|
||||
let status = if row.old_id.is_none() {
|
||||
StatusCode::CREATED
|
||||
} else {
|
||||
StatusCode::OK
|
||||
.map(|adjustment| adjustment.as_str().to_owned()),
|
||||
};
|
||||
let transaction = RecurringTransaction::try_from(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,
|
||||
})?;
|
||||
|
||||
let (status, row) = match upsert_scheduled_transaction(&pool, transaction).await? {
|
||||
Upsert::Created(row) => (StatusCode::CREATED, row),
|
||||
Upsert::Updated(row) => (StatusCode::OK, row),
|
||||
};
|
||||
let transaction = RecurringTransaction::try_from(row)?;
|
||||
|
||||
Ok((status, Json(transaction.into())))
|
||||
}
|
||||
@@ -140,16 +81,8 @@ async fn delete_scheduled_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
||||
id
|
||||
)
|
||||
.fetch_optional(&pool)
|
||||
.await?;
|
||||
|
||||
if deleted.is_none() {
|
||||
return Ok(StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
Ok(match delete_scheduled_transaction_by_id(&pool, id).await? {
|
||||
Delete::Deleted => StatusCode::NO_CONTENT,
|
||||
Delete::NotFound => StatusCode::NOT_FOUND,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user