Filter impls

This commit is contained in:
Franklin 2023-05-09 11:40:29 -04:00
parent 8de10c4e3b
commit b30072bfe0
2 changed files with 78 additions and 4 deletions

View File

@ -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<Utc>,
#[serde(rename = "lastUpdated")]
pub last_updated: DateTime<Utc>,
}

View File

@ -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<Self> {
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::<i16>() {
Ok(rooms) => Some(Self::Rooms(rooms)),
Err(_) => None,
},
"bathrooms" => match val.parse::<f32>() {
Ok(bathrooms) => Some(Self::Bathrooms(bathrooms)),
Err(_) => None,
},
"min_area" => match val.parse::<f32>() {
Ok(min_area) => Some(Self::MinArea(min_area)),
Err(_) => None,
},
"parking_spots" => match val.parse::<i16>() {
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::<f64>() {
Ok(price) => Some(Self::PriceLessThan(price)),
Err(_) => None,
},
"price_greater_than" => match val.parse::<f64>() {
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()),
}
}
}