From e9244efb2ff4902970aed757840c335f21250b76 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 11 Mar 2023 14:28:53 -0400 Subject: [PATCH] Added location search filters --- .env | 2 +- .vscode/launch.json | 16 +++++++++++ sql/property/fetch_paged_with_filters.sql | 25 +++++++++-------- src/dao/property.rs | 33 +++++++++++++++++++---- src/routes/read.rs | 21 ++++++++++----- 5 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.env b/.env index 7c7fa4c..b335040 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ DATABASE_URL="postgres://root:NMWrHZcu9QCu6wlmRUeEw1vXUYq7JhQYSPICTnl5yN9Pu5NIHUOcaj2noV9ejhFgfk5AfKIM2e9x97rbGCwbgTpVa3Fe8nfHgrYLZ2B36sOININQG60T2vIsQX3gkE6U@backend.blancoinfante.com:9102/postgres" -SQLX_OFFLINE=TRUE \ No newline at end of file +SQLX_OFFLINE=FALSE \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..10efcb2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/sql/property/fetch_paged_with_filters.sql b/sql/property/fetch_paged_with_filters.sql index 97f7c6d..fc89344 100644 --- a/sql/property/fetch_paged_with_filters.sql +++ b/sql/property/fetch_paged_with_filters.sql @@ -7,15 +7,18 @@ p.state as "state: _", p.time_created, p.last_updated FROM property p, property_details pd, location l where pd.property_id = p.id AND pd.location_id = l.id -AND (p.time_created <= $1 OR $1 = null) -- Before Filter -AND (p.time_created >= $2 OR $2 = null) -- After Filter -AND (p.title LIKE ('%' || LOWER($3) || '%') OR $3 = null) -- Title filter (like) -AND (p.description LIKE ('%' || LOWER($4) || '%') OR $4 = null) -- Description Filter (like) -AND (pd.meters > $5 OR $5 = null) -- Bigger than or equal to filter -AND (pd.meters < $6 OR $6 = null) -- Smaller than or equal to filter -AND (pd.location_id = $7 OR $7 = null) -- Location Filter -AND (pd.listing_type LIKE $8 || '%' OR $8 = null) -- Listing type filter -AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 >= $9::FLOAT8 OR $9 = null) -- More expensive than filter -AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 <= $10::FLOAT8 OR $10 = null) -- More expensive than filter +AND (p.time_created <= $1 OR $1 IS null) -- Before Filter +AND (p.time_created >= $2 OR $2 IS null) -- After Filter +AND (LOWER(p.title) LIKE '%' || LOWER($3) || '%' OR $3 IS null) -- Title filter (like) +AND (LOWER(p.description) LIKE '%' || LOWER($4) || '%' OR $4 IS null) -- Description Filter (like) +AND (pd.meters > $5 OR $5 IS null) -- Bigger than or equal to filter +AND (pd.meters < $6 OR $6 IS null) -- Smaller than or equal to filter +AND (LOWER(l.country) LIKE '%' || LOWER($7) || '%' OR $7 IS null) -- Location Filter +AND (LOWER(l.province) LIKE '%' || LOWER($8) || '%' OR $8 IS null) -- Location Filter +AND (LOWER(l.city) LIKE '%' || LOWER($9) || '%' OR $9 IS null) -- Location Filter +AND (LOWER(l.district) LIKE '%' || LOWER($10) || '%' OR $10 IS null) -- Location Filter +AND (pd.listing_type LIKE $11 || '%' OR $11 IS null) -- Listing type filter +AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 >= $12::FLOAT8 OR $12 IS null) -- More expensive than filter +AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 <= $13::FLOAT8 OR $13 IS null) -- More expensive than filter ORDER BY p.time_created DESC -LIMIT 25 OFFSET $11 \ No newline at end of file +LIMIT 25 OFFSET $14 \ No newline at end of file diff --git a/src/dao/property.rs b/src/dao/property.rs index 345b4c1..642aad2 100644 --- a/src/dao/property.rs +++ b/src/dao/property.rs @@ -41,7 +41,10 @@ pub async fn fetch_properties_paged_with_filters( let mut description_filter = None; let mut bigger_filter = None; let mut smaller_filter = None; - let mut location_filter = None; + let mut country_filter = None; + let mut province_filter = None; + let mut city_filter = None; + let mut district_filter = None; let mut listing_type_filter = None; let mut more_expensive_than_filter = None; let mut cheaper_than_filter = None; @@ -56,9 +59,19 @@ pub async fn fetch_properties_paged_with_filters( PropertyFilter::Description(description_like) => { description_filter = Some(description_like) } - PropertyFilter::Location(location_id) => location_filter = Some(location_id), + PropertyFilter::Country(country) => country_filter = Some(country), + PropertyFilter::Province(province) => province_filter = Some(province), + PropertyFilter::City(city) => city_filter = Some(city), + PropertyFilter::District(district) => district_filter = Some(district), PropertyFilter::MoreExpensiveThan(listing_type) => { - listing_type_filter = Some(listing_type.to_string().split(" ").collect::()); + let listing_type_string = listing_type.to_string(); + let listing_type_split = listing_type_string.split(" ").collect::>(); + match listing_type_split.first() { + Some(listing_type_str) => { + listing_type_filter = Some(listing_type_str.to_string().clone()) + }, + None => {}, + } more_expensive_than_filter = match listing_type { remax_types::domain::property_details::ListingType::Rent(amount) => { Some(amount) @@ -70,7 +83,14 @@ pub async fn fetch_properties_paged_with_filters( } } PropertyFilter::CheaperThan(listing_type) => { - listing_type_filter = Some(listing_type.to_string().split(" ").collect::()); + let listing_type_string = listing_type.to_string(); + let listing_type_split = listing_type_string.split(" ").collect::>(); + match listing_type_split.first() { + Some(listing_type_str) => { + listing_type_filter = Some(listing_type_str.to_string().clone()) + }, + None => {}, + } cheaper_than_filter = match listing_type { remax_types::domain::property_details::ListingType::Rent(amount) => { Some(amount) @@ -92,7 +112,10 @@ pub async fn fetch_properties_paged_with_filters( description_filter, bigger_filter, smaller_filter, - location_filter, + country_filter, + province_filter, + city_filter, + district_filter, listing_type_filter, more_expensive_than_filter, cheaper_than_filter, diff --git a/src/routes/read.rs b/src/routes/read.rs index 216f32d..d165314 100644 --- a/src/routes/read.rs +++ b/src/routes/read.rs @@ -65,12 +65,21 @@ fn parse_params_into_filters(params: HashMap) -> Result { - let uid = match Uuid::from_str(params.get(key).unwrap()) { - Ok(id) => id, - Err(error) => return Err(MessageResource::new_from_string(error.to_string())), - }; - filters.push(PropertyFilter::Location(uid)); + "country" => { + let country = params.get(key).unwrap().clone(); + filters.push(PropertyFilter::Country(country)); + }, + "province" => { + let province = params.get(key).unwrap().clone(); + filters.push(PropertyFilter::Province(province)); + }, + "city" => { + let city = params.get(key).unwrap().clone(); + filters.push(PropertyFilter::City(city)); + }, + "district" => { + let district = params.get(key).unwrap().clone(); + filters.push(PropertyFilter::District(district)); }, "cheaperthan" => { let cheaper_than = params.get(key).unwrap().clone();