Improve imports path of http handlers

develop
cahe 5 years ago
parent 3cb203e4c3
commit 55c62eb936

@ -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<T> {
}
// add a new robot entry
async fn create(
pool: PgSharedPool,
r: app::robots::GetRobot,
) -> Result<impl warp::Reply, warp::Rejection> {
async fn create(pool: PgSharedPool, r: app::robots::GetRobot) -> Result<impl Reply, Rejection> {
reply_json(app::robots::add_robot(pool, r).await)
}
// retrieve information of a single robot
async fn get(pool: PgSharedPool, id: i64) -> Result<impl warp::Reply, warp::Rejection> {
async fn get(pool: PgSharedPool, id: i64) -> Result<impl Reply, Rejection> {
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<impl warp::Reply, warp::Rejection> {
) -> Result<impl Reply, Rejection> {
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<impl warp::Reply, warp::Rejection> {
async fn get_all(pool: PgSharedPool, p: PaginationOptions) -> Result<impl Reply, Rejection> {
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<impl warp::Reply, warp::Rejection> {
async fn delete(pool: PgSharedPool, id: i64) -> Result<impl Reply, Rejection> {
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<impl warp::Reply, warp::R
// builds the route structure for "/robots" endpoint
pub fn all(
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,)> {
// 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),
)

Loading…
Cancel
Save