Modularize, Model trait, list_owners

This commit is contained in:
2020-07-18 17:46:33 -04:00
parent 6b1f31f246
commit 9ec7fc1518
10 changed files with 462 additions and 389 deletions

73
src/filters/mod.rs Normal file
View File

@@ -0,0 +1,73 @@
use serde::de::DeserializeOwned;
use std::convert::Infallible;
use warp::{Filter, Rejection, Reply};
use super::handlers;
use super::models::{ListParams, Owner, Shop};
use super::Environment;
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops" / i32)
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_shop)
}
pub fn create_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops")
.and(warp::post())
.and(json_body::<Shop>())
.and(with_env(env))
.and_then(handlers::create_shop)
}
pub fn list_shops(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops")
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_shops)
}
pub fn get_owner(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners" / i32)
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_owner)
}
pub fn create_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners")
.and(warp::post())
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(with_env(env))
.and_then(handlers::create_owner)
}
pub fn list_owners(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners")
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_owners)
}
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
warp::any().map(move || env.clone())
}
fn json_body<T>() -> impl Filter<Extract = (T,), Error = warp::Rejection> + Clone
where
T: Send + DeserializeOwned,
{
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}