From 72fd9eb61026d657f3473929a601d41da167b21c Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 29 Sep 2018 15:45:27 +0100 Subject: [PATCH] API: Filter posts in the list --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/api/posts.rs | 8 +++++--- src/main.rs | 3 ++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea75c73c..89073a4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -612,6 +612,11 @@ dependencies = [ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fnv" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "foreign-types" version = "0.3.2" @@ -939,6 +944,11 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "itoa" version = "0.4.2" @@ -1481,6 +1491,7 @@ dependencies = [ "serde 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_qs 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.11.7 (registry+https://github.com/rust-lang/crates.io-index)", "validator 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "validator_derive 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2014,6 +2025,21 @@ dependencies = [ "serde 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde_qs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "data-encoding 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_urlencoded" version = "0.5.3" @@ -2811,6 +2837,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7efb22686e4a466b1ec1a15c2898f91fa9cb340452496dca654032de20ff95b9" "checksum failure_derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "946d0e98a50d9831f5d589038d2ca7f8f455b1c21028c0db0e84116a12696426" "checksum filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "da4b9849e77b13195302c174324b5ba73eec9b236b24c221a61000daefb95c5f" +"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fsevent 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "c4bbbf71584aeed076100b5665ac14e3d85eeb31fdbb45fbd41ef9a682b5ec05" @@ -2846,6 +2873,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d8e17268922834707e1c29e8badbf9c712c9c43378e1b6a3388946baff10be2" "checksum isatty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc" +"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum itoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5adb58558dcd1d786b5f0bd15f3226ee23486e24b7b58304b60f64dc68e62606" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" @@ -2955,6 +2983,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)" = "c6e67977d7523ce4d9284ed58918af99392de8edb6192c44afefcf634654ab7f" "checksum serde_derive 1.0.77 (registry+https://github.com/rust-lang/crates.io-index)" = "5569c52faae3e21b9abae2cc5cfbb56ed008bfcac480ad62bc241b828f0b0aee" "checksum serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "59790990c5115d16027f00913e2e66de23a51f70422e549d2ad68c8c5f268f1c" +"checksum serde_qs 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e5873a8c1e2dc41f1d9305b7510f70768376dc974f81c0b9ce6b4d5b48be0203" "checksum serde_urlencoded 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "aaed41d9fb1e2f587201b863356590c90c1157495d811430a0c0325fe8169650" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" diff --git a/Cargo.toml b/Cargo.toml index 630e134f..ffc3b203 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ rpassword = "2.0" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" +serde_qs = "0.4" tera = "0.11" validator = "0.7" validator_derive = "0.7" diff --git a/src/api/posts.rs b/src/api/posts.rs index c193b09a..bfd96dfd 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -1,7 +1,9 @@ use canapi::Provider; use diesel::PgConnection; +use rocket::http::uri::Origin; use rocket_contrib::Json; use serde_json; +use serde_qs; use plume_api::posts::PostEndpoint; use plume_models::db_conn::DbConn; @@ -13,9 +15,9 @@ fn get(id: i32, conn: DbConn) -> Json { Json(json!(post)) } -// TODO: handle query params #[get("/posts")] -fn list(conn: DbConn) -> Json { - let post = >::list(&*conn, PostEndpoint::default()); +fn list(conn: DbConn, uri: &Origin) -> Json { + let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string"); + let post = >::list(&*conn, query); Json(json!(post)) } diff --git a/src/main.rs b/src/main.rs index fe26e038..d58134f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ extern crate serde; extern crate serde_derive; #[macro_use] extern crate serde_json; +extern crate serde_qs; extern crate validator; #[macro_use] extern crate validator_derive; @@ -147,7 +148,7 @@ fn main() { ]) .mount("/api/v1", routes![ api::posts::get, - api::posts::list + api::posts::list, ]) .catch(catchers![ routes::errors::not_found,