Skip to content

Commit

Permalink
feat: minPrice and maxPrice filters
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Dec 10, 2024
1 parent 6874aa5 commit d9b4358
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/canisters/deferred-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/deferred_data/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");

Expand Down
19 changes: 19 additions & 0 deletions src/deferred_data/src/http/contract_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -43,6 +46,10 @@ enum ContractFilter {
Buyer(H160),
/// Agent
Agent(Principal),
/// Min price
MinPrice(u64),
/// Max price
MaxPrice(u64),
}

impl ContractFilter {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/did/src/deferred/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Contract {
pub buyers: Vec<H160>,
/// Number of installments
pub installments: u64,
/// Token value
/// Contract value value
pub value: u64,
/// Deposit fiat value (already paid)
pub deposit: u64,
Expand Down

0 comments on commit d9b4358

Please sign in to comment.