Implemented a domain layer, because the API DTO's where getting bloated.
This commit is contained in:
@@ -3,18 +3,11 @@ use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::{
|
||||
common::{Delete, Upsert},
|
||||
dtos::transaction::TransactionRow,
|
||||
transactions::{
|
||||
delete_transaction_by_id, get_transaction_by_id, query_transactions, upsert_transaction,
|
||||
},
|
||||
},
|
||||
error::AppError,
|
||||
repository::{Delete, Upsert, transactions as repository},
|
||||
routes::{
|
||||
dtos::transaction::{Transaction, TransactionData},
|
||||
extractors::{json::Json, path::Path},
|
||||
validation::transaction::ValidatedTransaction,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -32,12 +25,11 @@ pub fn create_router() -> axum::Router<PgPool> {
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_transactions(State(pool): State<PgPool>) -> Result<Json<Vec<Transaction>>, AppError> {
|
||||
let rows = query_transactions(&pool).await?;
|
||||
|
||||
let transactions = rows
|
||||
let transactions = repository::list(&pool)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(Transaction::try_from)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.map(Transaction::from)
|
||||
.collect();
|
||||
|
||||
Ok(Json(transactions))
|
||||
}
|
||||
@@ -48,15 +40,11 @@ async fn get_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<Transaction>, AppError> {
|
||||
let transaction = get_transaction_by_id(&pool, id)
|
||||
let transaction = repository::get(&pool, id)
|
||||
.await?
|
||||
.map(Transaction::try_from)
|
||||
.transpose()?;
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
match transaction {
|
||||
Some(transaction) => Ok(Json(transaction)),
|
||||
None => Err(AppError::NotFound),
|
||||
}
|
||||
Ok(Json(transaction.into()))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -66,22 +54,14 @@ async fn put_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
Json(transaction): Json<TransactionData>,
|
||||
) -> Result<(StatusCode, Json<Transaction>), AppError> {
|
||||
let transaction = ValidatedTransaction::try_from(transaction)?;
|
||||
let transaction = TransactionRow {
|
||||
id,
|
||||
description: transaction.description,
|
||||
amount_cents: transaction.amount_cents,
|
||||
direction: transaction.direction.as_str().to_owned(),
|
||||
date: transaction.date,
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let (status, transaction) = match repository::upsert(&pool, transaction).await? {
|
||||
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
|
||||
let (status, row) = match upsert_transaction(&pool, transaction).await? {
|
||||
Upsert::Created(row) => (StatusCode::CREATED, row),
|
||||
Upsert::Updated(row) => (StatusCode::OK, row),
|
||||
};
|
||||
let transaction = Transaction::try_from(row)?;
|
||||
|
||||
Ok((status, Json(transaction)))
|
||||
Ok((status, Json(transaction.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -90,7 +70,7 @@ async fn delete_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match delete_transaction_by_id(&pool, id).await? {
|
||||
match repository::delete(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user