Added source and destination accounts.

This commit is contained in:
2026-09-27 11:04:47 +02:00
parent d219d63dca
commit 26a370aa31
16 changed files with 253 additions and 122 deletions
@@ -1,84 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO recurring_transactions (\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 )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "name!",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "amount_cents!",
"type_info": "Int8"
},
{
"ordinal": 4,
"name": "direction!",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "starts_at!",
"type_info": "Timestamptz"
},
{
"ordinal": 6,
"name": "ends_at?",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "recurrence_every!",
"type_info": "Int4"
},
{
"ordinal": 8,
"name": "recurrence_unit!",
"type_info": "Text"
},
{
"ordinal": 9,
"name": "weekend_adjustment?",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Uuid",
"Text",
"Int8",
"Text",
"Timestamptz",
"Timestamptz",
"Int4",
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
true,
false,
false,
true
]
},
"hash": "0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1"
}
@@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "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 ORDER BY starts_at DESC, id DESC\n LIMIT $1 OFFSET $2\n ", "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n ORDER BY starts_at DESC, id DESC\n LIMIT $1 OFFSET $2\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -25,26 +25,36 @@
}, },
{ {
"ordinal": 4, "ordinal": 4,
"name": "source_account_id",
"type_info": "Uuid"
},
{
"ordinal": 5,
"name": "destination_account_id",
"type_info": "Uuid"
},
{
"ordinal": 6,
"name": "starts_at", "name": "starts_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
}, },
{ {
"ordinal": 5, "ordinal": 7,
"name": "ends_at", "name": "ends_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
}, },
{ {
"ordinal": 6, "ordinal": 8,
"name": "recurrence_every", "name": "recurrence_every",
"type_info": "Int4" "type_info": "Int4"
}, },
{ {
"ordinal": 7, "ordinal": 9,
"name": "recurrence_unit", "name": "recurrence_unit",
"type_info": "Text" "type_info": "Text"
}, },
{ {
"ordinal": 8, "ordinal": 10,
"name": "weekend_adjustment", "name": "weekend_adjustment",
"type_info": "Text" "type_info": "Text"
} }
@@ -60,6 +70,8 @@
false, false,
false, false,
false, false,
true,
true,
false, false,
true, true,
false, false,
@@ -67,5 +79,5 @@
true true
] ]
}, },
"hash": "b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28" "hash": "a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426"
} }
@@ -0,0 +1,98 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO recurring_transactions (\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n source_account_id = EXCLUDED.source_account_id,\n destination_account_id = EXCLUDED.destination_account_id,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.source_account_id AS \"source_account_id?\",\n new_row.destination_account_id AS \"destination_account_id?\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "name!",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "amount_cents!",
"type_info": "Int8"
},
{
"ordinal": 4,
"name": "direction!",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "source_account_id?",
"type_info": "Uuid"
},
{
"ordinal": 6,
"name": "destination_account_id?",
"type_info": "Uuid"
},
{
"ordinal": 7,
"name": "starts_at!",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "ends_at?",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "recurrence_every!",
"type_info": "Int4"
},
{
"ordinal": 10,
"name": "recurrence_unit!",
"type_info": "Text"
},
{
"ordinal": 11,
"name": "weekend_adjustment?",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Uuid",
"Text",
"Int8",
"Text",
"Uuid",
"Uuid",
"Timestamptz",
"Timestamptz",
"Int4",
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
true,
true,
false,
true,
false,
false,
true
]
},
"hash": "cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4"
}
@@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "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 ", "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\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": { "describe": {
"columns": [ "columns": [
{ {
@@ -25,26 +25,36 @@
}, },
{ {
"ordinal": 4, "ordinal": 4,
"name": "source_account_id",
"type_info": "Uuid"
},
{
"ordinal": 5,
"name": "destination_account_id",
"type_info": "Uuid"
},
{
"ordinal": 6,
"name": "starts_at", "name": "starts_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
}, },
{ {
"ordinal": 5, "ordinal": 7,
"name": "ends_at", "name": "ends_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
}, },
{ {
"ordinal": 6, "ordinal": 8,
"name": "recurrence_every", "name": "recurrence_every",
"type_info": "Int4" "type_info": "Int4"
}, },
{ {
"ordinal": 7, "ordinal": 9,
"name": "recurrence_unit", "name": "recurrence_unit",
"type_info": "Text" "type_info": "Text"
}, },
{ {
"ordinal": 8, "ordinal": 10,
"name": "weekend_adjustment", "name": "weekend_adjustment",
"type_info": "Text" "type_info": "Text"
} }
@@ -59,6 +69,8 @@
false, false,
false, false,
false, false,
true,
true,
false, false,
true, true,
false, false,
@@ -66,5 +78,5 @@
true true
] ]
}, },
"hash": "1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f" "hash": "fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4"
} }
@@ -3,6 +3,8 @@ CREATE TABLE recurring_transactions (
name TEXT NOT NULL, name TEXT NOT NULL,
amount_cents BIGINT NOT NULL CHECK (amount_cents >= 0), amount_cents BIGINT NOT NULL CHECK (amount_cents >= 0),
direction TEXT NOT NULL CHECK (direction IN ('in', 'out')), direction TEXT NOT NULL CHECK (direction IN ('in', 'out')),
source_account_id UUID REFERENCES accounts (id),
destination_account_id UUID REFERENCES accounts (id),
starts_at TIMESTAMPTZ NOT NULL, starts_at TIMESTAMPTZ NOT NULL,
ends_at TIMESTAMPTZ, ends_at TIMESTAMPTZ,
recurrence_every INTEGER NOT NULL CHECK (recurrence_every > 0), recurrence_every INTEGER NOT NULL CHECK (recurrence_every > 0),
+20
View File
@@ -2,3 +2,23 @@ pub mod accounts;
pub mod dtos; pub mod dtos;
pub mod recurring_transactions; pub mod recurring_transactions;
pub mod transactions; pub mod transactions;
#[derive(Debug, thiserror::Error)]
pub enum DatabaseError {
#[error("Foreign key violation")]
ForeignKeyViolation,
#[error("Internal error: {0}")]
Internal(#[source] sqlx::Error),
}
impl From<sqlx::Error> for DatabaseError {
fn from(err: sqlx::Error) -> Self {
match err {
sqlx::Error::Database(db_err) if db_err.code().as_deref() == Some("23503") => {
DatabaseError::ForeignKeyViolation
}
_ => DatabaseError::Internal(err),
}
}
}
+14 -7
View File
@@ -1,6 +1,9 @@
use uuid::Uuid; use uuid::Uuid;
use super::dtos::{AccountRow, Delete, Upsert}; use super::{
DatabaseError,
dtos::{AccountRow, Delete, Upsert},
};
pub struct Query { pub struct Query {
pub name: Option<String>, pub name: Option<String>,
@@ -18,7 +21,7 @@ impl Default for Query {
} }
} }
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<AccountRow>, sqlx::Error> pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<AccountRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -38,9 +41,10 @@ where
) )
.fetch_all(executor) .fetch_all(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, sqlx::Error> pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -55,12 +59,13 @@ where
) )
.fetch_optional(executor) .fetch_optional(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn upsert<'e, E>( pub async fn upsert<'e, E>(
executor: E, executor: E,
account: AccountRow, account: AccountRow,
) -> Result<Upsert<AccountRow>, sqlx::Error> ) -> Result<Upsert<AccountRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -79,7 +84,8 @@ where
account.name, account.name,
) )
.fetch_one(executor) .fetch_one(executor)
.await?; .await
.map_err(DatabaseError::from)?;
let account = AccountRow { let account = AccountRow {
id: row.id, id: row.id,
@@ -93,13 +99,14 @@ where
} }
} }
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, sqlx::Error> pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id) let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id)
.fetch_optional(executor) .fetch_optional(executor)
.await?; .await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() { Ok(if deleted.is_some() {
Delete::Deleted Delete::Deleted
@@ -6,6 +6,8 @@ pub struct RecurringTransactionRow {
pub name: String, pub name: String,
pub amount_cents: i64, pub amount_cents: i64,
pub direction: String, pub direction: String,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub starts_at: DateTime<Utc>, pub starts_at: DateTime<Utc>,
pub ends_at: Option<DateTime<Utc>>, pub ends_at: Option<DateTime<Utc>>,
pub recurrence_every: i32, pub recurrence_every: i32,
+29 -8
View File
@@ -1,6 +1,9 @@
use uuid::Uuid; use uuid::Uuid;
use super::dtos::{Delete, RecurringTransactionRow, Upsert}; use super::{
DatabaseError,
dtos::{Delete, RecurringTransactionRow, Upsert},
};
pub struct Query { pub struct Query {
pub limit: Option<i64>, pub limit: Option<i64>,
@@ -19,7 +22,7 @@ impl Default for Query {
pub async fn query<'e, E>( pub async fn query<'e, E>(
executor: E, executor: E,
query: Query, query: Query,
) -> Result<Vec<RecurringTransactionRow>, sqlx::Error> ) -> Result<Vec<RecurringTransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -31,6 +34,8 @@ where
name, name,
amount_cents, amount_cents,
direction, direction,
source_account_id,
destination_account_id,
starts_at, starts_at,
ends_at, ends_at,
recurrence_every, recurrence_every,
@@ -45,12 +50,13 @@ where
) )
.fetch_all(executor) .fetch_all(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn get_by_id<'e, E>( pub async fn get_by_id<'e, E>(
executor: E, executor: E,
id: Uuid, id: Uuid,
) -> Result<Option<RecurringTransactionRow>, sqlx::Error> ) -> Result<Option<RecurringTransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -62,6 +68,8 @@ where
name, name,
amount_cents, amount_cents,
direction, direction,
source_account_id,
destination_account_id,
starts_at, starts_at,
ends_at, ends_at,
recurrence_every, recurrence_every,
@@ -74,12 +82,13 @@ where
) )
.fetch_optional(executor) .fetch_optional(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn upsert<'e, E>( pub async fn upsert<'e, E>(
executor: E, executor: E,
transaction: RecurringTransactionRow, transaction: RecurringTransactionRow,
) -> Result<Upsert<RecurringTransactionRow>, sqlx::Error> ) -> Result<Upsert<RecurringTransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -90,17 +99,21 @@ where
name, name,
amount_cents, amount_cents,
direction, direction,
source_account_id,
destination_account_id,
starts_at, starts_at,
ends_at, ends_at,
recurrence_every, recurrence_every,
recurrence_unit, recurrence_unit,
weekend_adjustment weekend_adjustment
) )
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (id) DO UPDATE SET ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name, name = EXCLUDED.name,
amount_cents = EXCLUDED.amount_cents, amount_cents = EXCLUDED.amount_cents,
direction = EXCLUDED.direction, direction = EXCLUDED.direction,
source_account_id = EXCLUDED.source_account_id,
destination_account_id = EXCLUDED.destination_account_id,
starts_at = EXCLUDED.starts_at, starts_at = EXCLUDED.starts_at,
ends_at = EXCLUDED.ends_at, ends_at = EXCLUDED.ends_at,
recurrence_every = EXCLUDED.recurrence_every, recurrence_every = EXCLUDED.recurrence_every,
@@ -112,6 +125,8 @@ where
new_row.name AS "name!", new_row.name AS "name!",
new_row.amount_cents AS "amount_cents!", new_row.amount_cents AS "amount_cents!",
new_row.direction AS "direction!", new_row.direction AS "direction!",
new_row.source_account_id AS "source_account_id?",
new_row.destination_account_id AS "destination_account_id?",
new_row.starts_at AS "starts_at!", new_row.starts_at AS "starts_at!",
new_row.ends_at AS "ends_at?", new_row.ends_at AS "ends_at?",
new_row.recurrence_every AS "recurrence_every!", new_row.recurrence_every AS "recurrence_every!",
@@ -122,6 +137,8 @@ where
transaction.name, transaction.name,
transaction.amount_cents, transaction.amount_cents,
transaction.direction, transaction.direction,
transaction.source_account_id,
transaction.destination_account_id,
transaction.starts_at, transaction.starts_at,
transaction.ends_at, transaction.ends_at,
transaction.recurrence_every, transaction.recurrence_every,
@@ -129,13 +146,16 @@ where
transaction.weekend_adjustment, transaction.weekend_adjustment,
) )
.fetch_one(executor) .fetch_one(executor)
.await?; .await
.map_err(DatabaseError::from)?;
let transaction = RecurringTransactionRow { let transaction = RecurringTransactionRow {
id: row.id, id: row.id,
name: row.name, name: row.name,
amount_cents: row.amount_cents, amount_cents: row.amount_cents,
direction: row.direction, direction: row.direction,
source_account_id: row.source_account_id,
destination_account_id: row.destination_account_id,
starts_at: row.starts_at, starts_at: row.starts_at,
ends_at: row.ends_at, ends_at: row.ends_at,
recurrence_every: row.recurrence_every, recurrence_every: row.recurrence_every,
@@ -150,7 +170,7 @@ where
} }
} }
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, sqlx::Error> pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -159,7 +179,8 @@ where
id, id,
) )
.fetch_optional(executor) .fetch_optional(executor)
.await?; .await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() { Ok(if deleted.is_some() {
Delete::Deleted Delete::Deleted
+17 -7
View File
@@ -1,6 +1,9 @@
use uuid::Uuid; use uuid::Uuid;
use super::dtos::{Delete, TransactionRow, Upsert}; use super::{
DatabaseError,
dtos::{Delete, TransactionRow, Upsert},
};
pub struct Query { pub struct Query {
pub limit: Option<i64>, pub limit: Option<i64>,
@@ -16,7 +19,7 @@ impl Default for Query {
} }
} }
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<TransactionRow>, sqlx::Error> pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<TransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -34,9 +37,13 @@ where
) )
.fetch_all(executor) .fetch_all(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<TransactionRow>, sqlx::Error> pub async fn get_by_id<'e, E>(
executor: E,
id: Uuid,
) -> Result<Option<TransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -51,12 +58,13 @@ where
) )
.fetch_optional(executor) .fetch_optional(executor)
.await .await
.map_err(DatabaseError::from)
} }
pub async fn upsert<'e, E>( pub async fn upsert<'e, E>(
executor: E, executor: E,
transaction: TransactionRow, transaction: TransactionRow,
) -> Result<Upsert<TransactionRow>, sqlx::Error> ) -> Result<Upsert<TransactionRow>, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
@@ -84,7 +92,8 @@ where
transaction.date, transaction.date,
) )
.fetch_one(executor) .fetch_one(executor)
.await?; .await
.map_err(DatabaseError::from)?;
let transaction = TransactionRow { let transaction = TransactionRow {
id: row.id, id: row.id,
@@ -101,13 +110,14 @@ where
} }
} }
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, sqlx::Error> pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{ {
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id) let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
.fetch_optional(executor) .fetch_optional(executor)
.await?; .await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() { Ok(if deleted.is_some() {
Delete::Deleted Delete::Deleted
@@ -8,5 +8,7 @@ pub struct RecurringTransaction {
pub name: String, pub name: String,
pub amount: Cents, pub amount: Cents,
pub direction: Direction, pub direction: Direction,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub schedule: Schedule, pub schedule: Schedule,
} }
+4 -1
View File
@@ -3,7 +3,7 @@ use axum::{
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use crate::{domain::DomainError, repository::RepositoryError}; use crate::{database::DatabaseError, domain::DomainError, repository::RepositoryError};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum AppError { pub enum AppError {
@@ -32,6 +32,9 @@ impl IntoResponse for AppError {
Self::Domain(error) => { Self::Domain(error) => {
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
} }
Self::Repository(RepositoryError::Database(DatabaseError::ForeignKeyViolation)) => {
return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Reference violation");
}
Self::Repository(RepositoryError::Database(error)) => { Self::Repository(RepositoryError::Database(error)) => {
tracing::error!(%error, "Database query failed"); tracing::error!(%error, "Database query failed");
} }
+5 -2
View File
@@ -1,4 +1,7 @@
use crate::domain::{CentsError, ScheduleError}; use crate::{
database::DatabaseError,
domain::{CentsError, ScheduleError},
};
pub mod accounts; pub mod accounts;
pub mod dtos; pub mod dtos;
@@ -9,7 +12,7 @@ pub mod transactions;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum RepositoryError { pub enum RepositoryError {
#[error(transparent)] #[error(transparent)]
Database(#[from] sqlx::Error), Database(#[from] DatabaseError),
#[error(transparent)] #[error(transparent)]
Decode(#[from] DecodeError), Decode(#[from] DecodeError),
@@ -109,6 +109,8 @@ fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeEr
name: row.name, name: row.name,
amount: Cents::try_from(row.amount_cents)?, amount: Cents::try_from(row.amount_cents)?,
direction: decode_direction(row.direction)?, direction: decode_direction(row.direction)?,
source_account_id: row.source_account_id,
destination_account_id: row.destination_account_id,
schedule: Schedule::new(row.starts_at, row.ends_at, recurrence, weekend_adjustment)?, schedule: Schedule::new(row.starts_at, row.ends_at, recurrence, weekend_adjustment)?,
}) })
} }
@@ -121,6 +123,8 @@ fn encode(transaction: RecurringTransaction) -> RecurringTransactionRow {
name: transaction.name, name: transaction.name,
amount_cents: transaction.amount.into(), amount_cents: transaction.amount.into(),
direction: encode_direction(transaction.direction).to_owned(), direction: encode_direction(transaction.direction).to_owned(),
source_account_id: transaction.source_account_id,
destination_account_id: transaction.destination_account_id,
starts_at: transaction.schedule.start(), starts_at: transaction.schedule.start(),
ends_at: transaction.schedule.end(), ends_at: transaction.schedule.end(),
recurrence_every: recurrence.every.into(), recurrence_every: recurrence.every.into(),
@@ -146,6 +150,8 @@ mod tests {
name: "Rent".to_owned(), name: "Rent".to_owned(),
amount_cents: 120_000, amount_cents: 120_000,
direction: "out".to_owned(), direction: "out".to_owned(),
source_account_id: Some(Uuid::new_v4()),
destination_account_id: Some(Uuid::new_v4()),
starts_at: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(), starts_at: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
ends_at: None, ends_at: None,
recurrence_every: 1, recurrence_every: 1,
@@ -157,7 +163,12 @@ mod tests {
#[test] #[test]
fn decodes_valid_rows() { fn decodes_valid_rows() {
let row = row(); let row = row();
let (id, start) = (row.id, row.starts_at); let (id, start, source_account_id, destination_account_id) = (
row.id,
row.starts_at,
row.source_account_id,
row.destination_account_id,
);
let transaction = decode(row).unwrap(); let transaction = decode(row).unwrap();
let recurrence = transaction.schedule.recurrence(); let recurrence = transaction.schedule.recurrence();
@@ -165,6 +176,8 @@ mod tests {
assert_eq!(transaction.name, "Rent"); assert_eq!(transaction.name, "Rent");
assert_eq!(transaction.amount.get(), 120_000); assert_eq!(transaction.amount.get(), 120_000);
assert_eq!(transaction.direction, Direction::Out); assert_eq!(transaction.direction, Direction::Out);
assert_eq!(transaction.source_account_id, source_account_id);
assert_eq!(transaction.destination_account_id, destination_account_id);
assert_eq!(transaction.schedule.start(), start); assert_eq!(transaction.schedule.start(), start);
assert_eq!(transaction.schedule.end(), None); assert_eq!(transaction.schedule.end(), None);
assert_eq!(recurrence.every.get(), 1); assert_eq!(recurrence.every.get(), 1);
@@ -30,6 +30,8 @@ pub struct RecurringTransactionRequest {
pub name: String, pub name: String,
pub amount_cents: u64, pub amount_cents: u64,
pub direction: Direction, pub direction: Direction,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub schedule: ScheduleRequest, pub schedule: ScheduleRequest,
} }
@@ -48,6 +50,8 @@ impl RecurringTransactionRequest {
name: self.name, name: self.name,
amount: domain::Cents::try_from(self.amount_cents)?, amount: domain::Cents::try_from(self.amount_cents)?,
direction: self.direction.into(), direction: self.direction.into(),
source_account_id: self.source_account_id,
destination_account_id: self.destination_account_id,
schedule: domain::Schedule::new( schedule: domain::Schedule::new(
self.schedule.start, self.schedule.start,
self.schedule.end, self.schedule.end,
@@ -64,6 +68,8 @@ pub struct RecurringTransactionResponse {
pub name: String, pub name: String,
pub amount_cents: u64, pub amount_cents: u64,
pub direction: Direction, pub direction: Direction,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub schedule: ScheduleResponse, pub schedule: ScheduleResponse,
} }
@@ -74,6 +80,8 @@ impl RecurringTransactionResponse {
name: transaction.name, name: transaction.name,
amount_cents: transaction.amount.get(), amount_cents: transaction.amount.get(),
direction: transaction.direction.into(), direction: transaction.direction.into(),
source_account_id: transaction.source_account_id,
destination_account_id: transaction.destination_account_id,
schedule: ScheduleResponse::new(transaction.schedule, next), schedule: ScheduleResponse::new(transaction.schedule, next),
} }
} }
@@ -199,6 +207,8 @@ mod tests {
name: String::new(), name: String::new(),
amount_cents, amount_cents,
direction: Direction::Out, direction: Direction::Out,
source_account_id: None,
destination_account_id: None,
schedule: ScheduleRequest { schedule: ScheduleRequest {
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(), start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
end: None, end: None,