From 5e0e8e7b20f1322bcad35b9f57c1785444989aea Mon Sep 17 00:00:00 2001 From: Franklin Date: Fri, 10 Mar 2023 07:10:23 -0400 Subject: [PATCH] Added all admin insert & update functions except delete ones --- src/dao/contact_info.rs | 2 +- src/service/admin.rs | 121 ++++++++++++++++++++++++++-------------- src/utils/macros.rs | 2 + 3 files changed, 83 insertions(+), 42 deletions(-) diff --git a/src/dao/contact_info.rs b/src/dao/contact_info.rs index fd11eca..9278e83 100644 --- a/src/dao/contact_info.rs +++ b/src/dao/contact_info.rs @@ -18,7 +18,7 @@ pub async fn insert_contact_info( .fetch_one(conn) .await } -pub async fn get_properties_with_ids( +pub async fn get_contact_infos_with_ids( conn: &PgPool, ids: &Vec, ) -> Result, sqlx::error::Error> { diff --git a/src/service/admin.rs b/src/service/admin.rs index 5e6c913..d652736 100644 --- a/src/service/admin.rs +++ b/src/service/admin.rs @@ -1,12 +1,12 @@ use actix_web_utils::extensions::typed_response::TypedHttpResponse; use remax_types::{ - domain::location::Location, + domain::{location::Location, contact_info::ContactInformation}, dto::{ agent::AgentContainer, payload::{ - agent::{AgentWithContactInfo, NewAgentPayload}, + agent::{AgentWithContactInfo, NewAgentPayload, UpdateAgentPayload}, location::NewLocationPayload, - property::{NewPropertyPayload, PropertyWithDetails}, + property::{NewPropertyPayload, PropertyWithDetails, UpdateListingPayload}, }, property::PropertyContainer, }, @@ -15,44 +15,9 @@ use sqlx::PgPool; use crate::{dao, handle_db_read_op, handle_db_write_op, handle_tx, success, unwrap_or_not_found}; -/// Needs to create a Property, a Property Details -/// Create or reference an existing Location -/// and an Agent -/// Needs to be given all the property details, agent details, location details -pub async fn create_new_property( - conn: &PgPool, - new_property_payload: NewPropertyPayload, -) -> TypedHttpResponse { - let agent_ids = &vec![new_property_payload.agent_id]; - - let mut tx = handle_tx!(conn.begin()); - let persisted_agents = handle_db_read_op!(dao::agent::get_agents_with_ids(conn, agent_ids)); - let _ = unwrap_or_not_found!(persisted_agents.first(), "agents"); - let persisted_location = unwrap_or_not_found!( - handle_db_read_op!(dao::location::get_location_with_id( - conn, - &new_property_payload.location_id - )), - "locations" - ); - - let property_with_details: PropertyWithDetails = new_property_payload.into(); - let persisted_property = handle_db_write_op!( - dao::property::insert_property(&mut tx, &property_with_details.property), - tx - ); - let persisted_property_details = handle_db_write_op!( - dao::property_details::insert_property_details(&mut tx, &property_with_details.details), - tx - ); - - handle_tx!(tx.commit()); - success!(PropertyContainer { - property: persisted_property, - details: persisted_property_details, - location: persisted_location, - }) -} +// +// Insert Methods +// pub async fn create_new_agent_profile( conn: &PgPool, @@ -90,3 +55,77 @@ 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 +/// Needs to be given all the property details, agent details, location details +pub async fn create_new_property( + conn: &PgPool, + new_property_payload: NewPropertyPayload, +) -> TypedHttpResponse { + let agent_ids = &vec![new_property_payload.agent_id]; + + let mut tx = handle_tx!(conn.begin()); + let persisted_agents = handle_db_read_op!(dao::agent::get_agents_with_ids(conn, agent_ids)); + let _ = unwrap_or_not_found!(persisted_agents.first(), "agents"); + let persisted_location = unwrap_or_not_found!( + handle_db_read_op!(dao::location::get_location_with_id( + conn, + &new_property_payload.location_id + )), + "locations" + ); + + let property_with_details: PropertyWithDetails = new_property_payload.into(); + let persisted_property = handle_db_write_op!( + dao::property::insert_property(&mut tx, &property_with_details.property), + tx + ); + let persisted_property_details = handle_db_write_op!( + dao::property_details::insert_property_details(&mut tx, &property_with_details.details), + tx + ); + handle_tx!(tx.commit()); + + success!(PropertyContainer { + property: persisted_property, + details: persisted_property_details, + location: persisted_location, + }) +} + + +// +// Update Methods +// + +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"); + 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); + handle_tx!(tx.commit()); + + success!(updated_contact_info) +} + +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 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"); + + 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); + handle_tx!(tx.commit()); + + success!(PropertyWithDetails { + property: updated_property, details: updated_property_details + }) +} \ No newline at end of file diff --git a/src/utils/macros.rs b/src/utils/macros.rs index 14f0d97..ea467b3 100644 --- a/src/utils/macros.rs +++ b/src/utils/macros.rs @@ -44,6 +44,8 @@ macro_rules! success { } /// This macro just returns a TypedHttpResponse with a not found status code (404) and an error concatenated. +/// The literal should be a subject in plural form: +/// Agent -> agents #[macro_export] macro_rules! unwrap_or_not_found { ($e:expr, $what:literal) => {