Added query parameters.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use axum::{extract::State, http::StatusCode};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -7,7 +8,7 @@ use crate::{
|
||||
repository::{Delete, Upsert, transactions as repository},
|
||||
routes::{
|
||||
dtos::transaction::{TransactionRequest, TransactionResponse},
|
||||
extractors::{json::Json, path::Path},
|
||||
extractors::{json::Json, path::Path, query::Query},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -22,12 +23,30 @@ pub fn create_router() -> axum::Router<PgPool> {
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct QueryParams {
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
impl TryFrom<QueryParams> for repository::Query {
|
||||
type Error = AppError;
|
||||
|
||||
fn try_from(query: QueryParams) -> Result<Self, Self::Error> {
|
||||
Ok(repository::Query {
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
||||
let transactions = repository::list(&pool)
|
||||
let transactions = repository::list(&pool, query.try_into()?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(TransactionResponse::from)
|
||||
|
||||
Reference in New Issue
Block a user