209 lines
5.7 KiB
Rust
209 lines
5.7 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{
|
|
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
|
dtos::Upsert,
|
|
encoding::{decode_direction, encode_direction},
|
|
};
|
|
use crate::{
|
|
database::{self, Constraint, DatabaseError, dtos::TransactionRow},
|
|
domain::{Cents, Transaction},
|
|
};
|
|
|
|
pub struct Query {
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
pub async fn list<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
query: Query,
|
|
) -> Result<Vec<Transaction>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let query = database::transactions::Query {
|
|
limit: query
|
|
.limit
|
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
|
.transpose()?,
|
|
offset: query
|
|
.offset
|
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
|
.transpose()?,
|
|
};
|
|
|
|
let transactions = database::transactions::list(executor, user_id, query)
|
|
.await?
|
|
.into_iter()
|
|
.map(decode)
|
|
.collect::<Result<_, _>>()?;
|
|
|
|
Ok(transactions)
|
|
}
|
|
|
|
pub async fn get_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<Transaction>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let transaction = database::transactions::get_by_id(executor, user_id, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(transaction)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
transaction: Transaction,
|
|
) -> Result<Option<Upsert<Transaction>>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let upserted = database::transactions::upsert(executor, user_id, encode(transaction))
|
|
.await
|
|
.map_err(classify_upsert_error)?
|
|
.map(|upserted| upserted.try_map(decode))
|
|
.transpose()?
|
|
.map(Into::into);
|
|
|
|
Ok(upserted)
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<Transaction>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let transaction = database::transactions::delete_by_id(executor, user_id, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(transaction)
|
|
}
|
|
|
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|
match &error {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint: Constraint::TransactionAccount,
|
|
..
|
|
} => RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::Account,
|
|
kind: ReferenceViolationKind::Missing,
|
|
source: error,
|
|
},
|
|
_ => RepositoryError::Database(error),
|
|
}
|
|
}
|
|
|
|
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
|
Ok(Transaction {
|
|
id: row.id,
|
|
account_id: row.account_id,
|
|
amount: Cents::try_from(row.amount_cents)?,
|
|
direction: decode_direction(row.direction)?,
|
|
date: row.date,
|
|
description: row.description,
|
|
})
|
|
}
|
|
|
|
fn encode(transaction: Transaction) -> TransactionRow {
|
|
TransactionRow {
|
|
id: transaction.id,
|
|
account_id: transaction.account_id,
|
|
amount_cents: transaction.amount.into(),
|
|
direction: encode_direction(transaction.direction).to_owned(),
|
|
date: transaction.date,
|
|
description: transaction.description,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use chrono::Utc;
|
|
use uuid::Uuid;
|
|
|
|
use super::*;
|
|
use crate::domain::Direction;
|
|
|
|
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint,
|
|
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn classifies_account_upsert_foreign_key_violation() {
|
|
let error = classify_upsert_error(foreign_key_violation(Constraint::TransactionAccount));
|
|
assert!(matches!(
|
|
error,
|
|
RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::Account,
|
|
kind: ReferenceViolationKind::Missing,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_unknown_upsert_foreign_keys_generic() {
|
|
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
|
assert!(matches!(error, RepositoryError::Database(_)));
|
|
}
|
|
|
|
fn row() -> TransactionRow {
|
|
TransactionRow {
|
|
id: Uuid::new_v4(),
|
|
account_id: Uuid::new_v4(),
|
|
amount_cents: 4250,
|
|
direction: "out".to_owned(),
|
|
date: Utc::now(),
|
|
description: "Groceries".to_owned(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decodes_valid_rows() {
|
|
let row = row();
|
|
let (id, account_id, date) = (row.id, row.account_id, row.date);
|
|
let transaction = decode(row).unwrap();
|
|
|
|
assert_eq!(transaction.id, id);
|
|
assert_eq!(transaction.account_id, account_id);
|
|
assert_eq!(transaction.amount.get(), 4250);
|
|
assert_eq!(transaction.direction, Direction::Out);
|
|
assert_eq!(transaction.date, date);
|
|
assert_eq!(transaction.description, "Groceries");
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_corrupt_rows() {
|
|
let mut negative = row();
|
|
negative.amount_cents = -1;
|
|
assert!(matches!(decode(negative), Err(DecodeError::Amount(_))));
|
|
|
|
let mut direction = row();
|
|
direction.direction = "sideways".to_owned();
|
|
assert!(matches!(decode(direction), Err(DecodeError::Direction(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn round_trips_through_rows() {
|
|
let transaction = decode(row()).unwrap();
|
|
let round_trip = decode(encode(transaction.clone())).unwrap();
|
|
assert_eq!(round_trip, transaction);
|
|
}
|
|
}
|