Skip to content

Commit

Permalink
Add alias of filter_by
Browse files Browse the repository at this point in the history
  • Loading branch information
kuy committed Sep 28, 2019
1 parent 715e76d commit f731bae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Upcoming]

### Added

- Add `QueryBuilder::and()` for better chaining [[#6](https://github.com/kuy/jsonbox-rs/issues/6)]

## [0.1.1] 2019-09-26

### Changed
Expand Down
6 changes: 5 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ fn main() -> Result<(), Error> {
client.update(&meta2.id, &data)?;
println!("UPDATE: OK");

let filtered = client.read().filter_by("age:<{}", 10).run::<Data>()?;
let filtered = client
.read()
.filter_by("age:<{}", 10)
.and("login:{}", false)
.run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);

client.delete(&meta1.id)?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ mod url;

pub use crate::client::Client;
pub use crate::error::{Error, Result};
pub use crate::query_builder::QueryBuilder;
30 changes: 30 additions & 0 deletions src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Set the field for sorting.
pub fn order_by(&self, field: &'a str) -> QueryBuilder {
QueryBuilder {
client: self.client,
Expand All @@ -40,6 +41,7 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Set reverse order. Use this with `order_by` method.
pub fn desc(&self) -> QueryBuilder {
let field = match &self.sort {
Order::Asc(f) => f,
Expand All @@ -54,6 +56,7 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Limit the number of records of query result.
pub fn limit(&self, limit: u32) -> QueryBuilder {
QueryBuilder {
client: self.client,
Expand All @@ -64,6 +67,7 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Specify the number of records to skip.
pub fn skip(&self, skip: u32) -> QueryBuilder {
QueryBuilder {
client: self.client,
Expand All @@ -74,6 +78,7 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Set filter option, whkch is mapped `q` parameter in REST API.
pub fn filter_by<T: Display>(&self, format: &str, value: T) -> QueryBuilder {
let value = utf8_percent_encode(&format!("{}", value), NON_ALPHANUMERIC).to_string();
let mut q = self.q.clone();
Expand All @@ -87,13 +92,20 @@ impl<'a> QueryBuilder<'a> {
}
}

/// Alias of `filter_by`.
pub fn and<T: Display>(&self, format: &str, value: T) -> QueryBuilder {
self.filter_by(format, value)
}

/// Get a single record by id.
pub fn id<T>(&self, id: &str) -> Result<(T, Meta)>
where
T: DeserializeOwned,
{
self.client.read_by_id(id)
}

/// Get all records with default query parameters.
pub fn all<T>(&self) -> Result<Vec<(T, Meta)>>
where
T: DeserializeOwned,
Expand All @@ -102,13 +114,15 @@ impl<'a> QueryBuilder<'a> {
self.client.read_by_query(&default)
}

/// Run query with configured query parameters.
pub fn run<T>(&self) -> Result<Vec<(T, Meta)>>
where
T: DeserializeOwned,
{
self.client.read_by_query(self)
}

/// Generate query string.
pub fn to_string(&self) -> String {
let mut query = format!(
"sort={}&skip={}&limit={}",
Expand Down Expand Up @@ -143,6 +157,22 @@ impl<'a> QueryBuilder<'a> {
mod tests {
use super::*;

#[test]
fn test_to_string() {
let c = Client::new("xxx");
assert_eq!(
QueryBuilder::new(&c)
.order_by("count")
.desc()
.limit(42)
.skip(8)
.filter_by("count:>{}", 20)
.and("count:<{}", 40)
.to_string(),
"sort=-count&skip=8&limit=42&q=count:>20,count:<40"
);
}

#[test]
fn test_sort_string() {
let c = Client::new("xxx");
Expand Down

0 comments on commit f731bae

Please sign in to comment.