diff --git a/sql/property/get_with_id.sql b/sql/property/get_with_id.sql new file mode 100644 index 0000000..3076207 --- /dev/null +++ b/sql/property/get_with_id.sql @@ -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; \ No newline at end of file diff --git a/sql/property/insert.sql b/sql/property/insert.sql new file mode 100644 index 0000000..f294a09 --- /dev/null +++ b/sql/property/insert.sql @@ -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; \ No newline at end of file diff --git a/sql/property/update.sql b/sql/property/update.sql new file mode 100644 index 0000000..450cae5 --- /dev/null +++ b/sql/property/update.sql @@ -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; \ No newline at end of file diff --git a/src/dao/property.rs b/src/dao/property.rs index 8b13789..6c73046 100644 --- a/src/dao/property.rs +++ b/src/dao/property.rs @@ -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 { + 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, 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 { + 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 +} \ No newline at end of file diff --git a/src/dao/trackable.rs b/src/dao/trackable.rs index 01c98a4..6db3d22 100644 --- a/src/dao/trackable.rs +++ b/src/dao/trackable.rs @@ -21,10 +21,20 @@ pub async fn insert_trackable( .await } -pub async fn get_trackable_with_id(conn: &PgPool, trackable_id: &Uuid) -> Result, sqlx::Error> { - sqlx::query_file_as!(Trackable, "sql/trackable/get_with_id.sql", trackable_id).fetch_optional(conn).await +pub async fn get_trackable_with_id( + conn: &PgPool, + trackable_id: &Uuid, +) -> Result, 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, sqlx::Error> { - sqlx::query_file_as!(Trackable, "sql/trackable/fetch_all_with_ip.sql", ip_addr).fetch_all(conn).await -} \ No newline at end of file +pub async fn fetch_trackables_with_ip_address( + conn: &PgPool, + ip_addr: String, +) -> Result, sqlx::Error> { + sqlx::query_file_as!(Trackable, "sql/trackable/fetch_all_with_ip.sql", ip_addr) + .fetch_all(conn) + .await +}