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::models::{InteriorRefList, ListParams, Owner, Shop};
use super::Environment; 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 { 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(warp::get())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::get_shop) .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( pub fn create_shop(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops") warp::post()
.and(warp::post())
.and(json_body::<Shop>()) .and(json_body::<Shop>())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::create_shop) .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( pub fn list_shops(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("shops") warp::get()
.and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::list_shops) .and_then(handlers::list_shops)
} }
pub fn get_owner(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { 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(warp::get())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::get_owner) .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( pub fn create_owner(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners") warp::post()
.and(warp::post())
.and(json_body::<Owner>()) .and(json_body::<Owner>())
.and(warp::addr::remote()) .and(warp::addr::remote())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::create_owner) .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( pub fn list_owners(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("owners") warp::get()
.and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::list_owners) .and_then(handlers::list_owners)
@ -64,7 +107,7 @@ pub fn list_owners(
pub fn get_interior_ref_list( pub fn get_interior_ref_list(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists" / i32) warp::path::param()
.and(warp::get()) .and(warp::get())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::get_interior_ref_list) .and_then(handlers::get_interior_ref_list)
@ -73,33 +116,30 @@ pub fn get_interior_ref_list(
pub fn create_interior_ref_list( pub fn create_interior_ref_list(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists") warp::post()
.and(warp::post())
.and(json_body::<InteriorRefList>()) .and(json_body::<InteriorRefList>())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::create_interior_ref_list) .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( pub fn list_interior_ref_lists(
env: Environment, env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("interior_ref_lists") warp::get()
.and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env)) .and(with_env(env))
.and_then(handlers::list_interior_ref_lists) .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 { fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
warp::any().map(move || env.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) 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> { 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 owner = Owner::get(&env.db, id).await.map_err(reject_anyhow)?;
let reply = json(&owner); let reply = json(&owner);
@ -75,6 +80,11 @@ pub async fn create_owner(
Ok(reply) 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> { pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
let interior_ref_list = InteriorRefList::get(&env.db, id) let interior_ref_list = InteriorRefList::get(&env.db, id)
.await .await
@ -113,12 +123,9 @@ pub async fn create_interior_ref_list(
Ok(reply) Ok(reply)
} }
pub async fn bulk_create_interior_ref_lists( pub async fn delete_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
interior_ref_lists: Vec<InteriorRefList>, InteriorRefList::delete(&env.db, id)
env: Environment,
) -> Result<impl Reply, Rejection> {
InteriorRefList::bulk_save(&env.db, interior_ref_lists)
.await .await
.map_err(reject_anyhow)?; .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 env = Environment::new(api_url).await?;
let base = warp::path("api").and(warp::path("v1")); 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 let routes = base
.and( .and(
create_shop filters::shops(env.clone())
.or(get_shop) .or(filters::owners(env.clone()))
.or(list_shops) .or(filters::interior_ref_lists(env.clone())),
.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),
) )
.recover(problem::unpack_problem) .recover(problem::unpack_problem)
.with(warp::compression::gzip()) .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> { async fn save(self, db: &PgPool) -> Result<Self> {
// TODO: // TODO:
// * Decide if I'll need to make the same changes to merchandise and transactions // * Decide if I'll need to make the same changes to merchandise and transactions
@ -74,6 +74,15 @@ impl Model for InteriorRefList {
.await?) .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))] #[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> { async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() { 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 get(db: &PgPool, id: i32) -> Result<Self>;
async fn save(self, db: &PgPool) -> 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 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?) .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))] #[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> { async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() { let result = if let Some(order_by) = list_params.get_order_by() {

View File

@ -62,6 +62,13 @@ impl Model for Shop {
.await?) .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))] #[instrument(level = "debug", skip(db))]
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> { async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() { let result = if let Some(order_by) = list_params.get_order_by() {