Add delete endpoints and clean up filters

This commit is contained in:
Tyler Hallada 2020-07-29 00:02:48 -04:00
parent 17cd3f12d2
commit 79b45551fd
7 changed files with 106 additions and 55 deletions

View File

@ -6,8 +6,37 @@ use super::handlers;
use super::models::{InteriorRefList, ListParams, Owner, Shop};
use super::Environment;
pub fn shops(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("shops").and(
get_shop(env.clone())
.or(delete_shop(env.clone()))
.or(create_shop(env.clone()))
.or(list_shops(env)),
)
}
pub fn owners(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("owners").and(
get_owner(env.clone())
.or(delete_owner(env.clone()))
.or(create_owner(env.clone()))
.or(list_owners(env)),
)
}
pub fn interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("interior_ref_lists").and(
get_interior_ref_list(env.clone())
.or(delete_interior_ref_list(env.clone()))
.or(create_interior_ref_list(env.clone()))
.or(list_interior_ref_lists(env)),
)
}
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops" / i32)
warp::path::param()
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_shop)
@ -16,25 +45,32 @@ pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = R
pub fn create_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops")
.and(warp::post())
warp::post()
.and(json_body::<Shop>())
.and(with_env(env))
.and_then(handlers::create_shop)
}
pub fn delete_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::delete())
.and(with_env(env))
.and_then(handlers::delete_shop)
}
pub fn list_shops(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops")
.and(warp::get())
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)
warp::path::param()
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_owner)
@ -43,19 +79,26 @@ pub fn get_owner(env: Environment) -> impl Filter<Extract = impl Reply, Error =
pub fn create_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners")
.and(warp::post())
warp::post()
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(with_env(env))
.and_then(handlers::create_owner)
}
pub fn delete_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::delete())
.and(with_env(env))
.and_then(handlers::delete_owner)
}
pub fn list_owners(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners")
.and(warp::get())
warp::get()
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_owners)
@ -64,7 +107,7 @@ pub fn list_owners(
pub fn get_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists" / i32)
warp::path::param()
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_interior_ref_list)
@ -73,33 +116,30 @@ pub fn get_interior_ref_list(
pub fn create_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists")
.and(warp::post())
warp::post()
.and(json_body::<InteriorRefList>())
.and(with_env(env))
.and_then(handlers::create_interior_ref_list)
}
pub fn delete_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::delete())
.and(with_env(env))
.and_then(handlers::delete_interior_ref_list)
}
pub fn list_interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists")
.and(warp::get())
warp::get()
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_interior_ref_lists)
}
pub fn bulk_create_interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists" / "bulk")
.and(warp::post())
.and(json_body::<Vec<InteriorRefList>>())
.and(with_env(env))
.and_then(handlers::bulk_create_interior_ref_lists)
}
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
warp::any().map(move || env.clone())
}

View File

@ -36,6 +36,11 @@ pub async fn create_shop(shop: Shop, env: Environment) -> Result<impl Reply, Rej
Ok(reply)
}
pub async fn delete_shop(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
Shop::delete(&env.db, id).await.map_err(reject_anyhow)?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_owner(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
let owner = Owner::get(&env.db, id).await.map_err(reject_anyhow)?;
let reply = json(&owner);
@ -75,6 +80,11 @@ pub async fn create_owner(
Ok(reply)
}
pub async fn delete_owner(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
Owner::delete(&env.db, id).await.map_err(reject_anyhow)?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
let interior_ref_list = InteriorRefList::get(&env.db, id)
.await
@ -113,12 +123,9 @@ pub async fn create_interior_ref_list(
Ok(reply)
}
pub async fn bulk_create_interior_ref_lists(
interior_ref_lists: Vec<InteriorRefList>,
env: Environment,
) -> Result<impl Reply, Rejection> {
InteriorRefList::bulk_save(&env.db, interior_ref_lists)
pub async fn delete_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
InteriorRefList::delete(&env.db, id)
.await
.map_err(reject_anyhow)?;
Ok(StatusCode::CREATED)
Ok(StatusCode::NO_CONTENT)
}

View File

@ -72,28 +72,11 @@ async fn main() -> Result<()> {
let env = Environment::new(api_url).await?;
let base = warp::path("api").and(warp::path("v1"));
let get_shop = filters::get_shop(env.clone());
let create_shop = filters::create_shop(env.clone());
let list_shops = filters::list_shops(env.clone());
let get_owner = filters::get_owner(env.clone());
let create_owner = filters::create_owner(env.clone());
let list_owners = filters::list_owners(env.clone());
let get_interior_ref_list = filters::get_interior_ref_list(env.clone());
let create_interior_ref_list = filters::create_interior_ref_list(env.clone());
let list_interior_ref_lists = filters::list_interior_ref_lists(env.clone());
let bulk_create_interior_ref_lists = filters::bulk_create_interior_ref_lists(env.clone());
let routes = base
.and(
create_shop
.or(get_shop)
.or(list_shops)
.or(create_owner)
.or(get_owner)
.or(list_owners)
.or(create_interior_ref_list)
.or(get_interior_ref_list)
.or(list_interior_ref_lists)
.or(bulk_create_interior_ref_lists),
filters::shops(env.clone())
.or(filters::owners(env.clone()))
.or(filters::interior_ref_lists(env.clone())),
)
.recover(problem::unpack_problem)
.with(warp::compression::gzip())

View File

@ -55,7 +55,7 @@ impl Model for InteriorRefList {
)
}
#[instrument(level = "debug", skip(db))]
#[instrument(level = "debug", skip(self, db))]
async fn save(self, db: &PgPool) -> Result<Self> {
// TODO:
// * Decide if I'll need to make the same changes to merchandise and transactions
@ -74,6 +74,15 @@ impl Model for InteriorRefList {
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn delete(db: &PgPool, id: i32) -> Result<u64> {
Ok(
sqlx::query!("DELETE FROM interior_ref_lists WHERE id = $1", id)
.execute(db)
.await?,
)
}
#[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() {

View File

@ -24,8 +24,6 @@ where
}
async fn get(db: &PgPool, id: i32) -> Result<Self>;
async fn save(self, db: &PgPool) -> Result<Self>;
async fn delete(db: &PgPool, id: i32) -> Result<u64>;
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>>;
async fn bulk_save(_db: &PgPool, _models: Vec<Self>) -> Result<()> {
unimplemented!()
}
}

View File

@ -57,6 +57,13 @@ impl Model for Owner {
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn delete(db: &PgPool, id: i32) -> Result<u64> {
Ok(sqlx::query!("DELETE FROM owners WHERE id = $1", id)
.execute(db)
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() {

View File

@ -62,6 +62,13 @@ impl Model for Shop {
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn delete(db: &PgPool, id: i32) -> Result<u64> {
Ok(sqlx::query!("DELETE FROM shops WHERE id = $1", id)
.execute(db)
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() {