From 898799113989dde6d45af98255a78b345699e9ba Mon Sep 17 00:00:00 2001 From: traxys Date: Thu, 29 Jun 2023 17:35:35 +0200 Subject: [PATCH] app,server: Allow to set the person count when creating a recipe --- api/src/lib.rs | 7 +++++++ app/src/recipe_creator.rs | 1 + src/routes/mod.rs | 4 ++++ src/routes/recipe.rs | 20 +++++++++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/api/src/lib.rs b/api/src/lib.rs index 30fdfc9..b9d8048 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -86,6 +86,7 @@ pub struct EditIngredientRequest { #[derive(Serialize, Deserialize, Clone, Debug)] pub struct CreateRecipeRequest { + pub person_count: u32, pub name: String, pub rating: u8, pub ingredients: Vec<(i64, f64)>, @@ -106,6 +107,7 @@ pub struct ListRecipesResponse { pub struct RecipeInfo { pub name: String, pub rating: u8, + pub person_count: u32, pub steps: String, pub ingredients: Vec<(i64, IngredientInfo, f64)>, } @@ -134,3 +136,8 @@ pub struct RecipeEditStepsRequest { pub struct RecipeEditRating { pub rating: u8, } + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct RecipeEditPersonCount { + pub person_count: u32, +} diff --git a/app/src/recipe_creator.rs b/app/src/recipe_creator.rs index fef8124..0febd41 100644 --- a/app/src/recipe_creator.rs +++ b/app/src/recipe_creator.rs @@ -357,6 +357,7 @@ pub fn RecipeCreator() -> Html { .iter() .map(|rcp_ig: &RecipeIngredient| (rcp_ig.id, rcp_ig.amount / (*pc) as f64)) .collect(), + person_count: (*pc) as _, steps: (*s).clone(), }, ) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index c983bd9..e42d9f3 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -236,6 +236,10 @@ pub(crate) fn router(api_allowed: Option) -> Router { "/household/:house_id/recipe/:recipe_id/rating", patch(recipe::edit_rating).layer(mk_service(vec![Method::PATCH])), ) + .route( + "/household/:house_id/recipe/:recipe_id/person_count", + patch(recipe::edit_person_count).layer(mk_service(vec![Method::PATCH])), + ) .route( "/household/:house_id/recipe/:recipe_id/ingredients/:iid", patch(recipe::edit_ig_amount) diff --git a/src/routes/recipe.rs b/src/routes/recipe.rs index d2593e9..1187b43 100644 --- a/src/routes/recipe.rs +++ b/src/routes/recipe.rs @@ -1,7 +1,7 @@ use api::{ AddRecipeIngredientRequest, CreateRecipeRequest, CreateRecipeResponse, EmptyResponse, IngredientInfo, ListRecipesResponse, RecipeEditRating, RecipeEditStepsRequest, RecipeInfo, - RecipeIngredientEditRequest, RecipeRenameRequest, + RecipeIngredientEditRequest, RecipeRenameRequest, RecipeEditPersonCount, }; use axum::{ async_trait, @@ -32,6 +32,7 @@ pub(super) async fn create_recipe( ranking: ActiveValue::Set(request.rating as i32), household: ActiveValue::Set(household.id), steps: ActiveValue::Set(request.steps), + person_count: ActiveValue::Set(request.person_count.min(1) as i32), ..Default::default() }; @@ -142,6 +143,7 @@ pub(super) async fn fetch_recipe( } Ok(RecipeInfo { + person_count: recipe.person_count as _, name: recipe.name, steps: recipe.steps, rating: recipe.ranking as _, @@ -243,3 +245,19 @@ pub(super) async fn edit_rating( Ok(EmptyResponse {}.into()) } + +pub(super) async fn edit_person_count( + State(state): State, + RecipeExtractor(recipe): RecipeExtractor, + Json(req): Json, +) -> JsonResult { + let model = recipe::ActiveModel { + id: ActiveValue::Set(recipe.id), + person_count: ActiveValue::Set(req.person_count.min(1) as _), + ..Default::default() + }; + + model.update(&state.db).await?; + + Ok(EmptyResponse {}.into()) +}