Getting ready for v1

This commit is contained in:
Franklin 2023-03-11 14:31:12 -04:00
parent 9bef82126a
commit 77499cd98d
4 changed files with 41 additions and 8 deletions

21
src/domain/error.rs Normal file
View File

@ -0,0 +1,21 @@
use std::fmt::Display;
use serde::Serialize;
#[derive(Debug, Clone, Copy, Serialize, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum InnerError {
#[default]
ParsingFromDb
}
impl Display for InnerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ParsingFromDb => write!(f, "Couldn't parse string to type from DB.")
}
}
}
impl std::error::Error for InnerError {
}

View File

@ -1,6 +1,5 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use crate::domain::{property_details::ListingType};
@ -15,7 +14,10 @@ pub enum PropertyFilter {
Title(String),
Description(String),
Location(Uuid),
Country(String),
Province(String),
City(String),
District(String),
// By cost
CheaperThan(ListingType),

View File

@ -3,4 +3,5 @@ pub mod property;
pub mod location;
pub mod contact_info;
pub mod property_details;
pub mod filters;
pub mod filters;
pub mod error;

View File

@ -4,6 +4,8 @@ use serde::{Serialize, Deserialize};
use sqlx::{Postgres, postgres::{PgArgumentBuffer, PgValueRef, PgTypeInfo}, encode::IsNull, error::BoxDynError};
use uuid::Uuid;
use super::error::InnerError;
use std::str::FromStr;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct PropertyDetails {
@ -45,7 +47,6 @@ pub struct Photos {
pub photos: Vec<String>
}
// Database implementations
impl sqlx::Encode<'_, Postgres> for Photos {
@ -78,7 +79,7 @@ impl sqlx::Type<Postgres> for Photos {
impl sqlx::Encode<'_, Postgres> for ListingType {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
let encoded_value = serde_json::to_string(self).unwrap();
let encoded_value = self.to_string();
<&str as sqlx::Encode<Postgres>>::encode(encoded_value.as_str(), buf)
}
}
@ -86,9 +87,17 @@ impl sqlx::Encode<'_, Postgres> for ListingType {
impl sqlx::Decode<'_, Postgres> for ListingType {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
let column = value.as_str()?;
match serde_json::from_str(column) {
Ok(val) => Ok(val),
Err(error) => Err(Box::new(error)),
let col_split = column.split(" ").collect::<Vec<&str>>();
match *col_split.first().ok_or(Box::new(InnerError::ParsingFromDb))? {
"rent" => Ok(Self::Rent(f64::from_str(*col_split.get(1).ok_or(Box::new(InnerError::ParsingFromDb))?)?)),
"sale" => Ok(Self::Sale(f64::from_str(*col_split.get(1).ok_or(Box::new(InnerError::ParsingFromDb))?)?)),
"both" => Ok(Self::Both(
f64::from_str(*col_split.get(1).ok_or(Box::new(InnerError::ParsingFromDb))?)?,
f64::from_str(*col_split.get(2).ok_or(Box::new(InnerError::ParsingFromDb))?)?,
)),
_ => {
Err(Box::new(InnerError::ParsingFromDb))
}
}
}
}