mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Cleanup up some code with the power of From
This commit is contained in:
parent
5f39b0e837
commit
9ac61731f7
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -55,7 +55,7 @@ checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "api"
|
name = "api"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "api"
|
name = "api"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -77,24 +77,28 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<Result<T, String>> for Response<T> {
|
impl<T, E> From<Result<Option<T>, E>> for Response<T>
|
||||||
fn from(val: Result<T, String>) -> Self {
|
where
|
||||||
|
T: Default,
|
||||||
|
E: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn from(val: Result<Option<T>, E>) -> Self {
|
||||||
match val {
|
match val {
|
||||||
Ok(val) => Response::Success(val),
|
Ok(Some(val)) => Response::Success(val),
|
||||||
Err(e) => Response::error(500, e),
|
Ok(None) => Response::Success(T::default()),
|
||||||
|
Err(e) => Response::error(500, format!("{:?}", e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<Result<Option<T>, String>> for Response<T>
|
impl<T, E> From<Result<T, E>> for Response<T>
|
||||||
where
|
where
|
||||||
T: Default,
|
E: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
fn from(val: Result<Option<T>, String>) -> Self {
|
fn from(val: Result<T, E>) -> Self {
|
||||||
match val {
|
match val {
|
||||||
Ok(Some(val)) => Response::Success(val),
|
Ok(v) => Response::success(v),
|
||||||
Ok(None) => Response::Success(T::default()),
|
Err(e) => Response::error(500, format!("{:?}", e)),
|
||||||
Err(e) => Response::error(500, e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ use recipes::{IngredientKey, RecipeEntry};
|
|||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::{debug, error, info, instrument};
|
use tracing::{debug, info, instrument};
|
||||||
|
|
||||||
use client_api as api;
|
use client_api as api;
|
||||||
use storage::{APIStore, AuthStore};
|
use storage::{APIStore, AuthStore};
|
||||||
@ -86,17 +86,13 @@ async fn api_recipe_entry(
|
|||||||
Path(recipe_id): Path<String>,
|
Path(recipe_id): Path<String>,
|
||||||
) -> api::Response<Option<RecipeEntry>> {
|
) -> api::Response<Option<RecipeEntry>> {
|
||||||
use storage::{UserId, UserIdFromSession::*};
|
use storage::{UserId, UserIdFromSession::*};
|
||||||
let result = match session {
|
match session {
|
||||||
NoUserId => store
|
NoUserId => store.get_recipe_entry(recipe_id).await.into(),
|
||||||
.get_recipe_entry(recipe_id)
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
|
||||||
FoundUserId(UserId(id)) => app_store
|
FoundUserId(UserId(id)) => app_store
|
||||||
.get_recipe_entry_for_user(id, recipe_id)
|
.get_recipe_entry_for_user(id, recipe_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
.into(),
|
||||||
};
|
}
|
||||||
result.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn api_recipe_delete(
|
async fn api_recipe_delete(
|
||||||
@ -105,21 +101,13 @@ async fn api_recipe_delete(
|
|||||||
Path(recipe_id): Path<String>,
|
Path(recipe_id): Path<String>,
|
||||||
) -> api::EmptyResponse {
|
) -> api::EmptyResponse {
|
||||||
use storage::{UserId, UserIdFromSession::*};
|
use storage::{UserId, UserIdFromSession::*};
|
||||||
let result = match session {
|
match session {
|
||||||
NoUserId => api::EmptyResponse::Unauthorized,
|
NoUserId => api::EmptyResponse::Unauthorized,
|
||||||
FoundUserId(UserId(id)) => {
|
FoundUserId(UserId(id)) => app_store
|
||||||
if let Err(e) = app_store
|
|
||||||
.delete_recipes_for_user(&id, &vec![recipe_id])
|
.delete_recipes_for_user(&id, &vec![recipe_id])
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Error: {:?}", e))
|
.into(),
|
||||||
{
|
|
||||||
api::EmptyResponse::error(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), e)
|
|
||||||
} else {
|
|
||||||
api::EmptyResponse::success(())
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
result.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
@ -130,17 +118,10 @@ async fn api_recipes(
|
|||||||
) -> api::RecipeEntryResponse {
|
) -> api::RecipeEntryResponse {
|
||||||
// Select recipes based on the user-id if it exists or serve the default if it does not.
|
// Select recipes based on the user-id if it exists or serve the default if it does not.
|
||||||
use storage::{UserId, UserIdFromSession::*};
|
use storage::{UserId, UserIdFromSession::*};
|
||||||
let result = match session {
|
match session {
|
||||||
NoUserId => store
|
NoUserId => api::RecipeEntryResponse::from(store.get_recipes().await),
|
||||||
.get_recipes()
|
FoundUserId(UserId(id)) => app_store.get_recipes_for_user(id.as_str()).await.into(),
|
||||||
.await
|
}
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
|
||||||
FoundUserId(UserId(id)) => app_store
|
|
||||||
.get_recipes_for_user(id.as_str())
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
|
||||||
};
|
|
||||||
result.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
@ -151,14 +132,10 @@ async fn api_category_mappings(
|
|||||||
use storage::UserIdFromSession::*;
|
use storage::UserIdFromSession::*;
|
||||||
match session {
|
match session {
|
||||||
NoUserId => api::Response::Unauthorized,
|
NoUserId => api::Response::Unauthorized,
|
||||||
FoundUserId(user_id) => match app_store.get_category_mappings_for_user(&user_id.0).await {
|
FoundUserId(user_id) => app_store
|
||||||
Ok(Some(mappings)) => api::CategoryMappingResponse::from(mappings),
|
.get_category_mappings_for_user(&user_id.0)
|
||||||
Ok(None) => api::CategoryMappingResponse::from(Vec::new()),
|
.await
|
||||||
Err(e) => api::CategoryMappingResponse::error(
|
.into(),
|
||||||
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
|
|
||||||
format!("{:?}", e),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,38 +169,25 @@ async fn api_categories(
|
|||||||
) -> api::Response<String> {
|
) -> api::Response<String> {
|
||||||
// Select Categories based on the user-id if it exists or serve the default if it does not.
|
// Select Categories based on the user-id if it exists or serve the default if it does not.
|
||||||
use storage::{UserId, UserIdFromSession::*};
|
use storage::{UserId, UserIdFromSession::*};
|
||||||
let categories_result = match session {
|
match session {
|
||||||
NoUserId => store
|
NoUserId => store.get_categories().await.into(),
|
||||||
.get_categories()
|
FoundUserId(UserId(id)) => app_store.get_categories_for_user(id.as_str()).await.into(),
|
||||||
.await
|
}
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
|
||||||
FoundUserId(UserId(id)) => app_store
|
|
||||||
.get_categories_for_user(id.as_str())
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Error: {:?}", e)),
|
|
||||||
};
|
|
||||||
categories_result.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn api_save_categories(
|
async fn api_save_categories(
|
||||||
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
|
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
|
||||||
session: storage::UserIdFromSession,
|
session: storage::UserIdFromSession,
|
||||||
Json(categories): Json<String>,
|
Json(categories): Json<String>,
|
||||||
) -> api::CategoryResponse {
|
) -> api::EmptyResponse {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(id)) = session {
|
if let FoundUserId(UserId(id)) = session {
|
||||||
if let Err(e) = app_store
|
app_store
|
||||||
.store_categories_for_user(id.as_str(), categories.as_str())
|
.store_categories_for_user(id.as_str(), categories.as_str())
|
||||||
.await
|
.await
|
||||||
{
|
.into()
|
||||||
return api::Response::error(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
|
|
||||||
format!("{:?}", e),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
api::CategoryResponse::success("Successfully saved categories".into())
|
|
||||||
} else {
|
} else {
|
||||||
api::CategoryResponse::Unauthorized
|
api::EmptyResponse::Unauthorized
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,10 +198,10 @@ async fn api_save_recipes(
|
|||||||
) -> api::EmptyResponse {
|
) -> api::EmptyResponse {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(id)) = session {
|
if let FoundUserId(UserId(id)) = session {
|
||||||
let result = app_store
|
app_store
|
||||||
.store_recipes_for_user(id.as_str(), &recipes)
|
.store_recipes_for_user(id.as_str(), &recipes)
|
||||||
.await;
|
.await
|
||||||
result.map_err(|e| format!("Error: {:?}", e)).into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
api::EmptyResponse::Unauthorized
|
api::EmptyResponse::Unauthorized
|
||||||
}
|
}
|
||||||
@ -249,11 +213,7 @@ async fn api_plan(
|
|||||||
) -> api::PlanDataResponse {
|
) -> api::PlanDataResponse {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(id)) = session {
|
if let FoundUserId(UserId(id)) = session {
|
||||||
app_store
|
app_store.fetch_latest_meal_plan(&id).await.into()
|
||||||
.fetch_latest_meal_plan(&id)
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Error: {:?}", e))
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
api::Response::Unauthorized
|
api::Response::Unauthorized
|
||||||
}
|
}
|
||||||
@ -266,11 +226,7 @@ async fn api_plan_since(
|
|||||||
) -> api::PlanHistoryResponse {
|
) -> api::PlanHistoryResponse {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(id)) = session {
|
if let FoundUserId(UserId(id)) = session {
|
||||||
app_store
|
app_store.fetch_meal_plans_since(&id, date).await.into()
|
||||||
.fetch_meal_plans_since(&id, date)
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Error: {:?}", e))
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
api::PlanHistoryResponse::Unauthorized
|
api::PlanHistoryResponse::Unauthorized
|
||||||
}
|
}
|
||||||
@ -286,7 +242,6 @@ async fn api_save_plan(
|
|||||||
app_store
|
app_store
|
||||||
.save_meal_plan(id.as_str(), &meal_plan, chrono::Local::now().date_naive())
|
.save_meal_plan(id.as_str(), &meal_plan, chrono::Local::now().date_naive())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("{:?}", e))
|
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
api::EmptyResponse::Unauthorized
|
api::EmptyResponse::Unauthorized
|
||||||
@ -302,7 +257,6 @@ async fn api_inventory_v2(
|
|||||||
app_store
|
app_store
|
||||||
.fetch_latest_inventory_data(id)
|
.fetch_latest_inventory_data(id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("{:?}", e))
|
|
||||||
.map(|d| {
|
.map(|d| {
|
||||||
let data: api::InventoryData = d.into();
|
let data: api::InventoryData = d.into();
|
||||||
data
|
data
|
||||||
@ -322,7 +276,6 @@ async fn api_inventory(
|
|||||||
app_store
|
app_store
|
||||||
.fetch_latest_inventory_data(id)
|
.fetch_latest_inventory_data(id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("{:?}", e))
|
|
||||||
.map(|(filtered, modified, _)| (filtered, modified))
|
.map(|(filtered, modified, _)| (filtered, modified))
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
@ -340,7 +293,6 @@ async fn save_inventory_data(
|
|||||||
app_store
|
app_store
|
||||||
.save_inventory_data(id, filtered_ingredients, modified_amts, extra_items)
|
.save_inventory_data(id, filtered_ingredients, modified_amts, extra_items)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("{:?}", e))
|
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,16 +364,7 @@ async fn api_staples(
|
|||||||
) -> api::Response<Option<String>> {
|
) -> api::Response<Option<String>> {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(user_id)) = session {
|
if let FoundUserId(UserId(user_id)) = session {
|
||||||
match app_store.fetch_staples(user_id).await {
|
app_store.fetch_staples(user_id).await.into()
|
||||||
Ok(staples) => api::Response::success(staples),
|
|
||||||
Err(err) => {
|
|
||||||
error!(?err);
|
|
||||||
api::Response::error(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
|
|
||||||
format!("{:?}", err),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
api::Response::Unauthorized
|
api::Response::Unauthorized
|
||||||
}
|
}
|
||||||
@ -434,16 +377,7 @@ async fn api_save_staples(
|
|||||||
) -> api::Response<()> {
|
) -> api::Response<()> {
|
||||||
use storage::{UserId, UserIdFromSession::FoundUserId};
|
use storage::{UserId, UserIdFromSession::FoundUserId};
|
||||||
if let FoundUserId(UserId(user_id)) = session {
|
if let FoundUserId(UserId(user_id)) = session {
|
||||||
match app_store.save_staples(user_id, content).await {
|
app_store.save_staples(user_id, content).await.into()
|
||||||
Ok(_) => api::EmptyResponse::success(()),
|
|
||||||
Err(err) => {
|
|
||||||
error!(?err);
|
|
||||||
api::EmptyResponse::error(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
|
|
||||||
format!("{:?}", err),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
api::EmptyResponse::Unauthorized
|
api::EmptyResponse::Unauthorized
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user