|
|
|
|
@ -21,6 +21,7 @@ struct Pagination<T> {
|
|
|
|
|
pub next: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add a new robot entry
|
|
|
|
|
async fn create(
|
|
|
|
|
pool: PgSharedPool,
|
|
|
|
|
r: app::robots::GetRobot,
|
|
|
|
|
@ -28,10 +29,12 @@ async fn create(
|
|
|
|
|
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> {
|
|
|
|
|
reply_json(app::robots::get_robot(pool, id).await)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// updates robot information
|
|
|
|
|
async fn update(
|
|
|
|
|
pool: PgSharedPool,
|
|
|
|
|
id: i64,
|
|
|
|
|
@ -40,6 +43,7 @@ async fn update(
|
|
|
|
|
reply_json(app::robots::update_robot(pool, id, r).await)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns a pagination of all robots
|
|
|
|
|
async fn get_all(
|
|
|
|
|
pool: PgSharedPool,
|
|
|
|
|
p: PaginationOptions,
|
|
|
|
|
@ -54,6 +58,7 @@ async fn get_all(
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// will delete the requested robot
|
|
|
|
|
async fn delete(pool: PgSharedPool, id: i64) -> Result<impl warp::Reply, warp::Rejection> {
|
|
|
|
|
match app::robots::delete_robot(pool, id).await {
|
|
|
|
|
Ok(_) => Ok(warp::reply::with_status(
|
|
|
|
|
@ -64,45 +69,81 @@ 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,)>,
|
|
|
|
|
pool: PgSharedPool,
|
|
|
|
|
) -> warp::filters::BoxedFilter<(impl warp::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());
|
|
|
|
|
|
|
|
|
|
base.or(prefix
|
|
|
|
|
base.or(
|
|
|
|
|
// filter for creating new robots
|
|
|
|
|
prefix
|
|
|
|
|
.clone()
|
|
|
|
|
// match HTTP POST method
|
|
|
|
|
.and(warp::filters::method::post())
|
|
|
|
|
// attempts to parse the request's body as JSON
|
|
|
|
|
.and(warp::body::json())
|
|
|
|
|
.and_then(create))
|
|
|
|
|
.or(prefix
|
|
|
|
|
// bind the handler
|
|
|
|
|
.and_then(create),
|
|
|
|
|
)
|
|
|
|
|
.or(
|
|
|
|
|
// filter for retrieving a pagination of robots
|
|
|
|
|
prefix
|
|
|
|
|
.clone()
|
|
|
|
|
// match HTTP GET method
|
|
|
|
|
.and(warp::filters::method::get())
|
|
|
|
|
// add pagination query parameters
|
|
|
|
|
.and(warp::filters::query::query())
|
|
|
|
|
.and_then(get_all))
|
|
|
|
|
// bind the handler
|
|
|
|
|
.and_then(get_all),
|
|
|
|
|
)
|
|
|
|
|
.boxed()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build the route structure for the "/robot/{id}" endpoint
|
|
|
|
|
pub fn with_id(
|
|
|
|
|
base: warp::filters::BoxedFilter<(impl warp::Reply + 'static,)>,
|
|
|
|
|
pool: PgSharedPool,
|
|
|
|
|
) -> warp::filters::BoxedFilter<(impl warp::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
|
|
|
|
|
let prefix = warp::path!("robot" / i64)
|
|
|
|
|
.map(move |id| (pool.clone(), id))
|
|
|
|
|
.untuple_one();
|
|
|
|
|
|
|
|
|
|
base.or(prefix
|
|
|
|
|
// append the routes to the base filter
|
|
|
|
|
base.or(
|
|
|
|
|
// filter to get information on a robot
|
|
|
|
|
prefix
|
|
|
|
|
.clone()
|
|
|
|
|
// match HTTP GET method
|
|
|
|
|
.and(warp::filters::method::get())
|
|
|
|
|
.and_then(get))
|
|
|
|
|
.or(prefix
|
|
|
|
|
// bind the handler
|
|
|
|
|
.and_then(get),
|
|
|
|
|
)
|
|
|
|
|
.or(
|
|
|
|
|
// filter for updating individual robots
|
|
|
|
|
prefix
|
|
|
|
|
.clone()
|
|
|
|
|
// metch HTTP POST method
|
|
|
|
|
.and(warp::filters::method::put())
|
|
|
|
|
// attempts to parse the request's body as JSON
|
|
|
|
|
.and(warp::body::json())
|
|
|
|
|
.and_then(update))
|
|
|
|
|
.or(prefix
|
|
|
|
|
// bind the handler
|
|
|
|
|
.and_then(update),
|
|
|
|
|
)
|
|
|
|
|
.or(
|
|
|
|
|
// filter that deletes a robot entry
|
|
|
|
|
prefix
|
|
|
|
|
.clone()
|
|
|
|
|
// match HTTP DELETE method
|
|
|
|
|
.and(warp::filters::method::delete())
|
|
|
|
|
.and_then(delete))
|
|
|
|
|
// bind the handler
|
|
|
|
|
.and_then(delete),
|
|
|
|
|
)
|
|
|
|
|
.boxed()
|
|
|
|
|
}
|
|
|
|
|
|