From b862782fd0550efa2badb65d68c75c95e5f6aad5 Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 17 May 2023 16:45:19 -0400 Subject: [PATCH] realtor last commit :( --- migrations/2_property.sql | 1 - src/dao/realtor.rs | 10 +++++----- src/routes/main_router.rs | 2 +- src/services/arrangement.rs | 4 ++-- src/services/property.rs | 4 ++-- src/services/realtor.rs | 8 ++++---- src/utils/macros.rs | 6 ++++++ 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/migrations/2_property.sql b/migrations/2_property.sql index 7521e33..bb18ae7 100644 --- a/migrations/2_property.sql +++ b/migrations/2_property.sql @@ -10,7 +10,6 @@ CREATE TABLE IF NOT EXISTS "property" ( district VARCHAR NOT NULL, order_index INT NOT NULL, thumbnail_format VARCHAR NOT NULL, - price_usd FLOAT8 NOT NULL, rooms SMALLINT NOT NULL, bathrooms FLOAT4 NOT NULL, area FLOAT4 NOT NULL, diff --git a/src/dao/realtor.rs b/src/dao/realtor.rs index 3b9d8e2..3a0a106 100644 --- a/src/dao/realtor.rs +++ b/src/dao/realtor.rs @@ -1,8 +1,8 @@ use realtor_lp_types::domain::realtor::Realtor; -use sqlx::PgPool; +use sqlx::{PgPool, Transaction, Postgres}; use uuid::Uuid; -pub async fn insert_realtor(conn: &PgPool, realtor: Realtor) -> Result { +pub async fn insert_realtor(tx: &mut Transaction<'_, Postgres>, realtor: Realtor) -> Result { sqlx::query_file_as!( Realtor, "sql/realtor/insert.sql", @@ -18,7 +18,7 @@ pub async fn insert_realtor(conn: &PgPool, realtor: Realtor) -> Result Result { +pub async fn update_realtor(tx: &mut Transaction<'_, Postgres>, realtor: Realtor) -> Result { sqlx::query_file_as!( Realtor, "sql/realtor/update.sql", @@ -59,6 +59,6 @@ pub async fn update_realtor(conn: &PgPool, realtor: Realtor) -> Result) -> Result<(), std::io::Error> { let client_state = web::Data::new(Arc::new(Client::new())); diff --git a/src/services/arrangement.rs b/src/services/arrangement.rs index cabad95..88873a5 100644 --- a/src/services/arrangement.rs +++ b/src/services/arrangement.rs @@ -10,9 +10,9 @@ pub async fn add_arrangement_to_property(conn: &PgPool, arrangement: PropertyPri 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) + success!(persisted_arrangement, tx); } else { let persisted_arrangement = handle_db_write_op!(dao::property_arrangement::insert_arrangement(&mut tx, arrangement.into()), tx); - success!(persisted_arrangement) + success!(persisted_arrangement, tx); } } \ No newline at end of file diff --git a/src/services/property.rs b/src/services/property.rs index 0fe8613..3da3db8 100644 --- a/src/services/property.rs +++ b/src/services/property.rs @@ -45,7 +45,7 @@ pub async fn create_property( success!(handle_db_write_op!( dao::property::insert_property(&mut transaction, property.into()), transaction - )) + ), transaction); } pub async fn udpate_property( @@ -85,7 +85,7 @@ pub async fn udpate_property( success!(handle_db_write_op!( dao::property::update_property(&mut transaction, property_insertable), transaction - )) + ), transaction); } pub async fn fetch_realtor_properties_paged( diff --git a/src/services/realtor.rs b/src/services/realtor.rs index 51b0a96..9595163 100644 --- a/src/services/realtor.rs +++ b/src/services/realtor.rs @@ -13,9 +13,9 @@ pub async fn new_realtor_profile( ) -> TypedHttpResponse { let mut transaction = handle_tx!(conn.begin()); success!(handle_db_write_op!( - dao::realtor::insert_realtor(conn, realtor.into()), + dao::realtor::insert_realtor(&mut transaction, realtor.into()), transaction - )); + ), transaction); } pub async fn update_realtor_profile( @@ -27,9 +27,9 @@ pub async fn update_realtor_profile( let mut realtor_insertable: Realtor = realtor.into(); realtor_insertable.id = realtor_id; success!(handle_db_write_op!( - dao::realtor::update_realtor(conn, realtor_insertable), + dao::realtor::update_realtor(&mut transaction, realtor_insertable), transaction - )); + ), transaction); } pub async fn get_realtor_by_shortcode( diff --git a/src/utils/macros.rs b/src/utils/macros.rs index 3728739..ddc1193 100644 --- a/src/utils/macros.rs +++ b/src/utils/macros.rs @@ -41,6 +41,12 @@ macro_rules! success { ($e:expr) => { return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_response(200, $e) }; + ($e:expr, $tx:expr) => { + + let resp = actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_response(200, $e); + handle_tx!($tx.commit()); + return resp; + }; } /// This macro just returns a TypedHttpResponse with a not found status code (404) and an error concatenated.