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