API: Filter posts in the list

This commit is contained in:
Bat
2018-09-29 15:45:27 +01:00
parent f893056d6d
commit 72fd9eb610
4 changed files with 37 additions and 4 deletions
+5 -3
View File
@@ -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<serde_json::Value> {
Json(json!(post))
}
// TODO: handle query params
#[get("/posts")]
fn list(conn: DbConn) -> Json<serde_json::Value> {
let post = <Post as Provider<PgConnection>>::list(&*conn, PostEndpoint::default());
fn list(conn: DbConn, uri: &Origin) -> Json<serde_json::Value> {
let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string");
let post = <Post as Provider<PgConnection>>::list(&*conn, query);
Json(json!(post))
}
+2 -1
View File
@@ -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,