Simplify handler content_type switching

The inner functions were a bit too cumbersome. I found a more compact way to do it in one flat function.
This commit is contained in:
Tyler Hallada 2020-11-08 20:10:32 -05:00
parent e0cc81c97e
commit 377a260a2f
6 changed files with 758 additions and 806 deletions

View File

@ -5,12 +5,14 @@ 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::{Cache, CachedResponse, CACHES}; use crate::caches::CACHES;
use crate::models::{InteriorRefList, ListParams}; use crate::models::{InteriorRefList, ListParams};
use crate::problem::reject_anyhow; use crate::problem::reject_anyhow;
use crate::Environment; use crate::Environment;
use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json}; use super::{
authenticate, check_etag, AcceptHeader, Bincode, ContentType, DataReply, ETagReply, Json,
};
pub async fn get( pub async fn get(
id: i32, id: i32,
@ -18,29 +20,28 @@ pub async fn get(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
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 interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply = T::from_serializable(&interior_ref_list)?;
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() => { Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.interior_ref_list_bin).await (ContentType::Bincode, &CACHES.interior_ref_list_bin)
} }
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.interior_ref_list).await, _ => (ContentType::Json, &CACHES.interior_ref_list),
} };
let response = cache
.get_response(id, || async {
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&interior_ref_list)?)
}
ContentType::Json => {
Box::new(ETagReply::<Json>::from_serializable(&interior_ref_list)?)
}
};
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
} }
pub async fn get_by_shop_id( pub async fn get_by_shop_id(
@ -49,30 +50,29 @@ pub async fn get_by_shop_id(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
shop_id: i32, Some(accept) if accept.accepts_bincode() => (
etag: Option<String>, ContentType::Bincode,
env: Environment, &CACHES.interior_ref_list_by_shop_id_bin,
cache: &'static Cache<i32, CachedResponse>, ),
) -> Result<Box<dyn Reply>, Rejection> { _ => (ContentType::Json, &CACHES.interior_ref_list_by_shop_id),
let response = cache };
.get_response(shop_id, || async { let response = cache
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?; .get_response(shop_id, || async {
let reply = T::from_serializable(&interior_ref_list)?; let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
let reply = with_status(reply, StatusCode::OK); let reply: Box<dyn Reply> = match content_type {
Ok(reply) ContentType::Bincode => {
}) Box::new(ETagReply::<Bincode>::from_serializable(&interior_ref_list)?)
.await?; }
Ok(Box::new(check_etag(etag, response))) ContentType::Json => {
} Box::new(ETagReply::<Json>::from_serializable(&interior_ref_list)?)
}
match accept { };
Some(accept) if accept.accepts_bincode() => { let reply = with_status(reply, StatusCode::OK);
get::<ETagReply<Bincode>>(shop_id, etag, env, &CACHES.interior_ref_list_by_shop_id_bin) Ok(reply)
.await })
} .await?;
_ => get::<ETagReply<Json>>(shop_id, etag, env, &CACHES.interior_ref_list_by_shop_id).await, Ok(check_etag(etag, response))
}
} }
pub async fn list( pub async fn list(
@ -81,31 +81,29 @@ pub async fn list(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
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 interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply = T::from_serializable(&interior_ref_lists)?;
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() => { Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_interior_ref_lists_bin) (ContentType::Bincode, &CACHES.list_interior_ref_lists_bin)
.await
} }
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_interior_ref_lists).await, _ => (ContentType::Json, &CACHES.list_interior_ref_lists),
} };
let response = cache
.get_response(list_params.clone(), || async {
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(ETagReply::<Bincode>::from_serializable(
&interior_ref_lists,
)?),
ContentType::Json => {
Box::new(ETagReply::<Json>::from_serializable(&interior_ref_lists)?)
}
};
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
} }
pub async fn create( pub async fn create(
@ -114,48 +112,49 @@ pub async fn create(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn create<'a, T: DataReply + 'a>( let content_type = match content_type {
interior_ref_list: InteriorRefList,
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 ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_interior_ref_list)
.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_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(saved_interior_ref_list.shop_id)
.await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
create::<ETagReply<Bincode>>(interior_ref_list, api_key, env).await ContentType::Bincode
} }
_ => create::<ETagReply<Json>>(interior_ref_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&saved_interior_ref_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&saved_interior_ref_list)
.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_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(saved_interior_ref_list.shop_id)
.await;
});
Ok(reply)
} }
pub async fn update( pub async fn update(
@ -165,58 +164,59 @@ pub async fn update(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
id: i32,
interior_ref_list: InteriorRefList,
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 interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_interior_ref_list).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.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, interior_ref_list, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(id, interior_ref_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&updated_interior_ref_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&updated_interior_ref_list)
.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.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(reply)
} }
pub async fn update_by_shop_id( pub async fn update_by_shop_id(
@ -226,53 +226,54 @@ pub async fn update_by_shop_id(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
shop_id: i32,
interior_ref_list: InteriorRefList,
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 interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id");
CACHES.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(shop_id, interior_ref_list, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(shop_id, interior_ref_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&updated_interior_ref_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&updated_interior_ref_list)
.map_err(reject_anyhow)?,
),
};
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id");
CACHES.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(

View File

@ -5,12 +5,14 @@ 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::{Cache, CachedResponse, CACHES}; use crate::caches::CACHES;
use crate::models::{ListParams, MerchandiseList}; use crate::models::{ListParams, MerchandiseList};
use crate::problem::reject_anyhow; use crate::problem::reject_anyhow;
use crate::Environment; use crate::Environment;
use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json}; use super::{
authenticate, check_etag, AcceptHeader, Bincode, ContentType, DataReply, ETagReply, Json,
};
pub async fn get( pub async fn get(
id: i32, id: i32,
@ -18,29 +20,28 @@ pub async fn get(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
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 merchandise_list = MerchandiseList::get(&env.db, id).await?;
let reply = T::from_serializable(&merchandise_list)?;
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() => { Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.merchandise_list_bin).await (ContentType::Bincode, &CACHES.merchandise_list_bin)
} }
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.merchandise_list).await, _ => (ContentType::Json, &CACHES.merchandise_list),
} };
let response = cache
.get_response(id, || async {
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&merchandise_list)?)
}
ContentType::Json => {
Box::new(ETagReply::<Json>::from_serializable(&merchandise_list)?)
}
};
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
} }
pub async fn get_by_shop_id( pub async fn get_by_shop_id(
@ -49,30 +50,28 @@ pub async fn get_by_shop_id(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
shop_id: i32,
etag: Option<String>,
env: Environment,
cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(shop_id, || async {
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = T::from_serializable(&merchandise_list)?;
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() => { Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id_bin) (ContentType::Bincode, &CACHES.merchandise_list_bin)
.await
} }
_ => get::<ETagReply<Json>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id).await, _ => (ContentType::Json, &CACHES.merchandise_list),
} };
let response = cache
.get_response(shop_id, || async {
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&merchandise_list)?)
}
ContentType::Json => {
Box::new(ETagReply::<Json>::from_serializable(&merchandise_list)?)
}
};
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
} }
pub async fn list( pub async fn list(
@ -81,30 +80,28 @@ pub async fn list(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
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 merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
let reply = T::from_serializable(&merchandise_lists)?;
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() => { Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_merchandise_lists_bin) (ContentType::Bincode, &CACHES.list_merchandise_lists_bin)
.await
} }
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_merchandise_lists).await, _ => (ContentType::Json, &CACHES.list_merchandise_lists),
} };
let response = cache
.get_response(list_params.clone(), || async {
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&merchandise_lists)?)
}
ContentType::Json => {
Box::new(ETagReply::<Json>::from_serializable(&merchandise_lists)?)
}
};
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
} }
pub async fn create( pub async fn create(
@ -113,47 +110,48 @@ pub async fn create(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn create<'a, T: DataReply + 'a>( let content_type = match content_type {
merchandise_list: MerchandiseList,
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 ref_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let saved_merchandise_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&saved_merchandise_list).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_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
CACHES
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(saved_merchandise_list.shop_id)
.await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
create::<ETagReply<Bincode>>(merchandise_list, api_key, env).await ContentType::Bincode
} }
_ => create::<ETagReply<Json>>(merchandise_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let saved_merchandise_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&saved_merchandise_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&saved_merchandise_list).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_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
CACHES
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(saved_merchandise_list.shop_id)
.await;
});
Ok(reply)
} }
pub async fn update( pub async fn update(
@ -163,58 +161,59 @@ pub async fn update(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
id: i32,
merchandise_list: MerchandiseList,
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 merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_merchandise_list).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.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(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_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 => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, merchandise_list, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(id, merchandise_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&updated_merchandise_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&updated_merchandise_list)
.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.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(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_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
});
Ok(reply)
} }
pub async fn update_by_shop_id( pub async fn update_by_shop_id(
@ -224,54 +223,54 @@ pub async fn update_by_shop_id(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
shop_id: i32,
merchandise_list: MerchandiseList,
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 merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&updated_merchandise_list)
.map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_merchandise_list
.id
.expect("saved merchandise_list has no id");
CACHES.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(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_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 => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(shop_id, merchandise_list, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(shop_id, merchandise_list, api_key, env).await, _ => ContentType::Json,
} };
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&updated_merchandise_list)
.map_err(reject_anyhow)?,
),
ContentType::Json => Box::new(
ETagReply::<Json>::from_serializable(&updated_merchandise_list)
.map_err(reject_anyhow)?,
),
};
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_merchandise_list
.id
.expect("saved merchandise_list has no id");
CACHES.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(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_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(

View File

@ -64,6 +64,12 @@ pub trait DataReply: Reply + Sized {
pub struct Json {} pub struct Json {}
pub struct Bincode {} pub struct Bincode {}
#[derive(Debug, PartialEq, Eq)]
pub enum ContentType {
Json,
Bincode,
}
impl Reply for ETagReply<Json> { impl Reply for ETagReply<Json> {
fn into_response(self) -> Response { fn into_response(self) -> Response {
let mut res = Response::new(self.body.into()); let mut res = Response::new(self.body.into());

View File

@ -7,12 +7,14 @@ 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::{Cache, CachedResponse, CACHES}; use crate::caches::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, AcceptHeader, Bincode, DataReply, ETagReply, Json}; use super::{
authenticate, check_etag, AcceptHeader, Bincode, ContentType, DataReply, ETagReply, Json,
};
pub async fn get( pub async fn get(
id: i32, id: i32,
@ -20,29 +22,22 @@ pub async fn get(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
id: i32, Some(accept) if accept.accepts_bincode() => (ContentType::Bincode, &CACHES.owner_bin),
etag: Option<String>, _ => (ContentType::Json, &CACHES.owner),
env: Environment, };
cache: &'static Cache<i32, CachedResponse>, let response = cache
) -> Result<Box<dyn Reply>, Rejection> { .get_response(id, || async {
let response = cache let owner = Owner::get(&env.db, id).await?;
.get_response(id, || async { let reply: Box<dyn Reply> = match content_type {
let owner = Owner::get(&env.db, id).await?; ContentType::Bincode => Box::new(ETagReply::<Bincode>::from_serializable(&owner)?),
let reply = T::from_serializable(&owner)?; ContentType::Json => Box::new(ETagReply::<Json>::from_serializable(&owner)?),
let reply = with_status(reply, StatusCode::OK); };
Ok(reply) let reply = with_status(reply, StatusCode::OK);
}) Ok(reply)
.await?; })
Ok(Box::new(check_etag(etag, response))) .await?;
} Ok(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(
@ -51,29 +46,22 @@ pub async fn list(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
list_params: ListParams, Some(accept) if accept.accepts_bincode() => (ContentType::Bincode, &CACHES.list_owners_bin),
etag: Option<String>, _ => (ContentType::Json, &CACHES.list_owners),
env: Environment, };
cache: &'static Cache<ListParams, CachedResponse>, let response = cache
) -> Result<Box<dyn Reply>, Rejection> { .get_response(list_params.clone(), || async {
let response = cache let owners = Owner::list(&env.db, &list_params).await?;
.get_response(list_params.clone(), || async { let reply: Box<dyn Reply> = match content_type {
let owners = Owner::list(&env.db, &list_params).await?; ContentType::Bincode => Box::new(ETagReply::<Bincode>::from_serializable(&owners)?),
let reply = T::from_serializable(&owners)?; ContentType::Json => Box::new(ETagReply::<Json>::from_serializable(&owners)?),
let reply = with_status(reply, StatusCode::OK); };
Ok(reply) let reply = with_status(reply, StatusCode::OK);
}) Ok(reply)
.await?; })
Ok(Box::new(check_etag(etag, response))) .await?;
} Ok(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(
@ -84,13 +72,13 @@ pub async fn create(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn create<'a, T: DataReply + 'a>( if let Some(api_key) = api_key {
owner: Owner, let content_type = match content_type {
remote_addr: Option<SocketAddr>, Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
api_key: Uuid, ContentType::Bincode
real_ip: Option<IpNetwork>, }
env: Environment, _ => ContentType::Json,
) -> 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),
@ -108,23 +96,21 @@ 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 = T::from_serializable(&saved_owner).map_err(reject_anyhow)?; let reply: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&saved_owner).map_err(reject_anyhow)?,
),
ContentType::Json => {
Box::new(ETagReply::<Json>::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; CACHES.list_owners_bin.clear().await;
}); });
Ok(Box::new(reply)) Ok(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()))
} }
@ -137,40 +123,39 @@ pub async fn update(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
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 => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, owner, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(id, owner, api_key, env).await, _ => ContentType::Json,
} };
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: Box<dyn Reply> = match content_type {
ContentType::Bincode => Box::new(
ETagReply::<Bincode>::from_serializable(&updated_owner).map_err(reject_anyhow)?,
),
ContentType::Json => {
Box::new(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.owner_bin.delete_response(id).await;
CACHES.list_owners.clear().await;
CACHES.list_owners_bin.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(

View File

@ -5,12 +5,14 @@ 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::{Cache, CachedResponse, CACHES}; use crate::caches::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, AcceptHeader, Bincode, DataReply, ETagReply, Json}; use super::{
authenticate, check_etag, AcceptHeader, Bincode, ContentType, DataReply, ETagReply, Json,
};
pub async fn get( pub async fn get(
id: i32, id: i32,
@ -18,29 +20,22 @@ pub async fn get(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
id: i32, Some(accept) if accept.accepts_bincode() => (ContentType::Bincode, &CACHES.shop_bin),
etag: Option<String>, _ => (ContentType::Json, &CACHES.shop),
env: Environment, };
cache: &'static Cache<i32, CachedResponse>, let response = cache
) -> Result<Box<dyn Reply>, Rejection> { .get_response(id, || async {
let response = cache let shop = Shop::get(&env.db, id).await?;
.get_response(id, || async { let reply: Box<dyn Reply> = match content_type {
let shop = Shop::get(&env.db, id).await?; ContentType::Bincode => Box::new(ETagReply::<Bincode>::from_serializable(&shop)?),
let reply = T::from_serializable(&shop)?; ContentType::Json => Box::new(ETagReply::<Json>::from_serializable(&shop)?),
let reply = with_status(reply, StatusCode::OK); };
Ok(reply) let reply = with_status(reply, StatusCode::OK);
}) Ok(reply)
.await?; })
Ok(Box::new(check_etag(etag, response))) .await?;
} Ok(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(
@ -49,29 +44,22 @@ pub async fn list(
accept: Option<AcceptHeader>, accept: Option<AcceptHeader>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>( let (content_type, cache) = match accept {
list_params: ListParams, Some(accept) if accept.accepts_bincode() => (ContentType::Bincode, &CACHES.list_shops_bin),
etag: Option<String>, _ => (ContentType::Json, &CACHES.list_shops),
env: Environment, };
cache: &'static Cache<ListParams, CachedResponse>, let response = cache
) -> Result<Box<dyn Reply>, Rejection> { .get_response(list_params.clone(), || async {
let response = cache let shops = Shop::list(&env.db, &list_params).await?;
.get_response(list_params.clone(), || async { let reply: Box<dyn Reply> = match content_type {
let shops = Shop::list(&env.db, &list_params).await?; ContentType::Bincode => Box::new(ETagReply::<Bincode>::from_serializable(&shops)?),
let reply = T::from_serializable(&shops)?; ContentType::Json => Box::new(ETagReply::<Json>::from_serializable(&shops)?),
let reply = with_status(reply, StatusCode::OK); };
Ok(reply) let reply = with_status(reply, StatusCode::OK);
}) Ok(reply)
.await?; })
Ok(Box::new(check_etag(etag, response))) .await?;
} Ok(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(
@ -80,67 +68,67 @@ pub async fn create(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn create<'a, T: DataReply + 'a>( let content_type = match content_type {
shop: Shop, Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
api_key: Option<Uuid>, ContentType::Bincode
env: Environment, }
) -> Result<Box<dyn Reply + 'a>, Rejection> { _ => ContentType::Json,
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?; };
let shop_with_owner_id = Shop { 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
// 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), owner_id: Some(owner_id),
..shop ref_list: sqlx::types::Json::default(),
created_at: None,
updated_at: None,
}; };
let saved_shop = shop_with_owner_id 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) .create(&env.db)
.await .await
.map_err(reject_anyhow)?; .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))
} }
match content_type { let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => { let reply: Box<dyn Reply> = match content_type {
create::<ETagReply<Bincode>>(shop, api_key, env).await ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&saved_shop).map_err(reject_anyhow)?)
} }
_ => create::<ETagReply<Json>>(shop, api_key, env).await, ContentType::Json => {
} Box::new(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;
CACHES.list_shops_bin.clear().await;
});
Ok(reply)
} }
pub async fn update( pub async fn update(
@ -150,49 +138,48 @@ pub async fn update(
content_type: Option<Mime>, content_type: Option<Mime>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
async fn update<'a, T: DataReply + 'a>( let content_type = match content_type {
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 => { Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, shop, api_key, env).await ContentType::Bincode
} }
_ => update::<ETagReply<Json>>(id, shop, api_key, env).await, _ => ContentType::Json,
} };
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: Box<dyn Reply> = match content_type {
ContentType::Bincode => {
Box::new(ETagReply::<Bincode>::from_serializable(&updated_shop).map_err(reject_anyhow)?)
}
ContentType::Json => {
Box::new(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.shop_bin.delete_response(id).await;
CACHES.list_shops.clear().await;
CACHES.list_shops_bin.clear().await;
});
Ok(reply)
} }
pub async fn delete( pub async fn delete(

View File

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