diff --git a/sql/property_arrangement/update.sql b/sql/property_arrangement/update.sql index 07d4896..18e4047 100644 --- a/sql/property_arrangement/update.sql +++ b/sql/property_arrangement/update.sql @@ -3,7 +3,7 @@ UPDATE property_arrangement SET currency = $3, arrangement = $4, last_updated = $5 -WHERE property_id = $1 +WHERE property_id = $1 AND arrangement = $4 RETURNING property_id, price, diff --git a/sqlx-data.json b/sqlx-data.json index 95d48b7..0f75209 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -297,60 +297,6 @@ }, "query": "SELECT * FROM realtor WHERE shortcode = $1;" }, - "22aa2761cf17dba99859d46e40c8b5e04a112f2fdae12494f4aabc384cde4727": { - "describe": { - "columns": [ - { - "name": "property_id", - "ordinal": 0, - "type_info": "Uuid" - }, - { - "name": "price", - "ordinal": 1, - "type_info": "Float8" - }, - { - "name": "currency", - "ordinal": 2, - "type_info": "Varchar" - }, - { - "name": "arrangement: _", - "ordinal": 3, - "type_info": "Varchar" - }, - { - "name": "time_created", - "ordinal": 4, - "type_info": "Timestamptz" - }, - { - "name": "last_updated", - "ordinal": 5, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false, - false, - false, - false, - false, - false - ], - "parameters": { - "Left": [ - "Uuid", - "Float8", - "Varchar", - "Varchar", - "Timestamptz" - ] - } - }, - "query": "UPDATE property_arrangement SET \n price = $2,\n currency = $3,\n arrangement = $4,\n last_updated = $5\nWHERE property_id = $1\nRETURNING \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated;" - }, "3acc0efdfdf6fd4e86b0f197cd27b7378507b82915127b76a6cf76a8f7cb5618": { "describe": { "columns": [ @@ -953,6 +899,60 @@ }, "query": "INSERT INTO property (\n id,\n project_id,\n realtor_id,\n media,\n property_type,\n property_sale_type,\n country,\n city,\n district,\n order_index,\n thumbnail_format,\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $17\n) RETURNING \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n order_index,\n thumbnail_format as \"thumbnail_format: _\",\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated;" }, + "6b566fd0b806219fa856991fc4bc93157c989d418562dc89268c9fb9363424bd": { + "describe": { + "columns": [ + { + "name": "property_id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "price", + "ordinal": 1, + "type_info": "Float8" + }, + { + "name": "currency", + "ordinal": 2, + "type_info": "Varchar" + }, + { + "name": "arrangement: _", + "ordinal": 3, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 4, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 5, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Left": [ + "Uuid", + "Float8", + "Varchar", + "Text", + "Timestamptz" + ] + } + }, + "query": "UPDATE property_arrangement SET \n price = $2,\n currency = $3,\n arrangement = $4,\n last_updated = $5\nWHERE property_id = $1 AND arrangement = $4\nRETURNING \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated;" + }, "7464713d873ea4ad81fdbaef816eb57ce3bdcdf3aa669036e05158e230abd82c": { "describe": { "columns": [ diff --git a/src/routes/arrangement.rs b/src/routes/arrangement.rs new file mode 100644 index 0000000..d12537a --- /dev/null +++ b/src/routes/arrangement.rs @@ -0,0 +1,18 @@ +use std::sync::Arc; + +use actix_web::{ + post, + web::{self, Json}, +}; +use actix_web_utils::extensions::typed_response::TypedHttpResponse; +use realtor_lp_types::{ + domain::{price::PropertyPrice}, dto::payloads::{arrangement::PropertyPriceForCreationPayload}, +}; +use sqlx::PgPool; + +use crate::services; + +#[post("/arrangement")] +pub async fn add_property_arrangement(db_conn: web::Data>, arrangement: Json) -> TypedHttpResponse { + services::arrangement::add_arrangement_to_property(&db_conn, arrangement.0).await +} \ No newline at end of file diff --git a/src/routes/main_router.rs b/src/routes/main_router.rs index bc8d4a4..5ff359c 100644 --- a/src/routes/main_router.rs +++ b/src/routes/main_router.rs @@ -13,7 +13,7 @@ use sqlx::PgPool; use crate::utils::s3; -use super::{click, property, realtor, trackable, view}; +use super::{click, property, realtor, trackable, view, arrangement}; pub const HOST_ADDR: &str = "0.0.0.0"; pub const HOST_PORT: u16 = 8080; @@ -37,7 +37,8 @@ pub async fn start_all_routes(start_time: i64, db_conn: Arc) -> Result<( .service(property::new_property) .service(property::update_property) .service(realtor::new_realtor_profile) - .service(realtor::update_realtor_profile), + .service(realtor::update_realtor_profile) + .service(arrangement::add_property_arrangement), ) .service( web::scope("/public") diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 6727f6b..2e4eefa 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -12,3 +12,4 @@ pub mod realtor; pub mod trackable; #[allow(unused)] pub mod view; +pub mod arrangement; diff --git a/src/services/arrangement.rs b/src/services/arrangement.rs new file mode 100644 index 0000000..cabad95 --- /dev/null +++ b/src/services/arrangement.rs @@ -0,0 +1,18 @@ +use actix_web_utils::extensions::typed_response::TypedHttpResponse; +use realtor_lp_types::{domain::price::PropertyPrice, dto::payloads::arrangement::PropertyPriceForCreationPayload}; +use sqlx::PgPool; + +use crate::{success, handle_tx, handle_db_write_op, dao, handle_db_read_op}; + + +pub async fn add_arrangement_to_property(conn: &PgPool, arrangement: PropertyPriceForCreationPayload) -> TypedHttpResponse { + let mut tx = handle_tx!(conn.begin()); + let existing_arrangement = handle_db_read_op!(dao::property_arrangement::fetch_arrangements_with_property_id(conn, &arrangement.property_id)); + if existing_arrangement.iter().any(|persisted_arrangement| persisted_arrangement.arrangement == arrangement.arrangement) { + let persisted_arrangement = handle_db_write_op!(dao::property_arrangement::update_arrangement(&mut tx, arrangement.into()), tx); + success!(persisted_arrangement) + } else { + let persisted_arrangement = handle_db_write_op!(dao::property_arrangement::insert_arrangement(&mut tx, arrangement.into()), tx); + success!(persisted_arrangement) + } +} \ No newline at end of file diff --git a/src/services/mod.rs b/src/services/mod.rs index 538bf95..0ffca89 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -10,3 +10,4 @@ pub mod realtor; pub mod trackable; #[allow(unused)] pub mod view; +pub mod arrangement; \ No newline at end of file