Added source and destination accounts.
This commit is contained in:
-84
@@ -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"
|
||||
}
|
||||
+18
-6
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"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": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -25,26 +25,36 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "source_account_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "destination_account_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "starts_at",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 7,
|
||||
"name": "ends_at",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"ordinal": 8,
|
||||
"name": "recurrence_every",
|
||||
"type_info": "Int4"
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"ordinal": 9,
|
||||
"name": "recurrence_unit",
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"ordinal": 10,
|
||||
"name": "weekend_adjustment",
|
||||
"type_info": "Text"
|
||||
}
|
||||
@@ -60,6 +70,8 @@
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
@@ -67,5 +79,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28"
|
||||
"hash": "a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426"
|
||||
}
|
||||
+98
@@ -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"
|
||||
}
|
||||
+18
-6
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"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": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -25,26 +25,36 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "source_account_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "destination_account_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "starts_at",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 7,
|
||||
"name": "ends_at",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"ordinal": 8,
|
||||
"name": "recurrence_every",
|
||||
"type_info": "Int4"
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"ordinal": 9,
|
||||
"name": "recurrence_unit",
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"ordinal": 10,
|
||||
"name": "weekend_adjustment",
|
||||
"type_info": "Text"
|
||||
}
|
||||
@@ -59,6 +69,8 @@
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
@@ -66,5 +78,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f"
|
||||
"hash": "fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4"
|
||||
}
|
||||
@@ -3,6 +3,8 @@ CREATE TABLE recurring_transactions (
|
||||
name TEXT NOT NULL,
|
||||
amount_cents BIGINT NOT NULL CHECK (amount_cents >= 0),
|
||||
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,
|
||||
ends_at TIMESTAMPTZ,
|
||||
recurrence_every INTEGER NOT NULL CHECK (recurrence_every > 0),
|
||||
|
||||
@@ -2,3 +2,23 @@ pub mod accounts;
|
||||
pub mod dtos;
|
||||
pub mod recurring_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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::dtos::{AccountRow, Delete, Upsert};
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{AccountRow, Delete, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -38,9 +41,10 @@ where
|
||||
)
|
||||
.fetch_all(executor)
|
||||
.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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -55,12 +59,13 @@ where
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
account: AccountRow,
|
||||
) -> Result<Upsert<AccountRow>, sqlx::Error>
|
||||
) -> Result<Upsert<AccountRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -79,7 +84,8 @@ where
|
||||
account.name,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let account = AccountRow {
|
||||
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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
|
||||
@@ -6,6 +6,8 @@ pub struct RecurringTransactionRow {
|
||||
pub name: String,
|
||||
pub amount_cents: i64,
|
||||
pub direction: String,
|
||||
pub source_account_id: Option<Uuid>,
|
||||
pub destination_account_id: Option<Uuid>,
|
||||
pub starts_at: DateTime<Utc>,
|
||||
pub ends_at: Option<DateTime<Utc>>,
|
||||
pub recurrence_every: i32,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::dtos::{Delete, RecurringTransactionRow, Upsert};
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, RecurringTransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub limit: Option<i64>,
|
||||
@@ -19,7 +22,7 @@ impl Default for Query {
|
||||
pub async fn query<'e, E>(
|
||||
executor: E,
|
||||
query: Query,
|
||||
) -> Result<Vec<RecurringTransactionRow>, sqlx::Error>
|
||||
) -> Result<Vec<RecurringTransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -31,6 +34,8 @@ where
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
source_account_id,
|
||||
destination_account_id,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
@@ -45,12 +50,13 @@ where
|
||||
)
|
||||
.fetch_all(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransactionRow>, sqlx::Error>
|
||||
) -> Result<Option<RecurringTransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -62,6 +68,8 @@ where
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
source_account_id,
|
||||
destination_account_id,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
@@ -74,12 +82,13 @@ where
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
transaction: RecurringTransactionRow,
|
||||
) -> Result<Upsert<RecurringTransactionRow>, sqlx::Error>
|
||||
) -> Result<Upsert<RecurringTransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -90,17 +99,21 @@ where
|
||||
name,
|
||||
amount_cents,
|
||||
direction,
|
||||
source_account_id,
|
||||
destination_account_id,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
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
|
||||
name = EXCLUDED.name,
|
||||
amount_cents = EXCLUDED.amount_cents,
|
||||
direction = EXCLUDED.direction,
|
||||
source_account_id = EXCLUDED.source_account_id,
|
||||
destination_account_id = EXCLUDED.destination_account_id,
|
||||
starts_at = EXCLUDED.starts_at,
|
||||
ends_at = EXCLUDED.ends_at,
|
||||
recurrence_every = EXCLUDED.recurrence_every,
|
||||
@@ -112,6 +125,8 @@ where
|
||||
new_row.name AS "name!",
|
||||
new_row.amount_cents AS "amount_cents!",
|
||||
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.ends_at AS "ends_at?",
|
||||
new_row.recurrence_every AS "recurrence_every!",
|
||||
@@ -122,6 +137,8 @@ where
|
||||
transaction.name,
|
||||
transaction.amount_cents,
|
||||
transaction.direction,
|
||||
transaction.source_account_id,
|
||||
transaction.destination_account_id,
|
||||
transaction.starts_at,
|
||||
transaction.ends_at,
|
||||
transaction.recurrence_every,
|
||||
@@ -129,13 +146,16 @@ where
|
||||
transaction.weekend_adjustment,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
source_account_id: row.source_account_id,
|
||||
destination_account_id: row.destination_account_id,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -159,7 +179,8 @@ where
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::dtos::{Delete, TransactionRow, Upsert};
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, TransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -34,9 +37,13 @@ where
|
||||
)
|
||||
.fetch_all(executor)
|
||||
.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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -51,12 +58,13 @@ where
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
transaction: TransactionRow,
|
||||
) -> Result<Upsert<TransactionRow>, sqlx::Error>
|
||||
) -> Result<Upsert<TransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -84,7 +92,8 @@ where
|
||||
transaction.date,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = TransactionRow {
|
||||
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
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
|
||||
@@ -8,5 +8,7 @@ pub struct RecurringTransaction {
|
||||
pub name: String,
|
||||
pub amount: Cents,
|
||||
pub direction: Direction,
|
||||
pub source_account_id: Option<Uuid>,
|
||||
pub destination_account_id: Option<Uuid>,
|
||||
pub schedule: Schedule,
|
||||
}
|
||||
|
||||
+4
-1
@@ -3,7 +3,7 @@ use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use crate::{domain::DomainError, repository::RepositoryError};
|
||||
use crate::{database::DatabaseError, domain::DomainError, repository::RepositoryError};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
@@ -32,6 +32,9 @@ impl IntoResponse for AppError {
|
||||
Self::Domain(error) => {
|
||||
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)) => {
|
||||
tracing::error!(%error, "Database query failed");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use crate::domain::{CentsError, ScheduleError};
|
||||
use crate::{
|
||||
database::DatabaseError,
|
||||
domain::{CentsError, ScheduleError},
|
||||
};
|
||||
|
||||
pub mod accounts;
|
||||
pub mod dtos;
|
||||
@@ -9,7 +12,7 @@ pub mod transactions;
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RepositoryError {
|
||||
#[error(transparent)]
|
||||
Database(#[from] sqlx::Error),
|
||||
Database(#[from] DatabaseError),
|
||||
|
||||
#[error(transparent)]
|
||||
Decode(#[from] DecodeError),
|
||||
|
||||
@@ -109,6 +109,8 @@ fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeEr
|
||||
name: row.name,
|
||||
amount: Cents::try_from(row.amount_cents)?,
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
@@ -121,6 +123,8 @@ fn encode(transaction: RecurringTransaction) -> RecurringTransactionRow {
|
||||
name: transaction.name,
|
||||
amount_cents: transaction.amount.into(),
|
||||
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(),
|
||||
ends_at: transaction.schedule.end(),
|
||||
recurrence_every: recurrence.every.into(),
|
||||
@@ -146,6 +150,8 @@ mod tests {
|
||||
name: "Rent".to_owned(),
|
||||
amount_cents: 120_000,
|
||||
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(),
|
||||
ends_at: None,
|
||||
recurrence_every: 1,
|
||||
@@ -157,7 +163,12 @@ mod tests {
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
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 recurrence = transaction.schedule.recurrence();
|
||||
|
||||
@@ -165,6 +176,8 @@ mod tests {
|
||||
assert_eq!(transaction.name, "Rent");
|
||||
assert_eq!(transaction.amount.get(), 120_000);
|
||||
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.end(), None);
|
||||
assert_eq!(recurrence.every.get(), 1);
|
||||
|
||||
@@ -30,6 +30,8 @@ pub struct RecurringTransactionRequest {
|
||||
pub name: String,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub source_account_id: Option<Uuid>,
|
||||
pub destination_account_id: Option<Uuid>,
|
||||
pub schedule: ScheduleRequest,
|
||||
}
|
||||
|
||||
@@ -48,6 +50,8 @@ impl RecurringTransactionRequest {
|
||||
name: self.name,
|
||||
amount: domain::Cents::try_from(self.amount_cents)?,
|
||||
direction: self.direction.into(),
|
||||
source_account_id: self.source_account_id,
|
||||
destination_account_id: self.destination_account_id,
|
||||
schedule: domain::Schedule::new(
|
||||
self.schedule.start,
|
||||
self.schedule.end,
|
||||
@@ -64,6 +68,8 @@ pub struct RecurringTransactionResponse {
|
||||
pub name: String,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub source_account_id: Option<Uuid>,
|
||||
pub destination_account_id: Option<Uuid>,
|
||||
pub schedule: ScheduleResponse,
|
||||
}
|
||||
|
||||
@@ -74,6 +80,8 @@ impl RecurringTransactionResponse {
|
||||
name: transaction.name,
|
||||
amount_cents: transaction.amount.get(),
|
||||
direction: transaction.direction.into(),
|
||||
source_account_id: transaction.source_account_id,
|
||||
destination_account_id: transaction.destination_account_id,
|
||||
schedule: ScheduleResponse::new(transaction.schedule, next),
|
||||
}
|
||||
}
|
||||
@@ -199,6 +207,8 @@ mod tests {
|
||||
name: String::new(),
|
||||
amount_cents,
|
||||
direction: Direction::Out,
|
||||
source_account_id: None,
|
||||
destination_account_id: None,
|
||||
schedule: ScheduleRequest {
|
||||
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
||||
end: None,
|
||||
|
||||
Reference in New Issue
Block a user