95 lines
2.9 KiB
Rust
95 lines
2.9 KiB
Rust
use axum::{extract::State, http::StatusCode};
|
|
use chrono::{DateTime, Utc};
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
domain,
|
|
error::AppError,
|
|
repository::{Delete, Upsert, recurring_transactions as repository},
|
|
routes::{
|
|
dtos::scheduled_transaction::{
|
|
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
|
},
|
|
extractors::{json::Json, path::Path},
|
|
},
|
|
};
|
|
|
|
pub fn create_router() -> axum::Router<PgPool> {
|
|
axum::Router::new()
|
|
.route("/", axum::routing::get(get_scheduled_transactions))
|
|
.route(
|
|
"/{id}",
|
|
axum::routing::get(get_scheduled_transaction)
|
|
.put(put_scheduled_transaction)
|
|
.delete(delete_scheduled_transaction),
|
|
)
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn get_scheduled_transactions(
|
|
State(pool): State<PgPool>,
|
|
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
|
let now = Utc::now();
|
|
let transactions = repository::list(&pool)
|
|
.await?
|
|
.into_iter()
|
|
.map(|transaction| create_response(transaction, now))
|
|
.collect();
|
|
|
|
Ok(Json(transactions))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn get_scheduled_transaction(
|
|
State(pool): State<PgPool>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
|
|
let transaction = repository::get(&pool, id)
|
|
.await?
|
|
.ok_or(AppError::NotFound)?;
|
|
|
|
Ok(Json(create_response(transaction, Utc::now())))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool, transaction))]
|
|
async fn put_scheduled_transaction(
|
|
State(pool): State<PgPool>,
|
|
Path(id): Path<Uuid>,
|
|
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? {
|
|
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
|
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
|
};
|
|
|
|
Ok((status, Json(create_response(transaction, Utc::now()))))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn delete_scheduled_transaction(
|
|
State(pool): State<PgPool>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<StatusCode, AppError> {
|
|
match repository::delete(&pool, id).await? {
|
|
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
|
Delete::NotFound => Err(AppError::NotFound),
|
|
}
|
|
}
|
|
|
|
fn create_response(
|
|
transaction: domain::RecurringTransaction,
|
|
now: DateTime<Utc>,
|
|
) -> ScheduledTransactionResponse {
|
|
let next = transaction.schedule.next_occurrence(now);
|
|
|
|
RecurringTransactionResponse::new(transaction, next).into()
|
|
}
|