From adf6888cc7f0871dbc08a87944781a13f43868d3 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Fri, 25 Sep 2026 15:41:40 +0200 Subject: [PATCH] Added get scheduled transaction. --- server/src/error.rs | 4 +++ server/src/routes/scheduled_transactions.rs | 29 ++++++++++++++++++--- server/src/routes/transactions.rs | 2 ++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/server/src/error.rs b/server/src/error.rs index 457ca7f..82ae280 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -14,6 +14,9 @@ use crate::routes::{ #[derive(Debug, thiserror::Error)] pub enum AppError { + #[error("not found")] + NotFound, + #[error("database error")] Database(#[from] sqlx::Error), @@ -33,6 +36,7 @@ pub enum AppError { impl IntoResponse for AppError { fn into_response(self) -> Response { match self { + Self::NotFound => return StatusCode::NOT_FOUND.into_response(), Self::ScheduledTransactionValidation(error) => { return (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response(); } diff --git a/server/src/routes/scheduled_transactions.rs b/server/src/routes/scheduled_transactions.rs index 48b8ee1..3befe46 100644 --- a/server/src/routes/scheduled_transactions.rs +++ b/server/src/routes/scheduled_transactions.rs @@ -12,8 +12,8 @@ use crate::{ common::{Delete, Upsert}, dtos::recurring_transaction::RecurringTransactionRow, recurring_transactions::{ - delete_scheduled_transaction_by_id, query_scheduled_transactions, - upsert_scheduled_transaction, + delete_scheduled_transaction_by_id, get_scheduled_transaction_by_id, + query_scheduled_transactions, upsert_scheduled_transaction, }, }, error::AppError, @@ -25,10 +25,12 @@ use crate::{ pub fn create_router() -> axum::Router { axum::Router::new() .route("/", axum::routing::get(get_scheduled_transactions)) + .route("/{id}", axum::routing::get(get_scheduled_transaction)) .route("/{id}", axum::routing::put(put_scheduled_transaction)) .route("/{id}", axum::routing::delete(delete_scheduled_transaction)) } +#[axum::debug_handler] #[tracing::instrument(skip(pool))] async fn get_scheduled_transactions( State(pool): State, @@ -44,10 +46,28 @@ async fn get_scheduled_transactions( Ok(Json(transactions)) } +#[axum::debug_handler] +#[tracing::instrument(skip(pool))] +async fn get_scheduled_transaction( + State(pool): State, + Path(id): Path, +) -> Result, AppError> { + let row = get_scheduled_transaction_by_id(&pool, id) + .await? + .map(|t| RecurringTransaction::try_from(t)) + .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( - Path(id): Path, State(pool): State, + Path(id): Path, Json(transaction): Json, ) -> Result<(StatusCode, Json), AppError> { let ScheduledTransactionData::Recurring(transaction) = transaction; @@ -76,10 +96,11 @@ async fn put_scheduled_transaction( Ok((status, Json(transaction.into()))) } +#[axum::debug_handler] #[tracing::instrument(skip(pool))] async fn delete_scheduled_transaction( - Path(id): Path, State(pool): State, + Path(id): Path, ) -> Result { Ok(match delete_scheduled_transaction_by_id(&pool, id).await? { Delete::Deleted => StatusCode::NO_CONTENT, diff --git a/server/src/routes/transactions.rs b/server/src/routes/transactions.rs index 0fb8376..13b0411 100644 --- a/server/src/routes/transactions.rs +++ b/server/src/routes/transactions.rs @@ -17,6 +17,7 @@ pub fn create_router() -> axum::Router { .route("/{id}", axum::routing::delete(delete_transaction)) } +#[axum::debug_handler] #[tracing::instrument(skip(pool))] async fn get_transactions( State(pool): State, @@ -40,6 +41,7 @@ async fn get_transactions( Ok(Json(transactions)) } +#[axum::debug_handler] #[tracing::instrument(skip(pool))] async fn delete_transaction( Path(id): Path,