115 lines
3.6 KiB
Rust
115 lines
3.6 KiB
Rust
use axum::{
|
|
Json,
|
|
extract::{Path, State},
|
|
http::StatusCode,
|
|
};
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
database::{
|
|
common::{Delete, Upsert},
|
|
dtos::recurring_transaction::RecurringTransactionRow,
|
|
recurring_transactions::{
|
|
delete_scheduled_transaction_by_id, get_scheduled_transaction_by_id,
|
|
query_scheduled_transactions, upsert_scheduled_transaction,
|
|
},
|
|
},
|
|
error::AppError,
|
|
routes::{
|
|
dtos::scheduled_transaction::{
|
|
RecurringTransaction, ScheduledTransaction, ScheduledTransactionData,
|
|
},
|
|
validation::scheduled_transaction::ValidatedRecurringTransaction,
|
|
},
|
|
};
|
|
|
|
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<ScheduledTransaction>>, AppError> {
|
|
let rows = query_scheduled_transactions(&pool).await?;
|
|
|
|
let transactions = rows
|
|
.into_iter()
|
|
.map(RecurringTransaction::try_from)
|
|
.map(|result| result.map(ScheduledTransaction::from))
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
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<ScheduledTransaction>, AppError> {
|
|
let row = get_scheduled_transaction_by_id(&pool, id)
|
|
.await?
|
|
.map(RecurringTransaction::try_from)
|
|
.transpose()?;
|
|
|
|
match row {
|
|
Some(transaction) => Ok(Json(ScheduledTransaction::from(transaction))),
|
|
None => Err(AppError::NotFound),
|
|
}
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool, 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;
|
|
|
|
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
|
let transaction = RecurringTransactionRow {
|
|
id,
|
|
name: transaction.name,
|
|
amount_cents: transaction.amount_cents,
|
|
direction: transaction.direction.as_str().to_owned(),
|
|
starts_at: transaction.starts_at,
|
|
ends_at: transaction.ends_at,
|
|
recurrence_every: transaction.recurrence_every.get(),
|
|
recurrence_unit: transaction.recurrence_unit.as_str().to_owned(),
|
|
weekend_adjustment: transaction
|
|
.weekend_adjustment
|
|
.map(|adjustment| adjustment.as_str().to_owned()),
|
|
};
|
|
|
|
let (status, row) = match upsert_scheduled_transaction(&pool, transaction).await? {
|
|
Upsert::Created(row) => (StatusCode::CREATED, row),
|
|
Upsert::Updated(row) => (StatusCode::OK, row),
|
|
};
|
|
let transaction = RecurringTransaction::try_from(row)?;
|
|
|
|
Ok((status, Json(transaction.into())))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn delete_scheduled_transaction(
|
|
State(pool): State<PgPool>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<StatusCode, AppError> {
|
|
match delete_scheduled_transaction_by_id(&pool, id).await? {
|
|
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
|
Delete::NotFound => Err(AppError::NotFound),
|
|
}
|
|
}
|