Added source and destination accounts.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user