Added get scheduled transaction.
This commit is contained in:
@@ -14,6 +14,9 @@ use crate::routes::{
|
|||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
|
#[error("not found")]
|
||||||
|
NotFound,
|
||||||
|
|
||||||
#[error("database error")]
|
#[error("database error")]
|
||||||
Database(#[from] sqlx::Error),
|
Database(#[from] sqlx::Error),
|
||||||
|
|
||||||
@@ -33,6 +36,7 @@ pub enum AppError {
|
|||||||
impl IntoResponse for AppError {
|
impl IntoResponse for AppError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
match self {
|
match self {
|
||||||
|
Self::NotFound => return StatusCode::NOT_FOUND.into_response(),
|
||||||
Self::ScheduledTransactionValidation(error) => {
|
Self::ScheduledTransactionValidation(error) => {
|
||||||
return (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response();
|
return (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ use crate::{
|
|||||||
common::{Delete, Upsert},
|
common::{Delete, Upsert},
|
||||||
dtos::recurring_transaction::RecurringTransactionRow,
|
dtos::recurring_transaction::RecurringTransactionRow,
|
||||||
recurring_transactions::{
|
recurring_transactions::{
|
||||||
delete_scheduled_transaction_by_id, query_scheduled_transactions,
|
delete_scheduled_transaction_by_id, get_scheduled_transaction_by_id,
|
||||||
upsert_scheduled_transaction,
|
query_scheduled_transactions, upsert_scheduled_transaction,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
error::AppError,
|
error::AppError,
|
||||||
@@ -25,10 +25,12 @@ use crate::{
|
|||||||
pub fn create_router() -> axum::Router<PgPool> {
|
pub fn create_router() -> axum::Router<PgPool> {
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.route("/", axum::routing::get(get_scheduled_transactions))
|
.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::put(put_scheduled_transaction))
|
||||||
.route("/{id}", axum::routing::delete(delete_scheduled_transaction))
|
.route("/{id}", axum::routing::delete(delete_scheduled_transaction))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_scheduled_transactions(
|
async fn get_scheduled_transactions(
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
@@ -44,10 +46,28 @@ async fn get_scheduled_transactions(
|
|||||||
Ok(Json(transactions))
|
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(|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))]
|
#[tracing::instrument(skip(pool, transaction))]
|
||||||
async fn put_scheduled_transaction(
|
async fn put_scheduled_transaction(
|
||||||
Path(id): Path<Uuid>,
|
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
Json(transaction): Json<ScheduledTransactionData>,
|
Json(transaction): Json<ScheduledTransactionData>,
|
||||||
) -> Result<(StatusCode, Json<ScheduledTransaction>), AppError> {
|
) -> Result<(StatusCode, Json<ScheduledTransaction>), AppError> {
|
||||||
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
||||||
@@ -76,10 +96,11 @@ async fn put_scheduled_transaction(
|
|||||||
Ok((status, Json(transaction.into())))
|
Ok((status, Json(transaction.into())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_scheduled_transaction(
|
async fn delete_scheduled_transaction(
|
||||||
Path(id): Path<Uuid>,
|
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
Ok(match delete_scheduled_transaction_by_id(&pool, id).await? {
|
Ok(match delete_scheduled_transaction_by_id(&pool, id).await? {
|
||||||
Delete::Deleted => StatusCode::NO_CONTENT,
|
Delete::Deleted => StatusCode::NO_CONTENT,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ pub fn create_router() -> axum::Router<PgPool> {
|
|||||||
.route("/{id}", axum::routing::delete(delete_transaction))
|
.route("/{id}", axum::routing::delete(delete_transaction))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_transactions(
|
async fn get_transactions(
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
@@ -40,6 +41,7 @@ async fn get_transactions(
|
|||||||
Ok(Json(transactions))
|
Ok(Json(transactions))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_transaction(
|
async fn delete_transaction(
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
|
|||||||
Reference in New Issue
Block a user