Refactored DB operations into dedicated module.
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n WHERE id = $1\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "amount_cents",
|
||||||
|
"type_info": "Int8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "direction",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "starts_at",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "ends_at",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "recurrence_every",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "recurrence_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "weekend_adjustment",
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f"
|
||||||
|
}
|
||||||
@@ -1 +1,3 @@
|
|||||||
|
pub mod common;
|
||||||
pub mod dtos;
|
pub mod dtos;
|
||||||
|
pub mod recurring_transactions;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
pub enum Upsert<T> {
|
||||||
|
Created(T),
|
||||||
|
Updated(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Delete {
|
||||||
|
Deleted,
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod scheduled_transaction;
|
pub mod recurring_transaction;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
|
|||||||
@@ -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
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
database::dtos::scheduled_transaction::RecurringTransactionRow,
|
database::dtos::recurring_transaction::RecurringTransactionRow,
|
||||||
routes::dtos::direction::{Direction, DirectionConversionError},
|
routes::dtos::direction::{Direction, DirectionConversionError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,17 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use super::validation::scheduled_transaction::ValidatedRecurringTransaction;
|
use super::validation::scheduled_transaction::ValidatedRecurringTransaction;
|
||||||
use crate::{
|
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,
|
error::AppError,
|
||||||
routes::dtos::scheduled_transaction::{
|
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(
|
async fn get_scheduled_transactions(
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
) -> Result<Json<Vec<ScheduledTransaction>>, AppError> {
|
) -> Result<Json<Vec<ScheduledTransaction>>, AppError> {
|
||||||
let rows = sqlx::query_as!(
|
let rows = query_scheduled_transactions(&pool).await?;
|
||||||
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 transactions = rows
|
let transactions = rows
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -64,73 +53,25 @@ async fn put_scheduled_transaction(
|
|||||||
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
||||||
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
||||||
|
|
||||||
let row = sqlx::query!(
|
let transaction = RecurringTransactionRow {
|
||||||
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?"
|
|
||||||
"#,
|
|
||||||
id,
|
id,
|
||||||
transaction.name,
|
name: transaction.name,
|
||||||
transaction.amount_cents.get(),
|
amount_cents: transaction.amount_cents.get(),
|
||||||
transaction.direction.as_str(),
|
direction: transaction.direction.as_str().to_owned(),
|
||||||
transaction.starts_at,
|
starts_at: transaction.starts_at,
|
||||||
transaction.ends_at,
|
ends_at: transaction.ends_at,
|
||||||
transaction.recurrence_every.get(),
|
recurrence_every: transaction.recurrence_every.get(),
|
||||||
transaction.recurrence_unit.as_str(),
|
recurrence_unit: transaction.recurrence_unit.as_str().to_owned(),
|
||||||
transaction
|
weekend_adjustment: transaction
|
||||||
.weekend_adjustment
|
.weekend_adjustment
|
||||||
.as_ref()
|
.map(|adjustment| adjustment.as_str().to_owned()),
|
||||||
.map(WeekendAdjustment::as_str),
|
|
||||||
)
|
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let status = if row.old_id.is_none() {
|
|
||||||
StatusCode::CREATED
|
|
||||||
} else {
|
|
||||||
StatusCode::OK
|
|
||||||
};
|
};
|
||||||
let transaction = RecurringTransaction::try_from(RecurringTransactionRow {
|
|
||||||
id: row.id,
|
let (status, row) = match upsert_scheduled_transaction(&pool, transaction).await? {
|
||||||
name: row.name,
|
Upsert::Created(row) => (StatusCode::CREATED, row),
|
||||||
amount_cents: row.amount_cents,
|
Upsert::Updated(row) => (StatusCode::OK, row),
|
||||||
direction: row.direction,
|
};
|
||||||
starts_at: row.starts_at,
|
let transaction = RecurringTransaction::try_from(row)?;
|
||||||
ends_at: row.ends_at,
|
|
||||||
recurrence_every: row.recurrence_every,
|
|
||||||
recurrence_unit: row.recurrence_unit,
|
|
||||||
weekend_adjustment: row.weekend_adjustment,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok((status, Json(transaction.into())))
|
Ok((status, Json(transaction.into())))
|
||||||
}
|
}
|
||||||
@@ -140,16 +81,8 @@ async fn delete_scheduled_transaction(
|
|||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
let deleted = sqlx::query!(
|
Ok(match delete_scheduled_transaction_by_id(&pool, id).await? {
|
||||||
"DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
Delete::Deleted => StatusCode::NO_CONTENT,
|
||||||
id
|
Delete::NotFound => StatusCode::NOT_FOUND,
|
||||||
)
|
})
|
||||||
.fetch_optional(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if deleted.is_none() {
|
|
||||||
return Ok(StatusCode::NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(StatusCode::NO_CONTENT)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user