Added query parameters.
This commit is contained in:
+6
-3
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n ORDER BY starts_at DESC, id DESC\n ",
|
"query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n ORDER BY starts_at DESC, id DESC\n LIMIT $1 OFFSET $2\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -50,7 +50,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": []
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Int8"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
false,
|
false,
|
||||||
@@ -64,5 +67,5 @@
|
|||||||
true
|
true
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "adfd3f5ee43fd9877d4411cb20b2d147ec204db6783a570668b4843c1c57ea4e"
|
"hash": "b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28"
|
||||||
}
|
}
|
||||||
+6
-3
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n\t\tSELECT id, description, amount_cents, direction, date\n\t\tFROM transactions\n\t\tORDER BY date DESC, id DESC\n\t\t",
|
"query": "\n\t\tSELECT id, description, amount_cents, direction, date\n\t\tFROM transactions\n\t\tORDER BY date DESC, id DESC\n\t\tLIMIT $1\n\t\tOFFSET $2\n\t\t",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -30,7 +30,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": []
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Int8"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
false,
|
false,
|
||||||
@@ -40,5 +43,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "7e9c0f4b0502b63b823233a826c9388703e9568f769cfc50c40ac0929750b744"
|
"hash": "e7d222f4f85fc93521d38786aff2d2b7012139d5b3175a4eae06988317475c5d"
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,14 @@ use crate::database::{
|
|||||||
dtos::recurring_transaction::RecurringTransactionRow,
|
dtos::recurring_transaction::RecurringTransactionRow,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub struct Query {
|
||||||
|
pub limit: Option<i64>,
|
||||||
|
pub offset: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn query_recurring_transactions<'e, E>(
|
pub async fn query_recurring_transactions<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
query: Query,
|
||||||
) -> Result<Vec<RecurringTransactionRow>, sqlx::Error>
|
) -> Result<Vec<RecurringTransactionRow>, sqlx::Error>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
@@ -26,7 +32,10 @@ where
|
|||||||
weekend_adjustment
|
weekend_adjustment
|
||||||
FROM recurring_transactions
|
FROM recurring_transactions
|
||||||
ORDER BY starts_at DESC, id DESC
|
ORDER BY starts_at DESC, id DESC
|
||||||
|
LIMIT $1 OFFSET $2
|
||||||
"#,
|
"#,
|
||||||
|
query.limit,
|
||||||
|
query.offset,
|
||||||
)
|
)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -5,7 +5,15 @@ use crate::database::{
|
|||||||
dtos::transaction::TransactionRow,
|
dtos::transaction::TransactionRow,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn query_transactions<'e, E>(executor: E) -> Result<Vec<TransactionRow>, sqlx::Error>
|
pub struct Query {
|
||||||
|
pub limit: Option<i64>,
|
||||||
|
pub offset: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn query_transactions<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
query: Query,
|
||||||
|
) -> Result<Vec<TransactionRow>, sqlx::Error>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
@@ -15,7 +23,11 @@ where
|
|||||||
SELECT id, description, amount_cents, direction, date
|
SELECT id, description, amount_cents, direction, date
|
||||||
FROM transactions
|
FROM transactions
|
||||||
ORDER BY date DESC, id DESC
|
ORDER BY date DESC, id DESC
|
||||||
|
LIMIT $1
|
||||||
|
OFFSET $2
|
||||||
"#,
|
"#,
|
||||||
|
query.limit,
|
||||||
|
query.offset,
|
||||||
)
|
)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await
|
.await
|
||||||
|
|||||||
+6
-3
@@ -29,15 +29,18 @@ impl IntoResponse for AppError {
|
|||||||
Self::NotFound => {
|
Self::NotFound => {
|
||||||
return error_response(StatusCode::NOT_FOUND, "Not found");
|
return error_response(StatusCode::NOT_FOUND, "Not found");
|
||||||
}
|
}
|
||||||
Self::Validation(error) => {
|
|
||||||
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::Database(error)) => {
|
Self::Repository(RepositoryError::Database(error)) => {
|
||||||
tracing::error!(%error, "Database query failed");
|
tracing::error!(%error, "Database query failed");
|
||||||
}
|
}
|
||||||
Self::Repository(RepositoryError::Decode(error)) => {
|
Self::Repository(RepositoryError::Decode(error)) => {
|
||||||
tracing::error!(%error, "Invalid stored data");
|
tracing::error!(%error, "Invalid stored data");
|
||||||
}
|
}
|
||||||
|
Self::Repository(RepositoryError::QueryConversion(error)) => {
|
||||||
|
return error_response(StatusCode::BAD_REQUEST, error.to_string());
|
||||||
|
}
|
||||||
|
Self::Validation(error) => {
|
||||||
|
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
|
error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ pub enum RepositoryError {
|
|||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Decode(#[from] DecodeError),
|
Decode(#[from] DecodeError),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
QueryConversion(#[from] QueryConversionError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
@@ -34,3 +37,12 @@ pub enum DecodeError {
|
|||||||
#[error("invalid weekend_adjustment value: {0}")]
|
#[error("invalid weekend_adjustment value: {0}")]
|
||||||
WeekendAdjustment(String),
|
WeekendAdjustment(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum QueryConversionError {
|
||||||
|
#[error("Failed to convert limit: {0}")]
|
||||||
|
Limit(u64),
|
||||||
|
|
||||||
|
#[error("Failed to convert offset: {0}")]
|
||||||
|
Offset(u64),
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
DecodeError, Delete, RepositoryError, Upsert,
|
DecodeError, Delete, QueryConversionError, RepositoryError, Upsert,
|
||||||
encoding::{
|
encoding::{
|
||||||
decode_direction, decode_recurrence_unit, decode_weekend_adjustment, encode_direction,
|
decode_direction, decode_recurrence_unit, decode_weekend_adjustment, encode_direction,
|
||||||
encode_recurrence_unit, encode_weekend_adjustment,
|
encode_recurrence_unit, encode_weekend_adjustment,
|
||||||
@@ -14,11 +14,36 @@ use crate::{
|
|||||||
domain::{Cents, Recurrence, RecurrenceInterval, RecurringTransaction, Schedule},
|
domain::{Cents, Recurrence, RecurrenceInterval, RecurringTransaction, Schedule},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn list<'e, E>(executor: E) -> Result<Vec<RecurringTransaction>, RepositoryError>
|
pub struct Query {
|
||||||
|
pub limit: Option<u64>,
|
||||||
|
pub offset: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Query> for db::Query {
|
||||||
|
type Error = QueryConversionError;
|
||||||
|
|
||||||
|
fn try_from(query: Query) -> Result<Self, Self::Error> {
|
||||||
|
Ok(db::Query {
|
||||||
|
limit: query
|
||||||
|
.limit
|
||||||
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||||
|
.transpose()?,
|
||||||
|
offset: query
|
||||||
|
.offset
|
||||||
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||||
|
.transpose()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn query<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
query: Query,
|
||||||
|
) -> Result<Vec<RecurringTransaction>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let transactions = db::query_recurring_transactions(executor)
|
let transactions = db::query_recurring_transactions(executor, query.try_into()?)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
DecodeError, Delete, RepositoryError, Upsert,
|
DecodeError, Delete, QueryConversionError, RepositoryError, Upsert,
|
||||||
encoding::{decode_direction, encode_direction},
|
encoding::{decode_direction, encode_direction},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -9,11 +9,33 @@ use crate::{
|
|||||||
domain::{Cents, Transaction},
|
domain::{Cents, Transaction},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn list<'e, E>(executor: E) -> Result<Vec<Transaction>, RepositoryError>
|
pub struct Query {
|
||||||
|
pub limit: Option<u64>,
|
||||||
|
pub offset: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Query> for db::Query {
|
||||||
|
type Error = QueryConversionError;
|
||||||
|
|
||||||
|
fn try_from(query: Query) -> Result<Self, Self::Error> {
|
||||||
|
Ok(db::Query {
|
||||||
|
limit: query
|
||||||
|
.limit
|
||||||
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||||
|
.transpose()?,
|
||||||
|
offset: query
|
||||||
|
.offset
|
||||||
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||||
|
.transpose()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<Transaction>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let transactions = db::query_transactions(executor)
|
let transactions = db::query_transactions(executor, query.try_into()?)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod json;
|
pub mod json;
|
||||||
pub mod path;
|
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 axum::{extract::State, http::StatusCode};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ use crate::{
|
|||||||
dtos::scheduled_transaction::{
|
dtos::scheduled_transaction::{
|
||||||
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
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]
|
#[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>,
|
||||||
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let transactions = repository::list(&pool)
|
let transactions = repository::query(&pool, query.try_into()?)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|transaction| create_response(transaction, now))
|
.map(|transaction| create_response(transaction, now))
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use axum::{extract::State, http::StatusCode};
|
use axum::{extract::State, http::StatusCode};
|
||||||
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ use crate::{
|
|||||||
repository::{Delete, Upsert, transactions as repository},
|
repository::{Delete, Upsert, transactions as repository},
|
||||||
routes::{
|
routes::{
|
||||||
dtos::transaction::{TransactionRequest, TransactionResponse},
|
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]
|
#[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>,
|
||||||
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
||||||
let transactions = repository::list(&pool)
|
let transactions = repository::list(&pool, query.try_into()?)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(TransactionResponse::from)
|
.map(TransactionResponse::from)
|
||||||
|
|||||||
Reference in New Issue
Block a user