Added all dao methods for project

This commit is contained in:
Franklin 2023-03-19 11:21:15 -04:00
parent 719281492b
commit 9341596265
16 changed files with 332 additions and 83 deletions

View File

@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS "project" (
id UUID PRIMARY KEY,
project_state VARCHAR NOT NULL,
project_type VARCHAR NOT NULL,
project_condition VARCHAR NOT NULL,
agent_id UUID NOT NULL,
@ -7,6 +8,7 @@ CREATE TABLE IF NOT EXISTS "project" (
title VARCHAR,
description TEXT NOT NULL,
admin_tag VARCHAR,
finish_date TIMESTAMP NOT NULL,
floors SMALLINT NOT NULL,
media TEXT NOT NULL,
time_created TIMESTAMPTZ NOT NULL,

View File

@ -1,5 +1,6 @@
SELECT
id,
project_state as "project_state: _",
project_type as "project_type: _",
project_condition as "project_condition: _",
agent_id,
@ -7,6 +8,7 @@ location_id,
title,
description,
admin_tag,
finish_date,
floors,
media as "media: _",
time_created,

View File

@ -0,0 +1,26 @@
SELECT
p.id,
p.project_state as "project_state: _",
p.project_type as "project_type: _",
p.project_condition as "project_condition: _",
p.agent_id,
p.location_id,
p.title,
p.description,
p.admin_tag,
p.finish_date,
p.floors,
p.media as "media: _",
p.time_created,
p.last_updated
FROM project p, location l
WHERE p.location_id = l.id
-- Filters here:
AND (LOWER(l.city) LIKE '%' || LOWER($1) || '%' OR $1 IS null) -- City Filter
AND (LOWER(l.district) LIKE '%' || LOWER($2) || '%' OR $2 IS null) -- District Filter
AND (p.project_type = $3 OR $3 IS null) -- ProjectType
AND (p.project_condition = $4 OR $4 IS null) -- ProjectCondition
AND (p.project_state = $5 OR $5 IS null) -- ProjectState
-- End of filters
ORDER BY p.time_created DESC
LIMIT 50 OFFSET $6;

View File

@ -1,14 +1,16 @@
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
SELECT
id,
project_state as "project_state: _",
project_type as "project_type: _",
project_condition as "project_condition: _",
agent_id,
location_id,
title,
description,
admin_tag,
finish_date,
floors,
media as "media: _",
time_created,
last_updated
FROM project WHERE id = $1;

View File

@ -1,5 +1,6 @@
INSERT INTO project (
id,
project_state,
project_type,
project_condition,
agent_id,
@ -7,14 +8,16 @@ INSERT INTO project (
title,
description,
admin_tag,
finish_date,
floors,
media,
time_created,
last_updated
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $13
) RETURNING
id,
project_state as "project_state: _",
project_type as "project_type: _",
project_condition as "project_condition: _",
agent_id,
@ -22,6 +25,7 @@ location_id,
title,
description,
admin_tag,
finish_date,
floors,
media as "media: _",
time_created,

View File

@ -1,11 +1,29 @@
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,
project_state = $1,
project_type = $2,
project_condition = $3,
agent_id = $4,
location_id = $5,
title = $6,
description = $7,
admin_tag = $8,
finish_date = $9,
floors = $10,
media = $11,
last_updated = $12
WHERE id = $13
RETURNING
id,
project_state as "project_state: _",
project_type as "project_type: _",
project_condition as "project_condition: _",
agent_id,
location_id,
title,
description,
admin_tag,
finish_date,
floors,
media as "media: _",
time_created,
last_updated;

View File

@ -1,24 +1,64 @@
use jl_types::domain::agent::Agent;
use sqlx::{Transaction, Postgres, PgPool, postgres::PgQueryResult};
use sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction};
use uuid::Uuid;
pub async fn insert_agent(tx: &mut Transaction<'_, Postgres>, agent: &Agent) -> Result<Agent, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/insert.sql", agent.id, agent.full_name, agent.credential, agent.credential_type as _, agent.time_created).fetch_one(tx).await
pub async fn insert_agent(
tx: &mut Transaction<'_, Postgres>,
agent: &Agent,
) -> Result<Agent, sqlx::Error> {
sqlx::query_file_as!(
Agent,
"sql/agent/insert.sql",
agent.id,
agent.full_name,
agent.credential,
agent.credential_type as _,
agent.time_created
)
.fetch_one(tx)
.await
}
pub async fn get_agent_with_id(conn: &PgPool, agent_id: &Uuid) -> Result<Option<Agent>, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/get_with_id.sql", agent_id).fetch_optional(conn).await
pub async fn get_agent_with_id(
conn: &PgPool,
agent_id: &Uuid,
) -> Result<Option<Agent>, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/get_with_id.sql", agent_id)
.fetch_optional(conn)
.await
}
pub async fn get_agents_with_ids(conn: &PgPool, agent_ids: &Vec<Uuid>) -> Result<Vec<Agent>, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/fetch_with_ids.sql", agent_ids).fetch_all(conn).await
pub async fn get_agents_with_ids(
conn: &PgPool,
agent_ids: &Vec<Uuid>,
) -> Result<Vec<Agent>, sqlx::Error> {
sqlx::query_file_as!(Agent, "sql/agent/fetch_with_ids.sql", agent_ids)
.fetch_all(conn)
.await
}
pub async fn update_agent(tx: &mut Transaction<'_, Postgres>, agent: Agent) -> Result<Agent, sqlx::Error> {
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 update_agent(
tx: &mut Transaction<'_, Postgres>,
agent: Agent,
) -> Result<Agent, sqlx::Error> {
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<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/agent/delete.sql", agent_id).execute(tx).await
}
pub async fn delete_agent(
tx: &mut Transaction<'_, Postgres>,
agent_id: &Uuid,
) -> Result<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/agent/delete.sql", agent_id)
.execute(tx)
.await
}

View File

@ -1,20 +1,45 @@
use jl_types::domain::location::Location;
use sqlx::{Transaction, Postgres, postgres::PgQueryResult, PgPool};
use sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction};
use uuid::Uuid;
pub async fn insert_location(tx: &mut Transaction<'_, Postgres>, location: Location) -> Result<Location, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/insert.sql", location.id, location.city, location.district).fetch_one(tx).await
pub async fn insert_location(
tx: &mut Transaction<'_, Postgres>,
location: Location,
) -> Result<Location, sqlx::Error> {
sqlx::query_file_as!(
Location,
"sql/location/insert.sql",
location.id,
location.city,
location.district
)
.fetch_one(tx)
.await
}
pub async fn get_location_with_id(conn: &PgPool, location_id: &Uuid) -> Result<Option<Location>, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/get_with_id.sql", location_id).fetch_optional(conn).await
pub async fn get_location_with_id(
conn: &PgPool,
location_id: &Uuid,
) -> Result<Option<Location>, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/get_with_id.sql", location_id)
.fetch_optional(conn)
.await
}
pub async fn get_locations_in_city(conn: &PgPool, city: &String) -> Result<Vec<Location>, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/fetch_with_city.sql", city).fetch_all(conn).await
pub async fn get_locations_in_city(
conn: &PgPool,
city: &String,
) -> Result<Vec<Location>, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/fetch_with_city.sql", city)
.fetch_all(conn)
.await
}
pub async fn delete_location(tx: &mut Transaction<'_, Postgres>, location_id: Uuid) -> Result<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/location/delete.sql", location_id).execute(tx).await
}
pub async fn delete_location(
tx: &mut Transaction<'_, Postgres>,
location_id: Uuid,
) -> Result<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/location/delete.sql", location_id)
.execute(tx)
.await
}

View File

@ -1,5 +1,5 @@
pub mod main_dao;
pub mod project;
pub mod agent;
pub mod location;
pub mod unit;
pub mod main_dao;
pub mod project;
pub mod unit;

View File

@ -1,16 +1,103 @@
use jl_types::domain::project::Project;
use sqlx::postgres::PgQueryResult;
use jl_types::{domain::{project::Project, project_state::ProjectState}, dto::filters::Filter};
use sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction};
use uuid::Uuid;
pub async fn insert() -> Result<Project, sqlx::Error> {
todo!()
pub async fn insert(
tx: &mut Transaction<'_, Postgres>,
project: &Project,
) -> Result<Project, sqlx::Error> {
sqlx::query_file_as!(
Project,
"sql/project/insert.sql",
project.id,
project.project_state as _,
project.project_type as _,
project.project_condition as _,
project.agent_id,
project.location_id,
project.title,
project.description,
project.admin_tag,
project.finish_date,
project.floors,
project.media as _,
project.time_created
)
.fetch_one(tx)
.await
}
pub async fn delete() -> Result<PgQueryResult, sqlx::Error> {
todo!()
pub async fn delete(
tx: &mut Transaction<'_, Postgres>,
project_id: &Uuid,
) -> Result<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/project/delete.sql", project_id)
.execute(tx)
.await
}
pub async fn update() -> Result<Project, sqlx::Error> {
todo!()
}
pub async fn update(
tx: &mut Transaction<'_, Postgres>,
project: &Project,
) -> Result<Project, sqlx::Error> {
sqlx::query_file_as!(
Project,
"sql/project/update.sql",
project.project_state as _,
project.project_type as _,
project.project_condition as _,
project.agent_id,
project.location_id,
project.title,
project.description,
project.admin_tag,
project.finish_date,
project.floors,
project.media as _,
project.last_updated,
project.id
).fetch_one(tx).await
}
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
}
pub async fn fetch_by_ids(
conn: &PgPool,
project_ids: &Vec<Uuid>,
) -> Result<Vec<Project>, sqlx::Error> {
sqlx::query_file_as!(Project, "sql/project/fetch_by_ids.sql", project_ids).fetch_all(conn).await
}
pub async fn fetch_with_filters_paged(
conn: &PgPool,
filters: &Vec<Filter>,
page: &i64,
) -> Result<Vec<Project>, sqlx::Error> {
let offset = (page - 1) * 50;
let mut city_filter = None;
let mut district_filter = None;
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),
}
}
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
}

View File

@ -1,20 +1,60 @@
use jl_types::domain::unit::Unit;
use sqlx::{PgPool, Transaction, Postgres, postgres::PgQueryResult};
use sqlx::{postgres::PgQueryResult, PgPool, Postgres, Transaction};
use uuid::Uuid;
pub async fn fetch_with_project_id(conn: &PgPool, project_id: &Uuid) -> Result<Vec<Unit>, sqlx::Error> {
sqlx::query_file_as!(Unit, "sql/unit/fetch_with_project_id.sql", project_id).fetch_all(conn).await
pub async fn fetch_with_project_id(
conn: &PgPool,
project_id: &Uuid,
) -> Result<Vec<Unit>, 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<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/unit/delete.sql", unit_id).execute(tx).await
pub async fn delete(
tx: &mut Transaction<'_, Postgres>,
unit_id: &Uuid,
) -> Result<PgQueryResult, sqlx::Error> {
sqlx::query_file!("sql/unit/delete.sql", unit_id)
.execute(tx)
.await
}
pub async fn insert(tx: &mut Transaction<'_, Postgres>, unit: &Unit) -> Result<Unit, sqlx::Error> {
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
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<Unit, sqlx::Error> {
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
}
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
}

View File

@ -11,7 +11,7 @@ pub mod utils;
#[tokio::main]
async fn main() {
let start_time = Utc::now().timestamp_millis();
let db_conn = Arc::new(main_dao::connect_to_database().await.unwrap());
// Pass shared state to server and start it

View File

@ -18,16 +18,17 @@ pub async fn start_all_routes(start_time: i64, db_conn: Arc<PgPool>) -> Result<(
.wrap(cors_policy)
.app_data(client_state.clone())
.app_data(web::Data::new(db_conn.clone()))
.service(web::scope("/admin")
/*.service(super::admin::create_new_agent_profile)
.service(super::admin::create_new_location)
.service(super::admin::create_new_property)
.service(super::admin::update_agent_info)
.service(super::admin::update_listing))
.service(web::scope("/read")
.service(super::read::get_all_agents)
.service(super::read::get_listing_container)
.service(super::read::get_property_listings_paged)*/)
.service(
web::scope("/admin"), /*.service(super::admin::create_new_agent_profile)
.service(super::admin::create_new_location)
.service(super::admin::create_new_property)
.service(super::admin::update_agent_info)
.service(super::admin::update_listing))
.service(web::scope("/read")
.service(super::read::get_all_agents)
.service(super::read::get_listing_container)
.service(super::read::get_property_listings_paged)*/
)
})
.bind((HOST_ADDR, HOST_PORT))?
.run();

View File

@ -1 +1 @@
pub mod main_router;
pub mod main_router;

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@