Added name query to accounts and updated query handling.

This commit is contained in:
2026-09-27 10:32:08 +02:00
parent bb58e3fd77
commit d219d63dca
10 changed files with 74 additions and 4 deletions
@@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "\n\t\tSELECT id, name\n\t\tFROM accounts\n\t\tORDER BY name DESC, id DESC\n\t\tLIMIT $1\n\t\tOFFSET $2\n\t\t", "query": "\n\t\tSELECT id, name\n\t\tFROM accounts\n\t\tWHERE ($1::text IS NULL OR name ILIKE '%' || $1 || '%')\n\t\tORDER BY name DESC, id DESC\n\t\tLIMIT $2\n\t\tOFFSET $3\n\t\t",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -16,6 +16,7 @@
], ],
"parameters": { "parameters": {
"Left": [ "Left": [
"Text",
"Int8", "Int8",
"Int8" "Int8"
] ]
@@ -25,5 +26,5 @@
false false
] ]
}, },
"hash": "b7bfaaed62475d82b8d551e1c9223ae42f06e1534d9587b876dbc494c9eeaebc" "hash": "9b5267c3bd09fb64717565c5c7e3084195d09c0a30f7e963a2497892398c6adc"
} }
+15 -2
View File
@@ -3,10 +3,21 @@ use uuid::Uuid;
use super::dtos::{AccountRow, Delete, Upsert}; use super::dtos::{AccountRow, Delete, Upsert};
pub struct Query { pub struct Query {
pub name: Option<String>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub offset: Option<i64>, pub offset: Option<i64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
name: None,
limit: None,
offset: None,
}
}
}
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<AccountRow>, sqlx::Error> pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<AccountRow>, sqlx::Error>
where where
E: sqlx::Executor<'e, Database = sqlx::Postgres>, E: sqlx::Executor<'e, Database = sqlx::Postgres>,
@@ -16,10 +27,12 @@ where
r#" r#"
SELECT id, name SELECT id, name
FROM accounts FROM accounts
WHERE ($1::text IS NULL OR name ILIKE '%' || $1 || '%')
ORDER BY name DESC, id DESC ORDER BY name DESC, id DESC
LIMIT $1 LIMIT $2
OFFSET $2 OFFSET $3
"#, "#,
query.name,
query.limit, query.limit,
query.offset, query.offset,
) )
@@ -7,6 +7,15 @@ pub struct Query {
pub offset: Option<i64>, pub offset: Option<i64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
limit: None,
offset: None,
}
}
}
pub async fn query<'e, E>( pub async fn query<'e, E>(
executor: E, executor: E,
query: Query, query: Query,
+9
View File
@@ -7,6 +7,15 @@ pub struct Query {
pub offset: Option<i64>, pub offset: Option<i64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
limit: None,
offset: None,
}
}
}
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<TransactionRow>, sqlx::Error> pub async fn query<'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>,
+13
View File
@@ -10,15 +10,27 @@ use crate::{
}; };
pub struct Query { pub struct Query {
pub name: Option<String>,
pub limit: Option<u64>, pub limit: Option<u64>,
pub offset: Option<u64>, pub offset: Option<u64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
name: None,
limit: None,
offset: None,
}
}
}
impl TryFrom<Query> for db::Query { impl TryFrom<Query> for db::Query {
type Error = QueryConversionError; type Error = QueryConversionError;
fn try_from(query: Query) -> Result<Self, Self::Error> { fn try_from(query: Query) -> Result<Self, Self::Error> {
Ok(db::Query { Ok(db::Query {
name: query.name,
limit: query limit: query
.limit .limit
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l))) .map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
@@ -27,6 +39,7 @@ impl TryFrom<Query> for db::Query {
.offset .offset
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o))) .map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
.transpose()?, .transpose()?,
..Default::default()
}) })
} }
} }
@@ -18,6 +18,15 @@ pub struct Query {
pub offset: Option<u64>, pub offset: Option<u64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
limit: None,
offset: None,
}
}
}
impl TryFrom<Query> for db::Query { impl TryFrom<Query> for db::Query {
type Error = QueryConversionError; type Error = QueryConversionError;
@@ -31,6 +40,7 @@ impl TryFrom<Query> for db::Query {
.offset .offset
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o))) .map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
.transpose()?, .transpose()?,
..Default::default()
}) })
} }
} }
+10
View File
@@ -15,6 +15,15 @@ pub struct Query {
pub offset: Option<u64>, pub offset: Option<u64>,
} }
impl Default for Query {
fn default() -> Self {
Self {
limit: None,
offset: None,
}
}
}
impl TryFrom<Query> for db::Query { impl TryFrom<Query> for db::Query {
type Error = QueryConversionError; type Error = QueryConversionError;
@@ -28,6 +37,7 @@ impl TryFrom<Query> for db::Query {
.offset .offset
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o))) .map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
.transpose()?, .transpose()?,
..Default::default()
}) })
} }
} }
+3
View File
@@ -28,6 +28,7 @@ pub fn create_router() -> axum::Router<PgPool> {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct QueryParams { pub struct QueryParams {
pub name: Option<String>,
pub limit: Option<u64>, pub limit: Option<u64>,
pub offset: Option<u64>, pub offset: Option<u64>,
} }
@@ -37,8 +38,10 @@ impl TryFrom<QueryParams> for repository::Query {
fn try_from(query: QueryParams) -> Result<Self, Self::Error> { fn try_from(query: QueryParams) -> Result<Self, Self::Error> {
Ok(repository::Query { Ok(repository::Query {
name: query.name,
limit: Some(query.limit.unwrap_or(20)), limit: Some(query.limit.unwrap_or(20)),
offset: query.offset, offset: query.offset,
..Default::default()
}) })
} }
} }
@@ -43,6 +43,7 @@ impl TryFrom<QueryParams> for repository::Query {
Ok(repository::Query { Ok(repository::Query {
limit: Some(query.limit.unwrap_or(20)), limit: Some(query.limit.unwrap_or(20)),
offset: query.offset, offset: query.offset,
..Default::default()
}) })
} }
} }
+1
View File
@@ -39,6 +39,7 @@ impl TryFrom<QueryParams> for repository::Query {
Ok(repository::Query { Ok(repository::Query {
limit: Some(query.limit.unwrap_or(20)), limit: Some(query.limit.unwrap_or(20)),
offset: query.offset, offset: query.offset,
..Default::default()
}) })
} }
} }