Added query parameters.
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
pub mod json;
|
||||
pub mod path;
|
||||
pub mod query;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
use axum::extract::FromRequestParts;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
pub struct Query<T>(pub T);
|
||||
|
||||
impl<T, S> FromRequestParts<S> for Query<T>
|
||||
where
|
||||
T: DeserializeOwned + Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AppError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
state: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
axum::extract::Query::<T>::from_request_parts(parts, state)
|
||||
.await
|
||||
.map(|axum::extract::Query(value)| Self(value))
|
||||
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use axum::{extract::State, http::StatusCode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -11,7 +12,7 @@ use crate::{
|
||||
dtos::scheduled_transaction::{
|
||||
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
||||
},
|
||||
extractors::{json::Json, path::Path},
|
||||
extractors::{json::Json, path::Path, query::Query},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -26,13 +27,31 @@ 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_scheduled_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
||||
let now = Utc::now();
|
||||
let transactions = repository::list(&pool)
|
||||
let transactions = repository::query(&pool, query.try_into()?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|transaction| create_response(transaction, now))
|
||||
|
||||
@@ -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