Finished property dao

This commit is contained in:
Franklin 2023-05-03 14:19:39 -04:00
parent 375a224d11
commit 08d07b6373
5 changed files with 156 additions and 5 deletions

View File

@ -0,0 +1,18 @@
SELECT
id,
project_id,
realtor_id,
media as "media: _",
property_type as "property_type: _",
property_sale_type as "property_sale_type: _",
country,
city,
district,
price_usd,
rooms,
bathrooms,
area,
admin_tag,
time_created,
last_updated
FROM property WHERE id = $1;

36
sql/property/insert.sql Normal file
View File

@ -0,0 +1,36 @@
INSERT INTO property (
id,
project_id,
realtor_id,
media,
property_type,
property_sale_type,
country,
city,
district,
price_usd,
rooms,
bathrooms,
area,
admin_tag,
time_created,
last_updated
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $15
) RETURNING
id,
project_id,
realtor_id,
media as "media: _",
property_type as "property_type: _",
property_sale_type as "property_sale_type: _",
country,
city,
district,
price_usd,
rooms,
bathrooms,
area,
admin_tag,
time_created,
last_updated;

33
sql/property/update.sql Normal file
View File

@ -0,0 +1,33 @@
UPDATE property SET
project_id = $2,
realtor_id = $3,
media = $4,
property_type = $5,
property_sale_type = $6,
country = $7,
city = $8,
district = $9,
price_usd = $10,
rooms = $11,
bathrooms = $12,
area = $13,
admin_tag = $14,
last_updated = $15
WHERE id = $1
RETURNING
id,
project_id,
realtor_id,
media as "media: _",
property_type as "property_type: _",
property_sale_type as "property_sale_type: _",
country,
city,
district,
price_usd,
rooms,
bathrooms,
area,
admin_tag,
time_created,
last_updated;

View File

@ -1 +1,55 @@
use realtor_lp_types::domain::property::Property;
use sqlx::PgPool;
use uuid::Uuid;
pub async fn insert_property(conn: &PgPool, property: Property) -> Result<Property, sqlx::Error> {
sqlx::query_file_as!(
Property,
"sql/property/insert.sql",
property.id,
property.project_id,
property.realtor_id,
property.media as _,
property.property_type as _,
property.property_sale_type as _,
property.country,
property.city,
property.district,
property.price_usd,
property.rooms,
property.bathrooms,
property.area,
property.admin_tag,
property.time_created
)
.fetch_one(conn)
.await
}
pub async fn get_property_with_id(conn: &PgPool, property_id: &Uuid) -> Result<Option<Property>, sqlx::Error> {
sqlx::query_file_as!(Property, "sql/property/get_with_id.sql", property_id).fetch_optional(conn).await
}
pub async fn update_property(conn: &PgPool, property: Property) -> Result<Property, sqlx::Error> {
sqlx::query_file_as!(
Property,
"sql/property/update.sql",
property.id,
property.project_id,
property.realtor_id,
property.media as _,
property.property_type as _,
property.property_sale_type as _,
property.country,
property.city,
property.district,
property.price_usd,
property.rooms,
property.bathrooms,
property.area,
property.admin_tag,
property.last_updated
)
.fetch_one(conn)
.await
}

View File

@ -21,10 +21,20 @@ pub async fn insert_trackable(
.await .await
} }
pub async fn get_trackable_with_id(conn: &PgPool, trackable_id: &Uuid) -> Result<Option<Trackable>, sqlx::Error> { pub async fn get_trackable_with_id(
sqlx::query_file_as!(Trackable, "sql/trackable/get_with_id.sql", trackable_id).fetch_optional(conn).await conn: &PgPool,
trackable_id: &Uuid,
) -> Result<Option<Trackable>, sqlx::Error> {
sqlx::query_file_as!(Trackable, "sql/trackable/get_with_id.sql", trackable_id)
.fetch_optional(conn)
.await
} }
pub async fn fetch_trackables_with_ip_address(conn: &PgPool, ip_addr: String) -> Result<Vec<Trackable>, sqlx::Error> { pub async fn fetch_trackables_with_ip_address(
sqlx::query_file_as!(Trackable, "sql/trackable/fetch_all_with_ip.sql", ip_addr).fetch_all(conn).await conn: &PgPool,
} ip_addr: String,
) -> Result<Vec<Trackable>, sqlx::Error> {
sqlx::query_file_as!(Trackable, "sql/trackable/fetch_all_with_ip.sql", ip_addr)
.fetch_all(conn)
.await
}