Added daos

This commit is contained in:
Franklin 2023-05-08 14:28:19 -04:00
parent 2e5e47c2be
commit dd200f0138
13 changed files with 810 additions and 401 deletions

View File

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS "property_arrangement" (
property_id UUID PRIMARY KEY,
price_usd FLOAT8 NOT NULL,
price FLOAT8 NOT NULL,
currency VARCHAR NOT NULL,
arrangement VARCHAR NOT NULL,
time_created TIMESTAMPTZ NOT NULL,

View File

@ -1,22 +1,37 @@
SELECT
id,
project_id,
realtor_id,
media as "media: _",
property_type as "property_type: _",
property_sale_type as "property_sale_type: _",
country,
city,
district,
order_index,
thumbnail_format as "thumbnail_format: _",
rooms,
bathrooms,
area,
parking_spots,
admin_tag,
time_created,
last_updated
FROM property WHERE realtor_id = $1
ORDER BY time_created DESC
p.id,
p.project_id,
p.realtor_id,
p.media as "media: _",
p.property_type as "property_type: _",
p.property_sale_type as "property_sale_type: _",
p.country,
p.city,
p.district,
p.order_index,
p.thumbnail_format as "thumbnail_format: _",
p.rooms,
p.bathrooms,
p.area,
p.parking_spots,
p.admin_tag,
p.time_created,
p.last_updated
FROM property p, property_arrangement pa
WHERE p.realtor_id = $1
AND pa.property_id = p.id
-- FILTERS
AND (pa.arrangement = $3 OR $3 IS null) -- Arrangement
AND (p.property_type = $4 OR $4 IS null) -- Property Type
AND (p.country = $5 OR $5 IS null) -- Country
AND (p.city = $6 OR $6 IS null) -- City
AND (p.district = $7 OR $7 IS null) -- District
AND (p.rooms = $8 OR $8 IS null) -- Room amount
AND (p.bathrooms = $9 OR $9 IS null) -- Bathroom amount
AND (p.area >= $10 OR $10 IS null) -- Area greater than
AND (p.parking_spots = $11 OR $11 IS null) -- Parking spots amount
AND (pa.price <= $12 OR $12 IS null) -- Price Less than
AND (pa.price >= $13 OR $13 IS null) -- Price Greater than
-- END OF FILTERS
ORDER BY p.time_created DESC
LIMIT 25 OFFSET $2;

View File

@ -1,6 +1,6 @@
SELECT
property_id,
price_usd,
price,
currency,
arrangement as "arrangement: _",
time_created,

View File

@ -0,0 +1,9 @@
SELECT
property_id,
price,
currency,
arrangement as "arrangement: _",
time_created,
last_updated
FROM property_arrangement
WHERE property_id = ANY($1);

View File

@ -1,6 +1,6 @@
INSERT INTO property_arrangement (
property_id,
price_usd,
price,
currency,
arrangement,
time_created,
@ -9,7 +9,7 @@ INSERT INTO property_arrangement (
$1, $2, $3, $4, $5, $5
) RETURNING
property_id,
price_usd,
price,
currency,
arrangement as "arrangement: _",
time_created,

View File

@ -1,12 +1,12 @@
UPDATE property_arrangement SET
price_usd = $2,
price = $2,
currency = $3,
arrangement = $4,
last_updated = $5
WHERE property_id = $1
RETURNING
property_id,
price_usd,
price,
currency,
arrangement as "arrangement: _",
time_created,

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,8 @@ pub mod project;
#[allow(unused)]
pub mod property;
#[allow(unused)]
pub mod property_arrangement;
#[allow(unused)]
pub mod realtor;
#[allow(unused)]
pub mod trackable;

View File

@ -1,4 +1,4 @@
use realtor_lp_types::domain::property::Property;
use realtor_lp_types::{domain::property::Property, dto::filter::Filter};
use sqlx::{PgPool, Postgres, Transaction};
use uuid::Uuid;
@ -43,14 +43,55 @@ pub async fn get_property_with_id(
pub async fn fetch_with_realtor_id_paged(
conn: &PgPool,
realtor_id: &Uuid,
filters: &Vec<Filter>,
page: &i64,
) -> Result<Vec<Property>, sqlx::Error> {
let offset = (page - 1) * 25;
let mut arrangement_filter = None;
let mut property_type_filter = None;
let mut country_filter = None;
let mut city_filter = None;
let mut district_filter = None;
let mut room_count_filter = None;
let mut bathroom_count_filter = None;
let mut min_area_filter = None;
let mut parking_spots_count_filter = None;
let mut min_price_filter = None;
let mut max_price_filter = None;
for filter in filters {
match filter {
Filter::Country(country) => country_filter = Some(country),
Filter::City(city) => city_filter = Some(city),
Filter::District(district) => district_filter = Some(district),
Filter::PriceGreaterThan(price_greater_than) => min_price_filter = Some(price_greater_than),
Filter::PriceLessThan(price_less_than) => max_price_filter = Some(price_less_than),
Filter::Rooms(room_count) => room_count_filter = Some(room_count),
Filter::Bathrooms(bathroom_count) => bathroom_count_filter = Some(bathroom_count),
Filter::PropertyType(property_type) => property_type_filter = Some(property_type),
Filter::MinArea(min_area) => min_area_filter = Some(min_area),
Filter::ParkingSpots(parking_spots_count) => parking_spots_count_filter = Some(parking_spots_count),
Filter::Arrangement(arrangement) => arrangement_filter = Some(arrangement),
}
}
sqlx::query_file_as!(
Property,
"sql/property/fetch_with_realtor_id_paged.sql",
realtor_id,
offset
offset,
arrangement_filter as _,
property_type_filter as _,
country_filter,
city_filter,
district_filter,
room_count_filter,
bathroom_count_filter,
min_area_filter,
parking_spots_count_filter,
max_price_filter,
min_price_filter,
)
.fetch_all(conn)
.await

View File

@ -0,0 +1,63 @@
use realtor_lp_types::domain::price::PropertyPrice;
use sqlx::{PgPool, Postgres, Transaction};
use uuid::Uuid;
pub async fn insert_arrangement<'a>(
tx: &mut Transaction<'a, Postgres>,
arrangement: PropertyPrice,
) -> Result<PropertyPrice, sqlx::Error> {
sqlx::query_file_as!(
PropertyPrice,
"sql/property_arrangement/insert.sql",
arrangement.property_id,
arrangement.price,
arrangement.currency,
arrangement.arrangement as _,
arrangement.time_created,
)
.fetch_one(tx)
.await
}
pub async fn fetch_arrangements_with_property_id(
conn: &PgPool,
property_id: &Uuid,
) -> Result<Vec<PropertyPrice>, sqlx::Error> {
sqlx::query_file_as!(
PropertyPrice,
"sql/property_arrangement/fetch_with_property_id.sql",
property_id
)
.fetch_all(conn)
.await
}
pub async fn fetch_arrangements_with_property_ids(
conn: &PgPool,
property_ids: &Vec<Uuid>,
) -> Result<Vec<PropertyPrice>, sqlx::Error> {
sqlx::query_file_as!(
PropertyPrice,
"sql/property_arrangement/fetch_with_property_ids.sql",
property_ids
)
.fetch_all(conn)
.await
}
pub async fn update_arrangement<'a>(
tx: &mut Transaction<'a, Postgres>,
arrangement: PropertyPrice,
) -> Result<PropertyPrice, sqlx::Error> {
sqlx::query_file_as!(
PropertyPrice,
"sql/property_arrangement/update.sql",
arrangement.property_id,
arrangement.price,
arrangement.currency,
arrangement.arrangement as _,
arrangement.last_updated
)
.fetch_one(tx)
.await
}

View File

@ -1,7 +1,10 @@
use realtor_lp_types::domain::view::View;
use sqlx::{PgPool, Transaction, Postgres};
use sqlx::{PgPool, Postgres, Transaction};
pub async fn insert_view<'a>(transaction: &mut Transaction<'a, Postgres>, view: View) -> Result<View, sqlx::Error> {
pub async fn insert_view<'a>(
transaction: &mut Transaction<'a, Postgres>,
view: View,
) -> Result<View, sqlx::Error> {
sqlx::query_file_as!(
View,
"sql/view/insert.sql",

View File

@ -1,6 +1,9 @@
use std::sync::Arc;
use actix_web::{web::{Path, Data}, get};
use actix_web::{
get,
web::{Data, Path},
};
use actix_web_utils::extensions::typed_response::TypedHttpResponse;
use err::MessageResource;
use realtor_lp_types::{
@ -93,6 +96,5 @@ pub async fn fetch_realtor_properties_paged(
conn: Data<Arc<PgPool>>,
path_vars: Path<(Uuid, i64)>,
) -> TypedHttpResponse<Vec<Property>> {
success!(Default::default())
}
}

View File

@ -2,11 +2,13 @@ use actix_web_utils::extensions::typed_response::TypedHttpResponse;
use realtor_lp_types::dto::payloads::view::ViewForCreationPayload;
use sqlx::PgPool;
use crate::{dao, handle_tx, handle_db_write_op, success};
use crate::{dao, handle_db_write_op, handle_tx, success};
pub async fn new_view(conn: &PgPool, view: ViewForCreationPayload) -> TypedHttpResponse<()> {
let mut transaction = handle_tx!(conn.begin());
handle_db_write_op!(dao::view::insert_view(&mut transaction, view.into()), transaction);
handle_db_write_op!(
dao::view::insert_view(&mut transaction, view.into()),
transaction
);
success!(())
}
}