From a11a35b767435684903ad4ce5276027ca6f9d92e Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 19 Mar 2023 11:23:27 -0400 Subject: [PATCH] Added admin method to agent --- sql/agent/fetch_all.sql | 9 +++++++++ src/dao/agent.rs | 6 ++++++ src/dao/project.rs | 31 +++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 sql/agent/fetch_all.sql diff --git a/sql/agent/fetch_all.sql b/sql/agent/fetch_all.sql new file mode 100644 index 0000000..f351309 --- /dev/null +++ b/sql/agent/fetch_all.sql @@ -0,0 +1,9 @@ +SELECT + id, + full_name, + credential, + credential_type as "credential_type: _", + time_created, + last_updated +FROM agent +ORDER BY time_created DESC; \ No newline at end of file diff --git a/src/dao/agent.rs b/src/dao/agent.rs index 2d3a1a4..56ab5fe 100644 --- a/src/dao/agent.rs +++ b/src/dao/agent.rs @@ -62,3 +62,9 @@ pub async fn delete_agent( .execute(tx) .await } + +pub async fn fetch_all(conn: &PgPool) -> Result, sqlx::Error> { + sqlx::query_file_as!(Agent, "sql/agent/fetch_all.sql") + .fetch_all(conn) + .await +} diff --git a/src/dao/project.rs b/src/dao/project.rs index 48a2601..fcd6a2d 100644 --- a/src/dao/project.rs +++ b/src/dao/project.rs @@ -1,4 +1,7 @@ -use jl_types::{domain::{project::Project, project_state::ProjectState}, dto::filters::Filter}; +use jl_types::{ + domain::{project::Project, project_state::ProjectState}, + dto::filters::Filter, +}; use sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction}; use uuid::Uuid; @@ -56,18 +59,24 @@ pub async fn update( project.media as _, project.last_updated, project.id - ).fetch_one(tx).await + ) + .fetch_one(tx) + .await } pub async fn get_with_id(conn: &PgPool, project_id: &Uuid) -> Result, sqlx::Error> { - sqlx::query_file_as!(Project, "sql/project/get_with_id.sql", project_id).fetch_optional(conn).await + sqlx::query_file_as!(Project, "sql/project/get_with_id.sql", project_id) + .fetch_optional(conn) + .await } pub async fn fetch_by_ids( conn: &PgPool, project_ids: &Vec, ) -> Result, sqlx::Error> { - sqlx::query_file_as!(Project, "sql/project/fetch_by_ids.sql", project_ids).fetch_all(conn).await + sqlx::query_file_as!(Project, "sql/project/fetch_by_ids.sql", project_ids) + .fetch_all(conn) + .await } pub async fn fetch_with_filters_paged( @@ -81,23 +90,29 @@ pub async fn fetch_with_filters_paged( let mut project_type_filter = None; let mut project_state_filter = ProjectState::InConstruction; let mut project_condition_filter = None; - + for filter in filters { match filter { Filter::InCity(city) => city_filter = Some(city), Filter::InDistrict(district) => district_filter = Some(district), Filter::Finished => project_state_filter = ProjectState::InConstruction, Filter::ByProjectType(project_type) => project_type_filter = Some(project_type), - Filter::ByProjectCondition(project_condition) => project_condition_filter = Some(project_condition), + Filter::ByProjectCondition(project_condition) => { + project_condition_filter = Some(project_condition) + } } } - sqlx::query_file_as!(Project, "sql/project/fetch_with_filters_paged.sql", + sqlx::query_file_as!( + Project, + "sql/project/fetch_with_filters_paged.sql", city_filter, district_filter, project_type_filter as _, project_condition_filter as _, project_state_filter as _, offset - ).fetch_all(conn).await + ) + .fetch_all(conn) + .await }