Implemented a domain layer, because the API DTO's where getting bloated.

This commit is contained in:
2026-09-26 17:22:10 +02:00
parent 04debfba7b
commit 4737bd5c81
24 changed files with 1103 additions and 538 deletions
+125
View File
@@ -0,0 +1,125 @@
use uuid::Uuid;
use super::{
DecodeError, Delete, RepositoryError, Upsert,
encoding::{decode_direction, encode_direction},
};
use crate::{
database::{dtos::transaction::TransactionRow, transactions as db},
domain::{Cents, Transaction},
};
pub async fn list<'e, E>(executor: E) -> Result<Vec<Transaction>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let transactions = db::query_transactions(executor)
.await?
.into_iter()
.map(decode)
.collect::<Result<_, DecodeError>>()?;
Ok(transactions)
}
pub async fn get<'e, E>(executor: E, id: Uuid) -> Result<Option<Transaction>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let transaction = db::get_transaction_by_id(executor, id)
.await?
.map(decode)
.transpose()?;
Ok(transaction)
}
pub async fn upsert<'e, E>(
executor: E,
transaction: Transaction,
) -> Result<Upsert<Transaction>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let transaction = db::upsert_transaction(executor, encode(transaction))
.await?
.try_map(decode)?;
Ok(transaction)
}
pub async fn delete<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
Ok(db::delete_transaction_by_id(executor, id).await?)
}
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
Ok(Transaction {
id: row.id,
description: row.description,
amount: Cents::try_from(row.amount_cents)?,
direction: decode_direction(row.direction)?,
date: row.date,
})
}
fn encode(transaction: Transaction) -> TransactionRow {
TransactionRow {
id: transaction.id,
description: transaction.description,
amount_cents: transaction.amount.into(),
direction: encode_direction(transaction.direction).to_owned(),
date: transaction.date,
}
}
#[cfg(test)]
mod tests {
use chrono::Utc;
use uuid::Uuid;
use super::*;
use crate::domain::Direction;
fn row() -> TransactionRow {
TransactionRow {
id: Uuid::new_v4(),
description: "Groceries".to_owned(),
amount_cents: 4250,
direction: "out".to_owned(),
date: Utc::now(),
}
}
#[test]
fn decodes_valid_rows() {
let row = row();
let (id, date) = (row.id, row.date);
let transaction = decode(row).unwrap();
assert_eq!(transaction.id, id);
assert_eq!(transaction.description, "Groceries");
assert_eq!(transaction.amount.get(), 4250);
assert_eq!(transaction.direction, Direction::Out);
assert_eq!(transaction.date, date);
}
#[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();
assert_eq!(decode(encode(transaction.clone())).unwrap(), transaction);
}
}