Refactored API DTOs to be more straight forward.
This commit is contained in:
@@ -9,7 +9,7 @@ use crate::{
|
||||
repository::{Delete, Upsert, recurring_transactions as repository},
|
||||
routes::{
|
||||
dtos::scheduled_transaction::{
|
||||
RecurringTransaction, ScheduledTransaction, ScheduledTransactionData,
|
||||
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
||||
},
|
||||
extractors::{json::Json, path::Path},
|
||||
},
|
||||
@@ -30,12 +30,12 @@ pub fn create_router() -> axum::Router<PgPool> {
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_scheduled_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<Json<Vec<ScheduledTransaction>>, AppError> {
|
||||
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
||||
let now = Utc::now();
|
||||
let transactions = repository::list(&pool)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|transaction| respond(transaction, now))
|
||||
.map(|transaction| create_response(transaction, now))
|
||||
.collect();
|
||||
|
||||
Ok(Json(transactions))
|
||||
@@ -46,12 +46,12 @@ async fn get_scheduled_transactions(
|
||||
async fn get_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ScheduledTransaction>, AppError> {
|
||||
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
|
||||
let transaction = repository::get(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(Json(respond(transaction, Utc::now())))
|
||||
Ok(Json(create_response(transaction, Utc::now())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -59,9 +59,9 @@ async fn get_scheduled_transaction(
|
||||
async fn put_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(transaction): Json<ScheduledTransactionData>,
|
||||
) -> Result<(StatusCode, Json<ScheduledTransaction>), AppError> {
|
||||
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
||||
Json(transaction): Json<ScheduledTransactionRequest>,
|
||||
) -> Result<(StatusCode, Json<ScheduledTransactionResponse>), AppError> {
|
||||
let ScheduledTransactionRequest::Recurring(transaction) = transaction;
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let (status, transaction) = match repository::upsert(&pool, transaction).await? {
|
||||
@@ -69,7 +69,7 @@ async fn put_scheduled_transaction(
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
|
||||
Ok((status, Json(respond(transaction, Utc::now()))))
|
||||
Ok((status, Json(create_response(transaction, Utc::now()))))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -84,8 +84,11 @@ async fn delete_scheduled_transaction(
|
||||
}
|
||||
}
|
||||
|
||||
fn respond(transaction: domain::RecurringTransaction, now: DateTime<Utc>) -> ScheduledTransaction {
|
||||
fn create_response(
|
||||
transaction: domain::RecurringTransaction,
|
||||
now: DateTime<Utc>,
|
||||
) -> ScheduledTransactionResponse {
|
||||
let next = transaction.schedule.next_occurrence(now);
|
||||
|
||||
RecurringTransaction::new(transaction, next).into()
|
||||
RecurringTransactionResponse::new(transaction, next).into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user