Update all endpoints to accept and send bincode
This commit is contained in:
parent
6ac4b03a0a
commit
e0cc81c97e
@ -289,11 +289,17 @@ pub async fn delete(
|
||||
.map_err(reject_anyhow)?;
|
||||
tokio::spawn(async move {
|
||||
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
|
||||
.interior_ref_list_by_shop_id
|
||||
.delete_response(interior_ref_list.shop_id)
|
||||
.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)
|
||||
}
|
||||
|
@ -1,46 +1,79 @@
|
||||
use anyhow::Result;
|
||||
use http::StatusCode;
|
||||
use ipnetwork::IpNetwork;
|
||||
use mime::Mime;
|
||||
use std::net::SocketAddr;
|
||||
use uuid::Uuid;
|
||||
use warp::reply::{with_header, with_status};
|
||||
use warp::{Rejection, Reply};
|
||||
|
||||
use crate::caches::CACHES;
|
||||
use crate::caches::{Cache, CachedResponse, CACHES};
|
||||
use crate::models::{ListParams, Owner};
|
||||
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
|
||||
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> {
|
||||
let response = CACHES
|
||||
.owner
|
||||
.get_response(id, || async {
|
||||
let owner = Owner::get(&env.db, id).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&owner)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
pub async fn get(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
async fn get<T: DataReply>(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
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(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let response = CACHES
|
||||
.list_owners
|
||||
.get_response(list_params.clone(), || async {
|
||||
let owners = Owner::list(&env.db, &list_params).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&owners)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
async fn get<T: DataReply>(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
cache: &'static Cache<ListParams, CachedResponse>,
|
||||
) -> Result<Box<dyn Reply>, Rejection> {
|
||||
let response = cache
|
||||
.get_response(list_params.clone(), || async {
|
||||
let owners = Owner::list(&env.db, &list_params).await?;
|
||||
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(
|
||||
@ -48,9 +81,16 @@ pub async fn create(
|
||||
remote_addr: Option<SocketAddr>,
|
||||
api_key: Option<Uuid>,
|
||||
real_ip: Option<IpNetwork>,
|
||||
content_type: Option<Mime>,
|
||||
env: Environment,
|
||||
) -> 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 {
|
||||
Some(addr) => Owner {
|
||||
api_key: Some(api_key),
|
||||
@ -68,13 +108,23 @@ pub async fn create(
|
||||
.await
|
||||
.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_status(reply, StatusCode::CREATED);
|
||||
tokio::spawn(async move {
|
||||
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 {
|
||||
Err(reject_anyhow(unauthorized_no_api_key()))
|
||||
}
|
||||
@ -84,26 +134,43 @@ pub async fn update(
|
||||
id: i32,
|
||||
owner: Owner,
|
||||
api_key: Option<Uuid>,
|
||||
content_type: Option<Mime>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let owner_with_id = Owner {
|
||||
id: Some(id),
|
||||
..owner
|
||||
};
|
||||
let updated_owner = owner_with_id
|
||||
.update(&env.db, owner_id, id)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&updated_owner).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.owner.delete_response(id).await;
|
||||
CACHES.list_owners.clear().await;
|
||||
});
|
||||
Ok(reply)
|
||||
async fn update<'a, T: DataReply + 'a>(
|
||||
id: i32,
|
||||
owner: Owner,
|
||||
api_key: Option<Uuid>,
|
||||
env: Environment,
|
||||
) -> Result<Box<dyn Reply + 'a>, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let owner_with_id = Owner {
|
||||
id: Some(id),
|
||||
..owner
|
||||
};
|
||||
let updated_owner = owner_with_id
|
||||
.update(&env.db, owner_id, id)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
|
||||
let reply = T::from_serializable(&updated_owner).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.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(
|
||||
@ -116,12 +183,12 @@ pub async fn delete(
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
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_ids_by_api_key
|
||||
.delete(api_key.expect("api-key has been validated during authenticate"))
|
||||
.await;
|
||||
CACHES.owner_bin.delete_response(id).await;
|
||||
CACHES.owner_ids_by_api_key.delete(api_key).await;
|
||||
CACHES.list_owners.clear().await;
|
||||
CACHES.list_owners_bin.clear().await;
|
||||
});
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
@ -1,132 +1,198 @@
|
||||
use anyhow::Result;
|
||||
use http::StatusCode;
|
||||
use mime::Mime;
|
||||
use uuid::Uuid;
|
||||
use warp::reply::{with_header, with_status};
|
||||
use warp::{Rejection, Reply};
|
||||
|
||||
use crate::caches::CACHES;
|
||||
use crate::caches::{Cache, CachedResponse, CACHES};
|
||||
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
|
||||
use crate::problem::reject_anyhow;
|
||||
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> {
|
||||
let response = CACHES
|
||||
.shop
|
||||
.get_response(id, || async {
|
||||
let shop = Shop::get(&env.db, id).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&shop)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
pub async fn get(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
async fn get<T: DataReply>(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
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(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let response = CACHES
|
||||
.list_shops
|
||||
.get_response(list_params.clone(), || async {
|
||||
let shops = Shop::list(&env.db, &list_params).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&shops)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
async fn get<T: DataReply>(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
cache: &'static Cache<ListParams, CachedResponse>,
|
||||
) -> Result<Box<dyn Reply>, Rejection> {
|
||||
let response = cache
|
||||
.get_response(list_params.clone(), || async {
|
||||
let shops = Shop::list(&env.db, &list_params).await?;
|
||||
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(
|
||||
shop: Shop,
|
||||
api_key: Option<Uuid>,
|
||||
content_type: Option<Mime>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let shop_with_owner_id = Shop {
|
||||
owner_id: Some(owner_id),
|
||||
..shop
|
||||
};
|
||||
let saved_shop = shop_with_owner_id
|
||||
.create(&env.db)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
async fn create<'a, T: DataReply + 'a>(
|
||||
shop: Shop,
|
||||
api_key: Option<Uuid>,
|
||||
env: Environment,
|
||||
) -> Result<Box<dyn Reply + 'a>, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let shop_with_owner_id = Shop {
|
||||
owner_id: Some(owner_id),
|
||||
..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
|
||||
if let Some(shop_id) = saved_shop.id {
|
||||
let interior_ref_list = InteriorRefList {
|
||||
id: None,
|
||||
shop_id,
|
||||
owner_id: Some(owner_id),
|
||||
ref_list: sqlx::types::Json::default(),
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
};
|
||||
interior_ref_list
|
||||
.create(&env.db)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let merchandise_list = MerchandiseList {
|
||||
id: None,
|
||||
shop_id,
|
||||
owner_id: Some(owner_id),
|
||||
form_list: sqlx::types::Json::default(),
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
};
|
||||
merchandise_list
|
||||
.create(&env.db)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
// also save empty interior_ref_list and merchandise_list rows
|
||||
// TODO: do this in a transaction with shop.create
|
||||
if let Some(shop_id) = saved_shop.id {
|
||||
let interior_ref_list = InteriorRefList {
|
||||
id: None,
|
||||
shop_id,
|
||||
owner_id: Some(owner_id),
|
||||
ref_list: sqlx::types::Json::default(),
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
};
|
||||
interior_ref_list
|
||||
.create(&env.db)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let merchandise_list = MerchandiseList {
|
||||
id: None,
|
||||
shop_id,
|
||||
owner_id: Some(owner_id),
|
||||
form_list: sqlx::types::Json::default(),
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
};
|
||||
merchandise_list
|
||||
.create(&env.db)
|
||||
.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)?;
|
||||
let reply = ETagReply::<Json>::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;
|
||||
});
|
||||
Ok(reply)
|
||||
match content_type {
|
||||
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
|
||||
create::<ETagReply<Bincode>>(shop, api_key, env).await
|
||||
}
|
||||
_ => create::<ETagReply<Json>>(shop, api_key, env).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: i32,
|
||||
shop: Shop,
|
||||
api_key: Option<Uuid>,
|
||||
content_type: Option<Mime>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, 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
|
||||
async fn update<'a, T: DataReply + 'a>(
|
||||
id: i32,
|
||||
shop: Shop,
|
||||
api_key: Option<Uuid>,
|
||||
env: Environment,
|
||||
) -> 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 {
|
||||
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)
|
||||
_ => update::<ETagReply<Json>>(id, shop, api_key, env).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
@ -140,12 +206,22 @@ pub async fn delete(
|
||||
.map_err(reject_anyhow)?;
|
||||
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;
|
||||
CACHES
|
||||
.interior_ref_list_by_shop_id
|
||||
.delete_response(id)
|
||||
.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_bin
|
||||
.delete_response(id)
|
||||
.await;
|
||||
});
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
@ -1,126 +1,209 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use http::StatusCode;
|
||||
use mime::Mime;
|
||||
use uuid::Uuid;
|
||||
use warp::reply::{with_header, with_status};
|
||||
use warp::{Rejection, Reply};
|
||||
|
||||
use crate::caches::CACHES;
|
||||
use crate::caches::{Cache, CachedResponse, CACHES};
|
||||
use crate::models::{ListParams, MerchandiseList, Transaction};
|
||||
use crate::problem::reject_anyhow;
|
||||
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> {
|
||||
let response = CACHES
|
||||
.transaction
|
||||
.get_response(id, || async {
|
||||
let transaction = Transaction::get(&env.db, id).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&transaction)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
pub async fn get(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
async fn get<T: DataReply>(
|
||||
id: i32,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
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(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let response = CACHES
|
||||
.list_transactions
|
||||
.get_response(list_params.clone(), || async {
|
||||
let transactions = Transaction::list(&env.db, &list_params).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
async fn get<T: DataReply>(
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
cache: &'static Cache<ListParams, CachedResponse>,
|
||||
) -> Result<Box<dyn Reply>, Rejection> {
|
||||
let response = cache
|
||||
.get_response(list_params.clone(), || async {
|
||||
let transactions = Transaction::list(&env.db, &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>>(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(
|
||||
shop_id: i32,
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
accept: Option<AcceptHeader>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let response = CACHES
|
||||
.list_transactions_by_shop_id
|
||||
.get_response((shop_id, list_params.clone()), || async {
|
||||
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
})
|
||||
.await?;
|
||||
Ok(check_etag(etag, response))
|
||||
async fn get<T: DataReply>(
|
||||
shop_id: i32,
|
||||
list_params: ListParams,
|
||||
etag: Option<String>,
|
||||
env: Environment,
|
||||
cache: &'static Cache<(i32, ListParams), CachedResponse>,
|
||||
) -> Result<Box<dyn Reply>, Rejection> {
|
||||
let response = cache
|
||||
.get_response((shop_id, list_params.clone()), || async {
|
||||
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(
|
||||
transaction: Transaction,
|
||||
api_key: Option<Uuid>,
|
||||
content_type: Option<Mime>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let transaction_with_owner_id = Transaction {
|
||||
owner_id: Some(owner_id),
|
||||
..transaction
|
||||
};
|
||||
let mut tx = env
|
||||
.db
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|error| reject_anyhow(anyhow!(error)))?;
|
||||
let saved_transaction = transaction_with_owner_id
|
||||
.create(&mut tx)
|
||||
async fn create<'a, T: DataReply + 'a>(
|
||||
transaction: Transaction,
|
||||
api_key: Option<Uuid>,
|
||||
env: Environment,
|
||||
) -> Result<Box<dyn Reply + 'a>, Rejection> {
|
||||
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||
let transaction_with_owner_id = Transaction {
|
||||
owner_id: Some(owner_id),
|
||||
..transaction
|
||||
};
|
||||
let mut tx = env
|
||||
.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
|
||||
.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
|
||||
.map_err(reject_anyhow)?;
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|error| reject_anyhow(anyhow!(error)))?;
|
||||
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
|
||||
let reply = ETagReply::<Json>::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
|
||||
let reply = with_header(reply, "Location", url.as_str());
|
||||
let reply = with_status(reply, StatusCode::CREATED);
|
||||
tokio::spawn(async move {
|
||||
// TODO: will this make these caches effectively useless?
|
||||
CACHES.list_transactions.clear().await;
|
||||
CACHES.list_transactions_by_shop_id.clear().await;
|
||||
CACHES
|
||||
.merchandise_list
|
||||
.delete_response(
|
||||
updated_merchandise_list
|
||||
.id
|
||||
.expect("saved merchandise_list has no id"),
|
||||
)
|
||||
.await;
|
||||
CACHES
|
||||
.merchandise_list_by_shop_id
|
||||
.delete_response(updated_merchandise_list.shop_id)
|
||||
.await;
|
||||
CACHES.list_merchandise_lists.clear().await;
|
||||
});
|
||||
Ok(reply)
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|error| reject_anyhow(anyhow!(error)))?;
|
||||
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
|
||||
let reply = T::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
|
||||
let reply = with_header(reply, "Location", url.as_str());
|
||||
let reply = with_status(reply, StatusCode::CREATED);
|
||||
tokio::spawn(async move {
|
||||
// TODO: will this make these caches effectively useless?
|
||||
let merch_id = updated_merchandise_list
|
||||
.id
|
||||
.expect("saved merchandise_list has no id");
|
||||
CACHES.merchandise_list.delete_response(merch_id).await;
|
||||
CACHES.merchandise_list_bin.delete_response(merch_id).await;
|
||||
CACHES
|
||||
.merchandise_list_by_shop_id
|
||||
.delete_response(updated_merchandise_list.shop_id)
|
||||
.await;
|
||||
CACHES
|
||||
.merchandise_list_by_shop_id_bin
|
||||
.delete_response(updated_merchandise_list.shop_id)
|
||||
.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_bin.clear().await;
|
||||
CACHES.list_merchandise_lists.clear().await;
|
||||
CACHES.list_merchandise_lists_bin.clear().await;
|
||||
});
|
||||
Ok(Box::new(reply))
|
||||
}
|
||||
|
||||
match content_type {
|
||||
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
|
||||
create::<ETagReply<Bincode>>(transaction, api_key, env).await
|
||||
}
|
||||
_ => create::<ETagReply<Json>>(transaction, api_key, env).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
@ -134,8 +217,11 @@ pub async fn delete(
|
||||
.map_err(reject_anyhow)?;
|
||||
tokio::spawn(async move {
|
||||
CACHES.transaction.delete_response(id).await;
|
||||
CACHES.transaction_bin.delete_response(id).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_bin.clear().await;
|
||||
});
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
12
src/main.rs
12
src/main.rs
@ -82,6 +82,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::path::end())
|
||||
.and(warp::get())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::owner::get),
|
||||
);
|
||||
@ -92,6 +93,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::addr::remote())
|
||||
.and(warp::header::optional("api-key"))
|
||||
.and(warp::header::optional("x-real-ip"))
|
||||
.and(warp::header::optional("content-type"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::owner::create),
|
||||
);
|
||||
@ -109,6 +111,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::patch())
|
||||
.and(json_body::<Owner>())
|
||||
.and(warp::header::optional("api-key"))
|
||||
.and(warp::header::optional("content-type"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::owner::update),
|
||||
);
|
||||
@ -117,6 +120,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::get())
|
||||
.and(warp::query::<ListParams>())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::owner::list),
|
||||
);
|
||||
@ -125,6 +129,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::path::end())
|
||||
.and(warp::get())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::shop::get),
|
||||
);
|
||||
@ -133,6 +138,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::post())
|
||||
.and(json_body::<Shop>())
|
||||
.and(warp::header::optional("api-key"))
|
||||
.and(warp::header::optional("content-type"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::shop::create),
|
||||
);
|
||||
@ -150,6 +156,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::patch())
|
||||
.and(json_body::<Shop>())
|
||||
.and(warp::header::optional("api-key"))
|
||||
.and(warp::header::optional("content-type"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::shop::update),
|
||||
);
|
||||
@ -158,6 +165,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::get())
|
||||
.and(warp::query::<ListParams>())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::shop::list),
|
||||
);
|
||||
@ -298,6 +306,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::path::end())
|
||||
.and(warp::get())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::transaction::get),
|
||||
);
|
||||
@ -306,6 +315,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::post())
|
||||
.and(json_body::<Transaction>())
|
||||
.and(warp::header::optional("api-key"))
|
||||
.and(warp::header::optional("content-type"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::transaction::create),
|
||||
);
|
||||
@ -322,6 +332,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::get())
|
||||
.and(warp::query::<ListParams>())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::transaction::list),
|
||||
);
|
||||
@ -332,6 +343,7 @@ async fn main() -> Result<()> {
|
||||
.and(warp::get())
|
||||
.and(warp::query::<ListParams>())
|
||||
.and(warp::header::optional("if-none-match"))
|
||||
.and(warp::header::optional("accept"))
|
||||
.and(with_env(env.clone()))
|
||||
.and_then(handlers::transaction::list_by_shop_id),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user