From 719281492bf7e13bf6e24d776c1a59a4abf2089e Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 19 Mar 2023 10:18:11 -0400 Subject: [PATCH] Added all dao & sql methods for unit and agent --- migrations/4_unit.sql | 2 -- sql/agent/update.sql | 21 +++++++------ sql/project/delete.sql | 1 + sql/project/fetch_by_ids.sql | 14 +++++++++ sql/project/fetch_with_filters_paged.sql | 0 sql/project/get_with_id.sql | 14 +++++++++ sql/project/insert.sql | 28 +++++++++++++++++ sql/project/update.sql | 11 +++++++ sql/unit/delete.sql | 1 + sql/unit/fetch_with_project_id.sql | 15 +++++++++ sql/unit/insert.sql | 40 ++++++++++++++++++++++++ sql/unit/update.sql | 24 ++++++++++++++ src/dao/agent.rs | 2 +- src/dao/project.rs | 16 ++++++++++ src/dao/unit.rs | 20 ++++++++++++ 15 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 sql/project/delete.sql create mode 100644 sql/project/fetch_by_ids.sql create mode 100644 sql/project/fetch_with_filters_paged.sql create mode 100644 sql/project/get_with_id.sql create mode 100644 sql/project/update.sql create mode 100644 sql/unit/delete.sql create mode 100644 sql/unit/fetch_with_project_id.sql create mode 100644 sql/unit/update.sql diff --git a/migrations/4_unit.sql b/migrations/4_unit.sql index 43f97ad..3428ef5 100644 --- a/migrations/4_unit.sql +++ b/migrations/4_unit.sql @@ -4,13 +4,11 @@ CREATE TABLE IF NOT EXISTS "unit" ( price_usd FLOAT8 NOT NULL, unit_type VARCHAR NOT NULL, rooms SMALLINT NOT NULL, - bathrooms SMALLINT NOT NULL, area FLOAT4 NOT NULL, description TEXT NOT NULL, media TEXT NOT NULL, admin_tag VARCHAR, - time_created TIMESTAMPTZ NOT NULL, last_updated TIMESTAMPTZ NOT NULL ); diff --git a/sql/agent/update.sql b/sql/agent/update.sql index 8beafc8..d83da50 100644 --- a/sql/agent/update.sql +++ b/sql/agent/update.sql @@ -1,12 +1,13 @@ UPDATE agent SET -full_name = $1, -credential = $2, -credential_type = $3, -last_updated = $4 + full_name = $1, + credential = $2, + credential_type = $3, + last_updated = $4 +WHERE id = $5 RETURNING -id, -full_name, -credential, -credential_type as "credential_type: _", -time_created, -last_updated; \ No newline at end of file + id, + full_name, + credential, + credential_type as "credential_type: _", + time_created, + last_updated; \ No newline at end of file diff --git a/sql/project/delete.sql b/sql/project/delete.sql new file mode 100644 index 0000000..2c6e61c --- /dev/null +++ b/sql/project/delete.sql @@ -0,0 +1 @@ +DELETE FROM project WHERE id = $1; \ No newline at end of file diff --git a/sql/project/fetch_by_ids.sql b/sql/project/fetch_by_ids.sql new file mode 100644 index 0000000..46ae5a1 --- /dev/null +++ b/sql/project/fetch_by_ids.sql @@ -0,0 +1,14 @@ +SELECT +id, +project_type as "project_type: _", +project_condition as "project_condition: _", +agent_id, +location_id, +title, +description, +admin_tag, +floors, +media as "media: _", +time_created, +last_updated +FROM project WHERE id = ANY($1); \ No newline at end of file diff --git a/sql/project/fetch_with_filters_paged.sql b/sql/project/fetch_with_filters_paged.sql new file mode 100644 index 0000000..e69de29 diff --git a/sql/project/get_with_id.sql b/sql/project/get_with_id.sql new file mode 100644 index 0000000..67963bc --- /dev/null +++ b/sql/project/get_with_id.sql @@ -0,0 +1,14 @@ +SELECT +id, +project_type as "project_type: _", +project_condition as "project_condition: _", +agent_id, +location_id, +title, +description, +admin_tag, +floors, +media as "media: _", +time_created, +last_updated +FROM project WHERE id = $1; \ No newline at end of file diff --git a/sql/project/insert.sql b/sql/project/insert.sql index e69de29..ae87b4c 100644 --- a/sql/project/insert.sql +++ b/sql/project/insert.sql @@ -0,0 +1,28 @@ +INSERT INTO project ( + id, + project_type, + project_condition, + agent_id, + location_id, + title, + description, + admin_tag, + floors, + media, + time_created, + last_updated +) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11 +) RETURNING +id, +project_type as "project_type: _", +project_condition as "project_condition: _", +agent_id, +location_id, +title, +description, +admin_tag, +floors, +media as "media: _", +time_created, +last_updated; \ No newline at end of file diff --git a/sql/project/update.sql b/sql/project/update.sql new file mode 100644 index 0000000..ba4cc42 --- /dev/null +++ b/sql/project/update.sql @@ -0,0 +1,11 @@ +UPDATE project SET +project_type = $1, +project_condition = $2, +agent_id = $3, +location_id = $4, +title = $5, +description = $6, +admin_tag = $7, +floors = $8, +media = $9, +last_updated = $10, \ No newline at end of file diff --git a/sql/unit/delete.sql b/sql/unit/delete.sql new file mode 100644 index 0000000..49bc751 --- /dev/null +++ b/sql/unit/delete.sql @@ -0,0 +1 @@ +DELETE FROM unit WHERE id = $1; \ No newline at end of file diff --git a/sql/unit/fetch_with_project_id.sql b/sql/unit/fetch_with_project_id.sql new file mode 100644 index 0000000..9fda831 --- /dev/null +++ b/sql/unit/fetch_with_project_id.sql @@ -0,0 +1,15 @@ +SELECT +id, +project_id, +price_usd, +unit_type as "unit_type: _", +rooms, +bathrooms, +area, +description, +media as "media: _", +admin_tag, +time_created, +last_updated +FROM unit +WHERE project_id = $1; \ No newline at end of file diff --git a/sql/unit/insert.sql b/sql/unit/insert.sql index e69de29..72d6e6b 100644 --- a/sql/unit/insert.sql +++ b/sql/unit/insert.sql @@ -0,0 +1,40 @@ +INSERT INTO unit ( + id, + project_id, + price_usd, + unit_type, + rooms, + bathrooms, + area, + description, + media, + admin_tag, + time_created, + last_updated +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8, + $9, + $10, + $11, + $11 +) +RETURNING +id, +project_id, +price_usd, +unit_type as "unit_type: _", +rooms, +bathrooms, +area, +description, +media as "media: _", +admin_tag, +time_created, +last_updated; \ No newline at end of file diff --git a/sql/unit/update.sql b/sql/unit/update.sql new file mode 100644 index 0000000..2bd0571 --- /dev/null +++ b/sql/unit/update.sql @@ -0,0 +1,24 @@ +UPDATE unit SET + price_usd = $1, + unit_type = $2, + rooms = $3, + bathrooms = $4, + area = $5, + description = $6, + media = $7, + admin_tag = $8, + last_updated = $9 +WHERE id = $10 +RETURNING + id, + project_id, + price_usd, + unit_type as "unit_type: _", + rooms, + bathrooms, + area, + description, + media as "media: _", + admin_tag, + time_created, + last_updated; \ No newline at end of file diff --git a/src/dao/agent.rs b/src/dao/agent.rs index b311985..d5c0c4d 100644 --- a/src/dao/agent.rs +++ b/src/dao/agent.rs @@ -16,7 +16,7 @@ pub async fn get_agents_with_ids(conn: &PgPool, agent_ids: &Vec) -> Result } pub async fn update_agent(tx: &mut Transaction<'_, Postgres>, agent: Agent) -> Result { - sqlx::query_file_as!(Agent, "sql/agent/update.sql", agent.full_name, agent.credential, agent.credential_type as _, agent.last_updated).fetch_one(tx).await + sqlx::query_file_as!(Agent, "sql/agent/update.sql", agent.full_name, agent.credential, agent.credential_type as _, agent.last_updated, agent.id).fetch_one(tx).await } pub async fn delete_agent(tx: &mut Transaction<'_, Postgres>, agent_id: &Uuid) -> Result { diff --git a/src/dao/project.rs b/src/dao/project.rs index e69de29..8b50a7c 100644 --- a/src/dao/project.rs +++ b/src/dao/project.rs @@ -0,0 +1,16 @@ +use jl_types::domain::project::Project; +use sqlx::postgres::PgQueryResult; + + + +pub async fn insert() -> Result { + todo!() +} + +pub async fn delete() -> Result { + todo!() +} + +pub async fn update() -> Result { + todo!() +} \ No newline at end of file diff --git a/src/dao/unit.rs b/src/dao/unit.rs index e69de29..6f6e3a3 100644 --- a/src/dao/unit.rs +++ b/src/dao/unit.rs @@ -0,0 +1,20 @@ +use jl_types::domain::unit::Unit; +use sqlx::{PgPool, Transaction, Postgres, postgres::PgQueryResult}; +use uuid::Uuid; + + +pub async fn fetch_with_project_id(conn: &PgPool, project_id: &Uuid) -> Result, sqlx::Error> { + sqlx::query_file_as!(Unit, "sql/unit/fetch_with_project_id.sql", project_id).fetch_all(conn).await +} + +pub async fn delete(tx: &mut Transaction<'_, Postgres>, unit_id: &Uuid) -> Result { + sqlx::query_file!("sql/unit/delete.sql", unit_id).execute(tx).await +} + +pub async fn insert(tx: &mut Transaction<'_, Postgres>, unit: &Unit) -> Result { + sqlx::query_file_as!(Unit, "sql/unit/insert.sql", unit.id, unit.project_id, unit.price_usd, unit.unit_type as _, unit.rooms, unit.bathrooms, unit.area, unit.description, unit.media as _, unit.admin_tag, unit.time_created).fetch_one(tx).await +} + +pub async fn update(conn: &PgPool, unit: &Unit) -> Result { + sqlx::query_file_as!(Unit, "sql/unit/update.sql", unit.price_usd, unit.unit_type as _, unit.rooms, unit.bathrooms, unit.area, unit.description, unit.media as _, unit.admin_tag, unit.last_updated, unit.id).fetch_one(conn).await +} \ No newline at end of file