From 55c62eb9360cc937346680d6746f8c7dd2f2fbf7 Mon Sep 17 00:00:00 2001 From: cahe Date: Mon, 21 Sep 2020 21:51:23 -0300 Subject: [PATCH] Improve imports path of http handlers --- src/http/robots.rs | 51 +++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/http/robots.rs b/src/http/robots.rs index 883f63f..dd15acb 100644 --- a/src/http/robots.rs +++ b/src/http/robots.rs @@ -1,12 +1,13 @@ -use warp::Filter; - use crate::simple::database::PgSharedPool; use crate::simple::http::reply_json; -use serde::{Deserialize, Serialize}; +use warp::{Filter, Rejection, Reply}; +use warp::filters::{body, method, query as params}; use warp::http::StatusCode; +use serde::{Deserialize, Serialize}; + use crate::app; #[derive(Deserialize)] @@ -22,15 +23,12 @@ struct Pagination { } // add a new robot entry -async fn create( - pool: PgSharedPool, - r: app::robots::GetRobot, -) -> Result { +async fn create(pool: PgSharedPool, r: app::robots::GetRobot) -> Result { reply_json(app::robots::add_robot(pool, r).await) } // retrieve information of a single robot -async fn get(pool: PgSharedPool, id: i64) -> Result { +async fn get(pool: PgSharedPool, id: i64) -> Result { reply_json(app::robots::get_robot(pool, id).await) } @@ -39,15 +37,12 @@ async fn update( pool: PgSharedPool, id: i64, r: app::robots::GetRobot, -) -> Result { +) -> Result { reply_json(app::robots::update_robot(pool, id, r).await) } // returns a pagination of all robots -async fn get_all( - pool: PgSharedPool, - p: PaginationOptions, -) -> Result { +async fn get_all(pool: PgSharedPool, p: PaginationOptions) -> Result { reply_json( app::robots::get_all(pool, p.offset, p.limit) .await @@ -59,7 +54,7 @@ async fn get_all( } // will delete the requested robot -async fn delete(pool: PgSharedPool, id: i64) -> Result { +async fn delete(pool: PgSharedPool, id: i64) -> Result { match app::robots::delete_robot(pool, id).await { Ok(_) => Ok(warp::reply::with_status( warp::reply(), @@ -71,9 +66,9 @@ async fn delete(pool: PgSharedPool, id: i64) -> Result, + base: warp::filters::BoxedFilter<(impl Reply + 'static,)>, pool: PgSharedPool, -) -> warp::filters::BoxedFilter<(impl warp::Reply,)> { +) -> warp::filters::BoxedFilter<(impl Reply,)> { // create a prefix that injects a database connection for every request // every filter is added after the prefix so it receives a database connection let prefix = warp::path!("robots").map(move || pool.clone()); @@ -83,9 +78,9 @@ pub fn all( prefix .clone() // match HTTP POST method - .and(warp::filters::method::post()) + .and(method::post()) // attempts to parse the request's body as JSON - .and(warp::body::json()) + .and(body::json()) // bind the handler .and_then(create), ) @@ -94,20 +89,20 @@ pub fn all( prefix .clone() // match HTTP GET method - .and(warp::filters::method::get()) + .and(method::get()) // add pagination query parameters - .and(warp::filters::query::query()) + .and(params::query()) // bind the handler .and_then(get_all), ) .boxed() } -// build the route structure for the "/robot/{id}" endpoint +// builds the route structure for the "/robot/{id}" endpoint pub fn with_id( - base: warp::filters::BoxedFilter<(impl warp::Reply + 'static,)>, + base: warp::filters::BoxedFilter<(impl Reply + 'static,)>, pool: PgSharedPool, -) -> warp::filters::BoxedFilter<(impl warp::Reply,)> { +) -> warp::filters::BoxedFilter<(impl Reply,)> { // we create a prefix containing the id of a robot and a shared database connection // every filter is added after the prefix // so it receives a robot's id and a database connection @@ -121,7 +116,7 @@ pub fn with_id( prefix .clone() // match HTTP GET method - .and(warp::filters::method::get()) + .and(method::get()) // bind the handler .and_then(get), ) @@ -129,10 +124,10 @@ pub fn with_id( // filter for updating individual robots prefix .clone() - // metch HTTP POST method - .and(warp::filters::method::put()) + // match HTTP POST method + .and(method::put()) // attempts to parse the request's body as JSON - .and(warp::body::json()) + .and(body::json()) // bind the handler .and_then(update), ) @@ -141,7 +136,7 @@ pub fn with_id( prefix .clone() // match HTTP DELETE method - .and(warp::filters::method::delete()) + .and(method::delete()) // bind the handler .and_then(delete), )