diff --git a/sql/location/fetch_with_ids.sql b/sql/location/fetch_with_ids.sql new file mode 100644 index 0000000..3cf8ccf --- /dev/null +++ b/sql/location/fetch_with_ids.sql @@ -0,0 +1 @@ +SELECT * FROM location WHERE id = ANY($1); \ No newline at end of file diff --git a/sqlx-data.json b/sqlx-data.json index 181b2e2..26b4fbc 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -311,6 +311,56 @@ }, "query": "INSERT INTO agent (\n id, full_name, time_created, last_updated\n) VALUES (\n $1, $2, $3, $3\n) RETURNING *" }, + "68da2e3f3ab23e76b332a9882d267deb63809de4f5dcf4c05616de9d38fab21f": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "country", + "ordinal": 1, + "type_info": "Varchar" + }, + { + "name": "province", + "ordinal": 2, + "type_info": "Varchar" + }, + { + "name": "city", + "ordinal": 3, + "type_info": "Varchar" + }, + { + "name": "district", + "ordinal": 4, + "type_info": "Varchar" + }, + { + "name": "google_maps_url", + "ordinal": 5, + "type_info": "Text" + } + ], + "nullable": [ + false, + false, + false, + false, + true, + true + ], + "parameters": { + "Left": [ + "UuidArray" + ] + } + }, + "query": "SELECT * FROM location WHERE id = ANY($1);" + }, "75508bc7b114d6f9915cadf9708c376d2ca1a6dcdd6bff24b1a717cba0ec2fca": { "describe": { "columns": [ diff --git a/src/dao/location.rs b/src/dao/location.rs index 56dbda4..0ce9b85 100644 --- a/src/dao/location.rs +++ b/src/dao/location.rs @@ -19,6 +19,16 @@ pub async fn insert_location( .fetch_one(conn) .await } + +pub async fn get_locations_with_ids( + conn: &PgPool, + ids: &Vec, +) -> Result, sqlx::error::Error> { + sqlx::query_file_as!(Location, "sql/location/fetch_with_ids.sql", ids) + .fetch_all(conn) + .await +} + pub async fn get_location_with_id( conn: &PgPool, id: &Uuid, diff --git a/src/service/admin.rs b/src/service/admin.rs index d652736..f6cab47 100644 --- a/src/service/admin.rs +++ b/src/service/admin.rs @@ -1,6 +1,6 @@ use actix_web_utils::extensions::typed_response::TypedHttpResponse; use remax_types::{ - domain::{location::Location, contact_info::ContactInformation}, + domain::{contact_info::ContactInformation, location::Location}, dto::{ agent::AgentContainer, payload::{ @@ -56,7 +56,6 @@ pub async fn create_new_location( success!(persisted_location) } - /// Needs to create a Property, a Property Details /// Create or reference an existing Location /// and an Agent @@ -96,36 +95,62 @@ pub async fn create_new_property( }) } - // // Update Methods // -pub async fn update_agent_info(conn: &PgPool, update_agent_payload: UpdateAgentPayload) -> TypedHttpResponse { +pub async fn update_agent_info( + conn: &PgPool, + update_agent_payload: UpdateAgentPayload, +) -> TypedHttpResponse { let mut tx = handle_tx!(conn.begin()); - let mut persisted_contact_infos = handle_db_read_op!(dao::contact_info::get_contact_infos_with_ids(conn, &vec![update_agent_payload.id])); - let persisted_contact_info = unwrap_or_not_found!(persisted_contact_infos.first_mut(), "agent contact infos"); + let mut persisted_contact_infos = handle_db_read_op!( + dao::contact_info::get_contact_infos_with_ids(conn, &vec![update_agent_payload.id]) + ); + let persisted_contact_info = + unwrap_or_not_found!(persisted_contact_infos.first_mut(), "agent contact infos"); update_agent_payload.update_contact_info(persisted_contact_info); - let updated_contact_info = handle_db_write_op!(dao::contact_info::update_contact_info(&mut tx, persisted_contact_info), tx); + let updated_contact_info = handle_db_write_op!( + dao::contact_info::update_contact_info(&mut tx, persisted_contact_info), + tx + ); handle_tx!(tx.commit()); success!(updated_contact_info) } -pub async fn update_listing(conn: &PgPool, update_property_payload: UpdateListingPayload) -> TypedHttpResponse { +pub async fn update_listing( + conn: &PgPool, + update_property_payload: UpdateListingPayload, +) -> TypedHttpResponse { let mut tx = handle_tx!(conn.begin()); - let mut persisted_properties = handle_db_read_op!(dao::property::get_properties_with_ids(conn, &vec![update_property_payload.property_id])); - let mut persisted_properties_details = handle_db_read_op!(dao::property_details::get_properties_with_ids(conn, &vec![update_property_payload.property_id])); + let mut persisted_properties = handle_db_read_op!(dao::property::get_properties_with_ids( + conn, + &vec![update_property_payload.property_id] + )); + let mut persisted_properties_details = + handle_db_read_op!(dao::property_details::get_properties_with_ids( + conn, + &vec![update_property_payload.property_id] + )); let persisted_property = unwrap_or_not_found!(persisted_properties.first_mut(), "properties"); - let persisted_property_details = unwrap_or_not_found!(persisted_properties_details.first_mut(), "property_details"); - + let persisted_property_details = + unwrap_or_not_found!(persisted_properties_details.first_mut(), "property_details"); + update_property_payload.update_listing(persisted_property, persisted_property_details); - let updated_property = handle_db_write_op!(dao::property::update_property(&mut tx, persisted_property), tx); - let updated_property_details = handle_db_write_op!(dao::property_details::update_property_details(&mut tx, persisted_property_details), tx); + let updated_property = handle_db_write_op!( + dao::property::update_property(&mut tx, persisted_property), + tx + ); + let updated_property_details = handle_db_write_op!( + dao::property_details::update_property_details(&mut tx, persisted_property_details), + tx + ); handle_tx!(tx.commit()); success!(PropertyWithDetails { - property: updated_property, details: updated_property_details + property: updated_property, + details: updated_property_details }) -} \ No newline at end of file +} diff --git a/src/service/mod.rs b/src/service/mod.rs index 92918b0..6e88d31 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1 +1,2 @@ pub mod admin; +pub mod read; diff --git a/src/service/read.rs b/src/service/read.rs new file mode 100644 index 0000000..ae5d015 --- /dev/null +++ b/src/service/read.rs @@ -0,0 +1,21 @@ +// TODO: Most important method: Get First page of property listings (Filters) +// TODO: Get individual property listing with all the info (contact, location, property, details) +// TODO: Get all agents + +use actix_web_utils::extensions::typed_response::TypedHttpResponse; +use remax_types::{ + domain::agent::Agent, + dto::property::{ListingContainer, PropertyContainer}, +}; + +pub async fn get_property_listings_paged() -> TypedHttpResponse { + todo!() +} + +pub async fn get_listing_container() -> TypedHttpResponse { + todo!() +} + +pub async fn get_all_agents() -> TypedHttpResponse> { + todo!() +}