From b30072bfe023a66a59f73595fe2798b3a7a38b3c Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 9 May 2023 11:40:29 -0400 Subject: [PATCH] Filter impls --- src/domain/price.rs | 5 +++ src/dto/filter.rs | 77 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/domain/price.rs b/src/domain/price.rs index ebc37d8..c213805 100644 --- a/src/domain/price.rs +++ b/src/domain/price.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -13,4 +14,8 @@ pub struct PropertyPrice { pub price: f64, pub currency: String, pub arrangement: ArrangementType, + #[serde(rename = "timeCreated")] + pub time_created: DateTime, + #[serde(rename = "lastUpdated")] + pub last_updated: DateTime, } \ No newline at end of file diff --git a/src/dto/filter.rs b/src/dto/filter.rs index 1ebaee7..8b44166 100644 --- a/src/dto/filter.rs +++ b/src/dto/filter.rs @@ -1,17 +1,86 @@ +use std::str::FromStr; + use serde::{Serialize, Deserialize}; +use crate::domain::{arrangement::ArrangementType, property_type::PropertyType}; + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, PartialOrd)] pub enum Filter { + PropertyType(PropertyType), + Country(String), City(String), District(String), - PriceGreaterThan(f64), - PriceLessThan(f64), + Rooms(i16), + Bathrooms(f32), + MinArea(f32), + ParkingSpots(i16), - Rooms(i32), - Bathrooms(i32), + Arrangement(ArrangementType), + PriceLessThan(f64), + PriceGreaterThan(f64), } +impl Filter { + pub fn from_query_param(key: String, val: String) -> Option { + match key.as_str() { + "property_type" => { + match PropertyType::from_str(&val) { + Ok(property_type) => Some(Self::PropertyType(property_type)), + Err(_) => None, + } + }, + "country" => Some(Self::Country(val)), + "city" => Some(Self::City(val)), + "district" => Some(Self::District(val)), + "rooms" => match val.parse::() { + Ok(rooms) => Some(Self::Rooms(rooms)), + Err(_) => None, + }, + "bathrooms" => match val.parse::() { + Ok(bathrooms) => Some(Self::Bathrooms(bathrooms)), + Err(_) => None, + }, + "min_area" => match val.parse::() { + Ok(min_area) => Some(Self::MinArea(min_area)), + Err(_) => None, + }, + "parking_spots" => match val.parse::() { + Ok(parking_spots) => Some(Self::ParkingSpots(parking_spots)), + Err(_) => None, + }, + "arrangement" => match ArrangementType::from_str(&val) { + Ok(arrangement_type) => Some(Self::Arrangement(arrangement_type)), + Err(_) => None, + }, + "price_less_than" => match val.parse::() { + Ok(price) => Some(Self::PriceLessThan(price)), + Err(_) => None, + }, + "price_greater_than" => match val.parse::() { + Ok(price) => Some(Self::PriceGreaterThan(price)), + Err(_) => None, + }, + _ => { None } + } + } + + pub fn to_query_param(self) -> (String, String) { + match self { + Filter::PropertyType(property_type) => (String::from("property_type"), property_type.to_string()), + Filter::Country(country) => (String::from("country"), country), + Filter::City(city) => (String::from("city"), city), + Filter::District(district) => (String::from("district"), district), + Filter::Rooms(rooms) => (String::from("rooms"), rooms.to_string()), + Filter::Bathrooms(bathrooms) => (String::from("bathrooms"), bathrooms.to_string()), + Filter::MinArea(min_area) => (String::from("min_area"), min_area.to_string()), + Filter::ParkingSpots(parking_spots) => (String::from("parking_spots"), parking_spots.to_string()), + Filter::Arrangement(arrangement) => (String::from("arrangement"), arrangement.to_string()), + Filter::PriceLessThan(price_less_than) => (String::from("price_less_than"), price_less_than.to_string()), + Filter::PriceGreaterThan(price_greater_than) => (String::from("price_greater_than"), price_greater_than.to_string()), + } + } +} \ No newline at end of file