Added all admin insert & update functions except delete ones
This commit is contained in:
parent
f92b077f56
commit
5e0e8e7b20
@ -18,7 +18,7 @@ pub async fn insert_contact_info(
|
|||||||
.fetch_one(conn)
|
.fetch_one(conn)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
pub async fn get_properties_with_ids(
|
pub async fn get_contact_infos_with_ids(
|
||||||
conn: &PgPool,
|
conn: &PgPool,
|
||||||
ids: &Vec<Uuid>,
|
ids: &Vec<Uuid>,
|
||||||
) -> Result<Vec<ContactInformation>, sqlx::error::Error> {
|
) -> Result<Vec<ContactInformation>, sqlx::error::Error> {
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
use actix_web_utils::extensions::typed_response::TypedHttpResponse;
|
use actix_web_utils::extensions::typed_response::TypedHttpResponse;
|
||||||
use remax_types::{
|
use remax_types::{
|
||||||
domain::location::Location,
|
domain::{location::Location, contact_info::ContactInformation},
|
||||||
dto::{
|
dto::{
|
||||||
agent::AgentContainer,
|
agent::AgentContainer,
|
||||||
payload::{
|
payload::{
|
||||||
agent::{AgentWithContactInfo, NewAgentPayload},
|
agent::{AgentWithContactInfo, NewAgentPayload, UpdateAgentPayload},
|
||||||
location::NewLocationPayload,
|
location::NewLocationPayload,
|
||||||
property::{NewPropertyPayload, PropertyWithDetails},
|
property::{NewPropertyPayload, PropertyWithDetails, UpdateListingPayload},
|
||||||
},
|
},
|
||||||
property::PropertyContainer,
|
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};
|
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
|
// Insert Methods
|
||||||
/// 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<PropertyContainer> {
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create_new_agent_profile(
|
pub async fn create_new_agent_profile(
|
||||||
conn: &PgPool,
|
conn: &PgPool,
|
||||||
@ -90,3 +55,77 @@ pub async fn create_new_location(
|
|||||||
|
|
||||||
success!(persisted_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<PropertyContainer> {
|
||||||
|
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<ContactInformation> {
|
||||||
|
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<PropertyWithDetails> {
|
||||||
|
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
|
||||||
|
})
|
||||||
|
}
|
@ -44,6 +44,8 @@ macro_rules! success {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This macro just returns a TypedHttpResponse with a not found status code (404) and an error concatenated.
|
/// 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_export]
|
||||||
macro_rules! unwrap_or_not_found {
|
macro_rules! unwrap_or_not_found {
|
||||||
($e:expr, $what:literal) => {
|
($e:expr, $what:literal) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user