Refactored API DTOs to be more straight forward.

This commit is contained in:
2026-09-26 18:27:18 +02:00
parent 4737bd5c81
commit 9279258ff1
6 changed files with 250 additions and 192 deletions
+8 -6
View File
@@ -6,7 +6,7 @@ use crate::{
error::AppError,
repository::{Delete, Upsert, transactions as repository},
routes::{
dtos::transaction::{Transaction, TransactionData},
dtos::transaction::{TransactionRequest, TransactionResponse},
extractors::{json::Json, path::Path},
},
};
@@ -24,11 +24,13 @@ 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> {
async fn get_transactions(
State(pool): State<PgPool>,
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
let transactions = repository::list(&pool)
.await?
.into_iter()
.map(Transaction::from)
.map(TransactionResponse::from)
.collect();
Ok(Json(transactions))
@@ -39,7 +41,7 @@ async fn get_transactions(State(pool): State<PgPool>) -> Result<Json<Vec<Transac
async fn get_transaction(
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<Transaction>, AppError> {
) -> Result<Json<TransactionResponse>, AppError> {
let transaction = repository::get(&pool, id)
.await?
.ok_or(AppError::NotFound)?;
@@ -52,8 +54,8 @@ async fn get_transaction(
async fn put_transaction(
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(transaction): Json<TransactionData>,
) -> Result<(StatusCode, Json<Transaction>), AppError> {
Json(transaction): Json<TransactionRequest>,
) -> Result<(StatusCode, Json<TransactionResponse>), AppError> {
let transaction = transaction.into_domain(id)?;
let (status, transaction) = match repository::upsert(&pool, transaction).await? {