Added admin method to agent

This commit is contained in:
Franklin 2023-03-19 11:23:27 -04:00
parent 9341596265
commit a11a35b767
3 changed files with 38 additions and 8 deletions

9
sql/agent/fetch_all.sql Normal file
View File

@ -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;

View File

@ -62,3 +62,9 @@ pub async fn delete_agent(
.execute(tx) .execute(tx)
.await .await
} }
pub async fn fetch_all(conn: &PgPool) -> Result<Vec<Agent>, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/fetch_all.sql")
.fetch_all(conn)
.await
}

View File

@ -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 sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction};
use uuid::Uuid; use uuid::Uuid;
@ -56,18 +59,24 @@ pub async fn update(
project.media as _, project.media as _,
project.last_updated, project.last_updated,
project.id project.id
).fetch_one(tx).await )
.fetch_one(tx)
.await
} }
pub async fn get_with_id(conn: &PgPool, project_id: &Uuid) -> Result<Option<Project>, sqlx::Error> { pub async fn get_with_id(conn: &PgPool, project_id: &Uuid) -> Result<Option<Project>, 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( pub async fn fetch_by_ids(
conn: &PgPool, conn: &PgPool,
project_ids: &Vec<Uuid>, project_ids: &Vec<Uuid>,
) -> Result<Vec<Project>, sqlx::Error> { ) -> Result<Vec<Project>, 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( 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_type_filter = None;
let mut project_state_filter = ProjectState::InConstruction; let mut project_state_filter = ProjectState::InConstruction;
let mut project_condition_filter = None; let mut project_condition_filter = None;
for filter in filters { for filter in filters {
match filter { match filter {
Filter::InCity(city) => city_filter = Some(city), Filter::InCity(city) => city_filter = Some(city),
Filter::InDistrict(district) => district_filter = Some(district), Filter::InDistrict(district) => district_filter = Some(district),
Filter::Finished => project_state_filter = ProjectState::InConstruction, Filter::Finished => project_state_filter = ProjectState::InConstruction,
Filter::ByProjectType(project_type) => project_type_filter = Some(project_type), 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, city_filter,
district_filter, district_filter,
project_type_filter as _, project_type_filter as _,
project_condition_filter as _, project_condition_filter as _,
project_state_filter as _, project_state_filter as _,
offset offset
).fetch_all(conn).await )
.fetch_all(conn)
.await
} }