Update all endpoints to accept and send bincode

This commit is contained in:
Tyler Hallada 2020-11-08 17:58:11 -05:00
parent 6ac4b03a0a
commit e0cc81c97e
5 changed files with 476 additions and 229 deletions

View File

@ -289,11 +289,17 @@ pub async fn delete(
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
tokio::spawn(async move { tokio::spawn(async move {
CACHES.interior_ref_list.delete_response(id).await; CACHES.interior_ref_list.delete_response(id).await;
CACHES.list_interior_ref_lists.clear().await; CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES CACHES
.interior_ref_list_by_shop_id .interior_ref_list_by_shop_id
.delete_response(interior_ref_list.shop_id) .delete_response(interior_ref_list.shop_id)
.await; .await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
}); });
Ok(StatusCode::NO_CONTENT) Ok(StatusCode::NO_CONTENT)
} }

View File

@ -1,46 +1,79 @@
use anyhow::Result; use anyhow::Result;
use http::StatusCode; use http::StatusCode;
use ipnetwork::IpNetwork; use ipnetwork::IpNetwork;
use mime::Mime;
use std::net::SocketAddr; use std::net::SocketAddr;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::caches::CACHES; use crate::caches::{Cache, CachedResponse, CACHES};
use crate::models::{ListParams, Owner}; use crate::models::{ListParams, Owner};
use crate::problem::{reject_anyhow, unauthorized_no_api_key}; use crate::problem::{reject_anyhow, unauthorized_no_api_key};
use crate::Environment; use crate::Environment;
use super::{authenticate, check_etag, DataReply, ETagReply, Json}; use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> { pub async fn get(
let response = CACHES id: i32,
.owner etag: Option<String>,
.get_response(id, || async { accept: Option<AcceptHeader>,
let owner = Owner::get(&env.db, id).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&owner)?; ) -> Result<impl Reply, Rejection> {
let reply = with_status(reply, StatusCode::OK); async fn get<T: DataReply>(
Ok(reply) id: i32,
}) etag: Option<String>,
.await?; env: Environment,
Ok(check_etag(etag, response)) cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(id, || async {
let owner = Owner::get(&env.db, id).await?;
let reply = T::from_serializable(&owner)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.owner_bin).await
}
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.owner).await,
}
} }
pub async fn list( pub async fn list(
list_params: ListParams, list_params: ListParams,
etag: Option<String>, etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let response = CACHES async fn get<T: DataReply>(
.list_owners list_params: ListParams,
.get_response(list_params.clone(), || async { etag: Option<String>,
let owners = Owner::list(&env.db, &list_params).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&owners)?; cache: &'static Cache<ListParams, CachedResponse>,
let reply = with_status(reply, StatusCode::OK); ) -> Result<Box<dyn Reply>, Rejection> {
Ok(reply) let response = cache
}) .get_response(list_params.clone(), || async {
.await?; let owners = Owner::list(&env.db, &list_params).await?;
Ok(check_etag(etag, response)) let reply = T::from_serializable(&owners)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_owners_bin).await
}
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_owners).await,
}
} }
pub async fn create( pub async fn create(
@ -48,9 +81,16 @@ pub async fn create(
remote_addr: Option<SocketAddr>, remote_addr: Option<SocketAddr>,
api_key: Option<Uuid>, api_key: Option<Uuid>,
real_ip: Option<IpNetwork>, real_ip: Option<IpNetwork>,
content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
if let Some(api_key) = api_key { async fn create<'a, T: DataReply + 'a>(
owner: Owner,
remote_addr: Option<SocketAddr>,
api_key: Uuid,
real_ip: Option<IpNetwork>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_with_ip_and_key = match remote_addr { let owner_with_ip_and_key = match remote_addr {
Some(addr) => Owner { Some(addr) => Owner {
api_key: Some(api_key), api_key: Some(api_key),
@ -68,13 +108,23 @@ pub async fn create(
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?; let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_owner).map_err(reject_anyhow)?; let reply = T::from_serializable(&saved_owner).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move { tokio::spawn(async move {
CACHES.list_owners.clear().await; CACHES.list_owners.clear().await;
CACHES.list_owners_bin.clear().await;
}); });
Ok(reply) Ok(Box::new(reply))
}
if let Some(api_key) = api_key {
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
create::<ETagReply<Bincode>>(owner, remote_addr, api_key, real_ip, env).await
}
_ => create::<ETagReply<Json>>(owner, remote_addr, api_key, real_ip, env).await,
}
} else { } else {
Err(reject_anyhow(unauthorized_no_api_key())) Err(reject_anyhow(unauthorized_no_api_key()))
} }
@ -84,26 +134,43 @@ pub async fn update(
id: i32, id: i32,
owner: Owner, owner: Owner,
api_key: Option<Uuid>, api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?; async fn update<'a, T: DataReply + 'a>(
let owner_with_id = Owner { id: i32,
id: Some(id), owner: Owner,
..owner api_key: Option<Uuid>,
}; env: Environment,
let updated_owner = owner_with_id ) -> Result<Box<dyn Reply + 'a>, Rejection> {
.update(&env.db, owner_id, id) let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
.await let owner_with_id = Owner {
.map_err(reject_anyhow)?; id: Some(id),
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?; ..owner
let reply = ETagReply::<Json>::from_serializable(&updated_owner).map_err(reject_anyhow)?; };
let reply = with_header(reply, "Location", url.as_str()); let updated_owner = owner_with_id
let reply = with_status(reply, StatusCode::CREATED); .update(&env.db, owner_id, id)
tokio::spawn(async move { .await
CACHES.owner.delete_response(id).await; .map_err(reject_anyhow)?;
CACHES.list_owners.clear().await; let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
}); let reply = T::from_serializable(&updated_owner).map_err(reject_anyhow)?;
Ok(reply) let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.owner.delete_response(id).await;
CACHES.owner_bin.delete_response(id).await;
CACHES.list_owners.clear().await;
CACHES.list_owners_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, owner, api_key, env).await
}
_ => update::<ETagReply<Json>>(id, owner, api_key, env).await,
}
} }
pub async fn delete( pub async fn delete(
@ -116,12 +183,12 @@ pub async fn delete(
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
tokio::spawn(async move { tokio::spawn(async move {
let api_key = api_key.expect("api-key has been validated during authenticate");
CACHES.owner.delete_response(id).await; CACHES.owner.delete_response(id).await;
CACHES CACHES.owner_bin.delete_response(id).await;
.owner_ids_by_api_key CACHES.owner_ids_by_api_key.delete(api_key).await;
.delete(api_key.expect("api-key has been validated during authenticate"))
.await;
CACHES.list_owners.clear().await; CACHES.list_owners.clear().await;
CACHES.list_owners_bin.clear().await;
}); });
Ok(StatusCode::NO_CONTENT) Ok(StatusCode::NO_CONTENT)
} }

View File

@ -1,132 +1,198 @@
use anyhow::Result; use anyhow::Result;
use http::StatusCode; use http::StatusCode;
use mime::Mime;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::caches::CACHES; use crate::caches::{Cache, CachedResponse, CACHES};
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop}; use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
use crate::problem::reject_anyhow; use crate::problem::reject_anyhow;
use crate::Environment; use crate::Environment;
use super::{authenticate, check_etag, DataReply, ETagReply, Json}; use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> { pub async fn get(
let response = CACHES id: i32,
.shop etag: Option<String>,
.get_response(id, || async { accept: Option<AcceptHeader>,
let shop = Shop::get(&env.db, id).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&shop)?; ) -> Result<impl Reply, Rejection> {
let reply = with_status(reply, StatusCode::OK); async fn get<T: DataReply>(
Ok(reply) id: i32,
}) etag: Option<String>,
.await?; env: Environment,
Ok(check_etag(etag, response)) cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(id, || async {
let shop = Shop::get(&env.db, id).await?;
let reply = T::from_serializable(&shop)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.shop_bin).await
}
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.shop).await,
}
} }
pub async fn list( pub async fn list(
list_params: ListParams, list_params: ListParams,
etag: Option<String>, etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let response = CACHES async fn get<T: DataReply>(
.list_shops list_params: ListParams,
.get_response(list_params.clone(), || async { etag: Option<String>,
let shops = Shop::list(&env.db, &list_params).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&shops)?; cache: &'static Cache<ListParams, CachedResponse>,
let reply = with_status(reply, StatusCode::OK); ) -> Result<Box<dyn Reply>, Rejection> {
Ok(reply) let response = cache
}) .get_response(list_params.clone(), || async {
.await?; let shops = Shop::list(&env.db, &list_params).await?;
Ok(check_etag(etag, response)) let reply = T::from_serializable(&shops)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_shops_bin).await
}
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_shops).await,
}
} }
pub async fn create( pub async fn create(
shop: Shop, shop: Shop,
api_key: Option<Uuid>, api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?; async fn create<'a, T: DataReply + 'a>(
let shop_with_owner_id = Shop { shop: Shop,
owner_id: Some(owner_id), api_key: Option<Uuid>,
..shop env: Environment,
}; ) -> Result<Box<dyn Reply + 'a>, Rejection> {
let saved_shop = shop_with_owner_id let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
.create(&env.db) let shop_with_owner_id = Shop {
.await owner_id: Some(owner_id),
.map_err(reject_anyhow)?; ..shop
};
let saved_shop = shop_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
// also save empty interior_ref_list and merchandise_list rows // also save empty interior_ref_list and merchandise_list rows
if let Some(shop_id) = saved_shop.id { // TODO: do this in a transaction with shop.create
let interior_ref_list = InteriorRefList { if let Some(shop_id) = saved_shop.id {
id: None, let interior_ref_list = InteriorRefList {
shop_id, id: None,
owner_id: Some(owner_id), shop_id,
ref_list: sqlx::types::Json::default(), owner_id: Some(owner_id),
created_at: None, ref_list: sqlx::types::Json::default(),
updated_at: None, created_at: None,
}; updated_at: None,
interior_ref_list };
.create(&env.db) interior_ref_list
.await .create(&env.db)
.map_err(reject_anyhow)?; .await
let merchandise_list = MerchandiseList { .map_err(reject_anyhow)?;
id: None, let merchandise_list = MerchandiseList {
shop_id, id: None,
owner_id: Some(owner_id), shop_id,
form_list: sqlx::types::Json::default(), owner_id: Some(owner_id),
created_at: None, form_list: sqlx::types::Json::default(),
updated_at: None, created_at: None,
}; updated_at: None,
merchandise_list };
.create(&env.db) merchandise_list
.await .create(&env.db)
.map_err(reject_anyhow)?; .await
.map_err(reject_anyhow)?;
}
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = T::from_serializable(&saved_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.list_shops.clear().await;
CACHES.list_shops_bin.clear().await;
});
Ok(Box::new(reply))
} }
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?; match content_type {
let reply = ETagReply::<Json>::from_serializable(&saved_shop).map_err(reject_anyhow)?; Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
let reply = with_header(reply, "Location", url.as_str()); create::<ETagReply<Bincode>>(shop, api_key, env).await
let reply = with_status(reply, StatusCode::CREATED); }
tokio::spawn(async move { _ => create::<ETagReply<Json>>(shop, api_key, env).await,
CACHES.list_shops.clear().await; }
});
Ok(reply)
} }
pub async fn update( pub async fn update(
id: i32, id: i32,
shop: Shop, shop: Shop,
api_key: Option<Uuid>, api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?; async fn update<'a, T: DataReply + 'a>(
let shop_with_id_and_owner_id = if shop.owner_id.is_some() { id: i32,
// allows an owner to transfer ownership of shop to another owner shop: Shop,
Shop { api_key: Option<Uuid>,
id: Some(id), env: Environment,
..shop ) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let shop_with_id_and_owner_id = if shop.owner_id.is_some() {
// allows an owner to transfer ownership of shop to another owner
Shop {
id: Some(id),
..shop
}
} else {
Shop {
id: Some(id),
owner_id: Some(owner_id),
..shop
}
};
let updated_shop = shop_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.shop.delete_response(id).await;
CACHES.shop_bin.delete_response(id).await;
CACHES.list_shops.clear().await;
CACHES.list_shops_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, shop, api_key, env).await
} }
} else { _ => update::<ETagReply<Json>>(id, shop, api_key, env).await,
Shop { }
id: Some(id),
owner_id: Some(owner_id),
..shop
}
};
let updated_shop = shop_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&updated_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.shop.delete_response(id).await;
CACHES.list_shops.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(
@ -140,12 +206,22 @@ pub async fn delete(
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
tokio::spawn(async move { tokio::spawn(async move {
CACHES.shop.delete_response(id).await; CACHES.shop.delete_response(id).await;
CACHES.shop_bin.delete_response(id).await;
CACHES.list_shops.clear().await; CACHES.list_shops.clear().await;
CACHES.list_shops_bin.clear().await;
CACHES CACHES
.interior_ref_list_by_shop_id .interior_ref_list_by_shop_id
.delete_response(id) .delete_response(id)
.await; .await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(id)
.await;
CACHES.merchandise_list_by_shop_id.delete_response(id).await; CACHES.merchandise_list_by_shop_id.delete_response(id).await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(id)
.await;
}); });
Ok(StatusCode::NO_CONTENT) Ok(StatusCode::NO_CONTENT)
} }

View File

@ -1,126 +1,209 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use http::StatusCode; use http::StatusCode;
use mime::Mime;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::caches::CACHES; use crate::caches::{Cache, CachedResponse, CACHES};
use crate::models::{ListParams, MerchandiseList, Transaction}; use crate::models::{ListParams, MerchandiseList, Transaction};
use crate::problem::reject_anyhow; use crate::problem::reject_anyhow;
use crate::Environment; use crate::Environment;
use super::{authenticate, check_etag, DataReply, ETagReply, Json}; use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> { pub async fn get(
let response = CACHES id: i32,
.transaction etag: Option<String>,
.get_response(id, || async { accept: Option<AcceptHeader>,
let transaction = Transaction::get(&env.db, id).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&transaction)?; ) -> Result<impl Reply, Rejection> {
let reply = with_status(reply, StatusCode::OK); async fn get<T: DataReply>(
Ok(reply) id: i32,
}) etag: Option<String>,
.await?; env: Environment,
Ok(check_etag(etag, response)) cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(id, || async {
let transaction = Transaction::get(&env.db, id).await?;
let reply = T::from_serializable(&transaction)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.transaction_bin).await
}
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.transaction).await,
}
} }
pub async fn list( pub async fn list(
list_params: ListParams, list_params: ListParams,
etag: Option<String>, etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let response = CACHES async fn get<T: DataReply>(
.list_transactions list_params: ListParams,
.get_response(list_params.clone(), || async { etag: Option<String>,
let transactions = Transaction::list(&env.db, &list_params).await?; env: Environment,
let reply = ETagReply::<Json>::from_serializable(&transactions)?; cache: &'static Cache<ListParams, CachedResponse>,
let reply = with_status(reply, StatusCode::OK); ) -> Result<Box<dyn Reply>, Rejection> {
Ok(reply) let response = cache
}) .get_response(list_params.clone(), || async {
.await?; let transactions = Transaction::list(&env.db, &list_params).await?;
Ok(check_etag(etag, response)) let reply = T::from_serializable(&transactions)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_transactions_bin).await
}
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_transactions).await,
}
} }
pub async fn list_by_shop_id( pub async fn list_by_shop_id(
shop_id: i32, shop_id: i32,
list_params: ListParams, list_params: ListParams,
etag: Option<String>, etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let response = CACHES async fn get<T: DataReply>(
.list_transactions_by_shop_id shop_id: i32,
.get_response((shop_id, list_params.clone()), || async { list_params: ListParams,
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?; etag: Option<String>,
let reply = ETagReply::<Json>::from_serializable(&transactions)?; env: Environment,
let reply = with_status(reply, StatusCode::OK); cache: &'static Cache<(i32, ListParams), CachedResponse>,
Ok(reply) ) -> Result<Box<dyn Reply>, Rejection> {
}) let response = cache
.await?; .get_response((shop_id, list_params.clone()), || async {
Ok(check_etag(etag, response)) let transactions =
Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
let reply = T::from_serializable(&transactions)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(
shop_id,
list_params,
etag,
env,
&CACHES.list_transactions_by_shop_id_bin,
)
.await
}
_ => {
get::<ETagReply<Json>>(
shop_id,
list_params,
etag,
env,
&CACHES.list_transactions_by_shop_id,
)
.await
}
}
} }
pub async fn create( pub async fn create(
transaction: Transaction, transaction: Transaction,
api_key: Option<Uuid>, api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?; async fn create<'a, T: DataReply + 'a>(
let transaction_with_owner_id = Transaction { transaction: Transaction,
owner_id: Some(owner_id), api_key: Option<Uuid>,
..transaction env: Environment,
}; ) -> Result<Box<dyn Reply + 'a>, Rejection> {
let mut tx = env let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
.db let transaction_with_owner_id = Transaction {
.begin() owner_id: Some(owner_id),
.await ..transaction
.map_err(|error| reject_anyhow(anyhow!(error)))?; };
let saved_transaction = transaction_with_owner_id let mut tx = env
.create(&mut tx) .db
.begin()
.await
.map_err(|error| reject_anyhow(anyhow!(error)))?;
let saved_transaction = transaction_with_owner_id
.create(&mut tx)
.await
.map_err(reject_anyhow)?;
let quantity_delta = match transaction.is_sell {
true => transaction.quantity,
false => transaction.quantity * -1,
};
let updated_merchandise_list = MerchandiseList::update_merchandise_quantity(
&mut tx,
saved_transaction.shop_id,
&(saved_transaction.mod_name),
saved_transaction.local_form_id,
&(saved_transaction.name),
saved_transaction.form_type,
saved_transaction.is_food,
saved_transaction.price,
quantity_delta,
)
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let quantity_delta = match transaction.is_sell { tx.commit()
true => transaction.quantity, .await
false => transaction.quantity * -1, .map_err(|error| reject_anyhow(anyhow!(error)))?;
}; let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
let updated_merchandise_list = MerchandiseList::update_merchandise_quantity( let reply = T::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
&mut tx, let reply = with_header(reply, "Location", url.as_str());
saved_transaction.shop_id, let reply = with_status(reply, StatusCode::CREATED);
&(saved_transaction.mod_name), tokio::spawn(async move {
saved_transaction.local_form_id, // TODO: will this make these caches effectively useless?
&(saved_transaction.name), let merch_id = updated_merchandise_list
saved_transaction.form_type, .id
saved_transaction.is_food, .expect("saved merchandise_list has no id");
saved_transaction.price, CACHES.merchandise_list.delete_response(merch_id).await;
quantity_delta, CACHES.merchandise_list_bin.delete_response(merch_id).await;
) CACHES
.await .merchandise_list_by_shop_id
.map_err(reject_anyhow)?; .delete_response(updated_merchandise_list.shop_id)
tx.commit() .await;
.await CACHES
.map_err(|error| reject_anyhow(anyhow!(error)))?; .merchandise_list_by_shop_id_bin
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?; .delete_response(updated_merchandise_list.shop_id)
let reply = ETagReply::<Json>::from_serializable(&saved_transaction).map_err(reject_anyhow)?; .await;
let reply = with_header(reply, "Location", url.as_str()); CACHES.list_transactions.clear().await;
let reply = with_status(reply, StatusCode::CREATED); CACHES.list_transactions_bin.clear().await;
tokio::spawn(async move { CACHES.list_transactions_by_shop_id.clear().await;
// TODO: will this make these caches effectively useless? CACHES.list_transactions_by_shop_id_bin.clear().await;
CACHES.list_transactions.clear().await; CACHES.list_merchandise_lists.clear().await;
CACHES.list_transactions_by_shop_id.clear().await; CACHES.list_merchandise_lists_bin.clear().await;
CACHES });
.merchandise_list Ok(Box::new(reply))
.delete_response( }
updated_merchandise_list
.id match content_type {
.expect("saved merchandise_list has no id"), Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
) create::<ETagReply<Bincode>>(transaction, api_key, env).await
.await; }
CACHES _ => create::<ETagReply<Json>>(transaction, api_key, env).await,
.merchandise_list_by_shop_id }
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(
@ -134,8 +217,11 @@ pub async fn delete(
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
tokio::spawn(async move { tokio::spawn(async move {
CACHES.transaction.delete_response(id).await; CACHES.transaction.delete_response(id).await;
CACHES.transaction_bin.delete_response(id).await;
CACHES.list_transactions.clear().await; CACHES.list_transactions.clear().await;
CACHES.list_transactions_bin.clear().await;
CACHES.list_transactions_by_shop_id.clear().await; CACHES.list_transactions_by_shop_id.clear().await;
CACHES.list_transactions_by_shop_id_bin.clear().await;
}); });
Ok(StatusCode::NO_CONTENT) Ok(StatusCode::NO_CONTENT)
} }

View File

@ -82,6 +82,7 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::owner::get), .and_then(handlers::owner::get),
); );
@ -92,6 +93,7 @@ async fn main() -> Result<()> {
.and(warp::addr::remote()) .and(warp::addr::remote())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("x-real-ip")) .and(warp::header::optional("x-real-ip"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::owner::create), .and_then(handlers::owner::create),
); );
@ -109,6 +111,7 @@ async fn main() -> Result<()> {
.and(warp::patch()) .and(warp::patch())
.and(json_body::<Owner>()) .and(json_body::<Owner>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::owner::update), .and_then(handlers::owner::update),
); );
@ -117,6 +120,7 @@ async fn main() -> Result<()> {
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::owner::list), .and_then(handlers::owner::list),
); );
@ -125,6 +129,7 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::shop::get), .and_then(handlers::shop::get),
); );
@ -133,6 +138,7 @@ async fn main() -> Result<()> {
.and(warp::post()) .and(warp::post())
.and(json_body::<Shop>()) .and(json_body::<Shop>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::shop::create), .and_then(handlers::shop::create),
); );
@ -150,6 +156,7 @@ async fn main() -> Result<()> {
.and(warp::patch()) .and(warp::patch())
.and(json_body::<Shop>()) .and(json_body::<Shop>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::shop::update), .and_then(handlers::shop::update),
); );
@ -158,6 +165,7 @@ async fn main() -> Result<()> {
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::shop::list), .and_then(handlers::shop::list),
); );
@ -298,6 +306,7 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::transaction::get), .and_then(handlers::transaction::get),
); );
@ -306,6 +315,7 @@ async fn main() -> Result<()> {
.and(warp::post()) .and(warp::post())
.and(json_body::<Transaction>()) .and(json_body::<Transaction>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::transaction::create), .and_then(handlers::transaction::create),
); );
@ -322,6 +332,7 @@ async fn main() -> Result<()> {
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::transaction::list), .and_then(handlers::transaction::list),
); );
@ -332,6 +343,7 @@ async fn main() -> Result<()> {
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match")) .and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::transaction::list_by_shop_id), .and_then(handlers::transaction::list_by_shop_id),
); );