diff --git a/docs/canisters/deferred-data.md b/docs/canisters/deferred-data.md index 1ebcf66..18f22d3 100644 --- a/docs/canisters/deferred-data.md +++ b/docs/canisters/deferred-data.md @@ -43,7 +43,9 @@ It is also possible to filter contracts using query params: - seller: seller ETH address - buyer: buyer ETH address -- agency: agency principal +- agent: agency principal +- minPrice: minimum price +- maxPrice: maximum price (price is) - contract_property: name of the contract property followed by the value (e.g. `contract:garden` => `garden=true`). ### Get contract by id diff --git a/src/deferred_data/src/http.rs b/src/deferred_data/src/http.rs index adb3d4e..b36f455 100644 --- a/src/deferred_data/src/http.rs +++ b/src/deferred_data/src/http.rs @@ -226,8 +226,13 @@ mod test { #[tokio::test] async fn test_should_filter_contract() { + // total price is 100 * 100 + let min_price = 100 * 50u64; + let max_price = 100 * 150u64; + store_mock_contract_with(1u64, 100, |contract| { // insert all properties + contract.value = 100 * 100; contract.buyers = vec![H160::from_hex_str("0x0b24F78CF0033FAbf1977D9aA61f583fBF7586D9").unwrap()]; contract.sellers = vec![Seller { @@ -333,7 +338,9 @@ mod test { // build url with filters let url = Url::parse( - "http://localhost/contracts?latitude=123&longitude=456&zone=zone&city=city&squareMeters=123&rooms=4&bathrooms=8&floors=10&balconies=2&garden=true&pool=true&garage=true&parking=true&energyClass=A&youtubeUrl=https://www.youtube.com/watch?v=IOuTVyaNSrU", + &format!( + "http://localhost/contracts?latitude=123&longitude=456&zone=zone&city=city&squareMeters=123&rooms=4&bathrooms=8&floors=10&balconies=2&garden=true&pool=true&garage=true&parking=true&energyClass=A&youtubeUrl=https://www.youtube.com/watch?v=IOuTVyaNSrU&minPrice={min_price}&maxPrice={max_price}", + ) ) .expect("Failed to parse URL"); diff --git a/src/deferred_data/src/http/contract_filter.rs b/src/deferred_data/src/http/contract_filter.rs index b8a529c..fa21147 100644 --- a/src/deferred_data/src/http/contract_filter.rs +++ b/src/deferred_data/src/http/contract_filter.rs @@ -7,6 +7,9 @@ const FILTER_SELLER: &str = "seller"; const FILTER_BUYER: &str = "buyer"; const FILTER_AGENT: &str = "agent"; +const FILTER_MIN_PRICE: &str = "minPrice"; +const FILTER_MAX_PRICE: &str = "maxPrice"; + const FILTER_PROPERTY_NAME: &str = "name"; const FILTER_PROPERTY_DESCRIPTION: &str = "description"; const FILTER_PROPERTY_IMAGE: &str = "image"; @@ -43,6 +46,10 @@ enum ContractFilter { Buyer(H160), /// Agent Agent(Principal), + /// Min price + MinPrice(u64), + /// Max price + MaxPrice(u64), } impl ContractFilter { @@ -67,6 +74,8 @@ impl ContractFilter { .as_ref() .map(|agency| agency.owner == *agent) .unwrap_or_default(), + ContractFilter::MinPrice(min_price) => contract.value >= *min_price, + ContractFilter::MaxPrice(max_price) => contract.value <= *max_price, } } } @@ -96,6 +105,16 @@ impl From<&Url> for Filters { filters.push(ContractFilter::Seller(addr)); } } + FILTER_MIN_PRICE => { + if let Ok(min_price) = value.parse() { + filters.push(ContractFilter::MinPrice(min_price)); + } + } + FILTER_MAX_PRICE => { + if let Ok(max_price) = value.parse() { + filters.push(ContractFilter::MaxPrice(max_price)); + } + } FILTER_PROPERTY_NAME | FILTER_PROPERTY_DESCRIPTION | FILTER_PROPERTY_IMAGE diff --git a/src/did/src/deferred/contract.rs b/src/did/src/deferred/contract.rs index ea43fdc..28d7d90 100644 --- a/src/did/src/deferred/contract.rs +++ b/src/did/src/deferred/contract.rs @@ -27,7 +27,7 @@ pub struct Contract { pub buyers: Vec, /// Number of installments pub installments: u64, - /// Token value + /// Contract value value pub value: u64, /// Deposit fiat value (already paid) pub deposit: u64,