Added additional transaction operations.
This commit is contained in:
@@ -7,53 +7,94 @@ use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::dtos::transaction::TransactionRow, error::AppError,
|
||||
routes::dtos::transaction::TransactionResponse,
|
||||
database::{
|
||||
common::{Delete, Upsert},
|
||||
dtos::transaction::TransactionRow,
|
||||
transactions::{
|
||||
delete_transaction_by_id, get_transaction_by_id, query_transactions, upsert_transaction,
|
||||
},
|
||||
},
|
||||
error::AppError,
|
||||
routes::{
|
||||
dtos::transaction::{Transaction, TransactionData},
|
||||
validation::transaction::ValidatedTransaction,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(get_transactions))
|
||||
.route("/{id}", axum::routing::delete(delete_transaction))
|
||||
.route(
|
||||
"/{id}",
|
||||
axum::routing::get(get_transaction)
|
||||
.put(put_transaction)
|
||||
.delete(delete_transaction),
|
||||
)
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
||||
let rows = sqlx::query_as!(
|
||||
TransactionRow,
|
||||
r#"
|
||||
SELECT id, description, amount_cents, direction, date
|
||||
FROM transactions
|
||||
ORDER BY date DESC, id DESC
|
||||
"#
|
||||
)
|
||||
.fetch_all(&pool)
|
||||
.await?;
|
||||
async fn get_transactions(State(pool): State<PgPool>) -> Result<Json<Vec<Transaction>>, AppError> {
|
||||
let rows = query_transactions(&pool).await?;
|
||||
|
||||
let transactions = rows
|
||||
.into_iter()
|
||||
.map(TransactionResponse::try_from)
|
||||
.map(Transaction::try_from)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(Json(transactions))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<Transaction>, AppError> {
|
||||
let transaction = get_transaction_by_id(&pool, id)
|
||||
.await?
|
||||
.map(Transaction::try_from)
|
||||
.transpose()?;
|
||||
|
||||
match transaction {
|
||||
Some(transaction) => Ok(Json(transaction)),
|
||||
None => Err(AppError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool, transaction))]
|
||||
async fn put_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(transaction): Json<TransactionData>,
|
||||
) -> Result<(StatusCode, Json<Transaction>), AppError> {
|
||||
let transaction = ValidatedTransaction::try_from(transaction)?;
|
||||
let transaction = TransactionRow {
|
||||
id,
|
||||
description: transaction.description,
|
||||
amount_cents: transaction.amount_cents,
|
||||
direction: transaction.direction.as_str().to_owned(),
|
||||
date: transaction.date,
|
||||
};
|
||||
|
||||
let (status, row) = match upsert_transaction(&pool, transaction).await? {
|
||||
Upsert::Created(row) => (StatusCode::CREATED, row),
|
||||
Upsert::Updated(row) => (StatusCode::OK, row),
|
||||
};
|
||||
let transaction = Transaction::try_from(row)?;
|
||||
|
||||
Ok((status, Json(transaction)))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn delete_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(&pool)
|
||||
.await?;
|
||||
|
||||
if deleted.is_none() {
|
||||
return Ok(StatusCode::NOT_FOUND);
|
||||
match delete_transaction_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user