Factorize some ron api types
authorGreg Burri <greg.burri@gmail.com>
Wed, 15 Jan 2025 23:43:19 +0000 (00:43 +0100)
committerGreg Burri <greg.burri@gmail.com>
Wed, 15 Jan 2025 23:43:19 +0000 (00:43 +0100)
backend/src/services/ron.rs
common/src/ron_api.rs
frontend/src/recipe_edit.rs

index ddf19b4..900152b 100644 (file)
@@ -346,10 +346,10 @@ pub async fn set_is_published(
 pub async fn rm(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::Remove>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
-    connection.rm_recipe(ron.recipe_id).await?;
+    check_user_rights_recipe(&connection, &user, ron.id).await?;
+    connection.rm_recipe(ron.id).await?;
     Ok(StatusCode::OK)
 }
 
@@ -415,25 +415,22 @@ pub async fn get_groups(
 pub async fn add_group(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeGroup>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
-    let group_id = connection.add_recipe_group(ron.recipe_id).await?;
+    check_user_rights_recipe(&connection, &user, ron.id).await?;
+    let id = connection.add_recipe_group(ron.id).await?;
 
-    Ok(ron_response(
-        StatusCode::OK,
-        common::ron_api::AddRecipeGroupResult { group_id },
-    ))
+    Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
 }
 
 #[debug_handler]
 pub async fn rm_group(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeGroup>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
-    connection.rm_recipe_group(ron.group_id).await?;
+    check_user_rights_recipe_group(&connection, &user, ron.id).await?;
+    connection.rm_recipe_group(ron.id).await?;
     Ok(StatusCode::OK)
 }
 
@@ -465,10 +462,10 @@ pub async fn set_group_comment(
 pub async fn set_groups_order(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::SetGroupOrders>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_groups(&connection, &user, &ron.group_ids).await?;
-    connection.set_groups_order(&ron.group_ids).await?;
+    check_user_rights_recipe_groups(&connection, &user, &ron.ids).await?;
+    connection.set_groups_order(&ron.ids).await?;
     Ok(StatusCode::OK)
 }
 
@@ -476,25 +473,22 @@ pub async fn set_groups_order(
 pub async fn add_step(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeStep>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
-    let step_id = connection.add_recipe_step(ron.group_id).await?;
+    check_user_rights_recipe_group(&connection, &user, ron.id).await?;
+    let id = connection.add_recipe_step(ron.id).await?;
 
-    Ok(ron_response(
-        StatusCode::OK,
-        common::ron_api::AddRecipeStepResult { step_id },
-    ))
+    Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
 }
 
 #[debug_handler]
 pub async fn rm_step(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeStep>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
-    connection.rm_recipe_step(ron.step_id).await?;
+    check_user_rights_recipe_step(&connection, &user, ron.id).await?;
+    connection.rm_recipe_step(ron.id).await?;
     Ok(StatusCode::OK)
 }
 
@@ -513,10 +507,10 @@ pub async fn set_step_action(
 pub async fn set_steps_order(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::SetStepOrders>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_steps(&connection, &user, &ron.step_ids).await?;
-    connection.set_steps_order(&ron.step_ids).await?;
+    check_user_rights_recipe_steps(&connection, &user, &ron.ids).await?;
+    connection.set_steps_order(&ron.ids).await?;
     Ok(StatusCode::OK)
 }
 
@@ -524,25 +518,22 @@ pub async fn set_steps_order(
 pub async fn add_ingredient(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeIngredient>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
-    let ingredient_id = connection.add_recipe_ingredient(ron.step_id).await?;
+    check_user_rights_recipe_step(&connection, &user, ron.id).await?;
+    let id = connection.add_recipe_ingredient(ron.id).await?;
 
-    Ok(ron_response(
-        StatusCode::OK,
-        common::ron_api::AddRecipeIngredientResult { ingredient_id },
-    ))
+    Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
 }
 
 #[debug_handler]
 pub async fn rm_ingredient(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeIngredient>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Id>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
-    connection.rm_recipe_ingredient(ron.ingredient_id).await?;
+    check_user_rights_recipe_ingredient(&connection, &user, ron.id).await?;
+    connection.rm_recipe_ingredient(ron.id).await?;
     Ok(StatusCode::OK)
 }
 
@@ -602,12 +593,10 @@ pub async fn set_ingredient_unit(
 pub async fn set_ingredients_order(
     State(connection): State<db::Connection>,
     Extension(user): Extension<Option<model::User>>,
-    ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientOrders>,
+    ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
 ) -> Result<impl IntoResponse> {
-    check_user_rights_recipe_ingredients(&connection, &user, &ron.ingredient_ids).await?;
-    connection
-        .set_ingredients_order(&ron.ingredient_ids)
-        .await?;
+    check_user_rights_recipe_ingredients(&connection, &user, &ron.ids).await?;
+    connection.set_ingredients_order(&ron.ids).await?;
     Ok(StatusCode::OK)
 }
 
index f3a6b1a..a088220 100644 (file)
@@ -6,6 +6,16 @@ pub struct SetLang {
     pub lang: String,
 }
 
+#[derive(Serialize, Deserialize, Clone)]
+pub struct Ids {
+    pub ids: Vec<i64>,
+}
+
+#[derive(Serialize, Deserialize, Clone)]
+pub struct Id {
+    pub id: i64,
+}
+
 ///// RECIPE /////
 
 #[derive(Serialize, Deserialize, Clone)]
@@ -76,26 +86,6 @@ pub struct SetIsPublished {
     pub is_published: bool,
 }
 
-#[derive(Serialize, Deserialize, Clone)]
-pub struct Remove {
-    pub recipe_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeGroup {
-    pub recipe_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeGroupResult {
-    pub group_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct RemoveRecipeGroup {
-    pub group_id: i64,
-}
-
 #[derive(Serialize, Deserialize, Clone)]
 pub struct SetGroupName {
     pub group_id: i64,
@@ -108,52 +98,12 @@ pub struct SetGroupComment {
     pub comment: String,
 }
 
-#[derive(Serialize, Deserialize, Clone)]
-pub struct SetGroupOrders {
-    pub group_ids: Vec<i64>,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeStep {
-    pub group_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeStepResult {
-    pub step_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct RemoveRecipeStep {
-    pub step_id: i64,
-}
-
 #[derive(Serialize, Deserialize, Clone)]
 pub struct SetStepAction {
     pub step_id: i64,
     pub action: String,
 }
 
-#[derive(Serialize, Deserialize, Clone)]
-pub struct SetStepOrders {
-    pub step_ids: Vec<i64>,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeIngredient {
-    pub step_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct AddRecipeIngredientResult {
-    pub ingredient_id: i64,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct RemoveRecipeIngredient {
-    pub ingredient_id: i64,
-}
-
 #[derive(Serialize, Deserialize, Clone)]
 pub struct SetIngredientName {
     pub ingredient_id: i64,
@@ -178,11 +128,6 @@ pub struct SetIngredientUnit {
     pub unit: String,
 }
 
-#[derive(Serialize, Deserialize, Clone)]
-pub struct SetIngredientOrders {
-    pub ingredient_ids: Vec<i64>,
-}
-
 #[derive(Serialize, Deserialize, Clone)]
 pub struct Tags {
     pub recipe_id: i64,
@@ -213,86 +158,6 @@ pub struct Ingredient {
     pub quantity_unit: String,
 }
 
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeImage {
-//     pub recipe_id: i64,
-//     pub image: Vec<u8>,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeImageReply {
-//     pub image_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct RemoveRecipeImage {
-//     pub image_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeIngredient {
-//     pub group_id: i64,
-//     pub name: String,
-//     pub quantity_value: Option<f64>,
-//     pub quantity_unit: String,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeIngredientReply {
-//     pub ingredient_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct RemoveRecipeIngredient {
-//     pub group_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct SetRecipeIngredientsOrder {
-//     pub group_id: i64,
-//     pub ingredient_ids: Vec<i64>,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeGroup {
-//     pub recipe_id: i64,
-//     pub name: String,
-//     pub quantity_value: Option<f64>,
-//     pub quantity_unit: String,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeGroupReply {
-//     pub group_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct RemoveRecipeGroupReply {
-//     pub group_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct SetRecipeGroupsOrder {
-//     pub recipe_id: i64,
-//     pub group_ids: Vec<i64>,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeStep {
-//     pub group_id: i64,
-//     pub name: String,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct AddRecipeStepReply {
-//     pub step_id: i64,
-// }
-
-// #[derive(Serialize, Deserialize, Clone)]
-// pub struct RemoveRecipeStep {
-//     pub step_id: i64,
-// }
-
 ///// PROFILE /////
 
 #[derive(Serialize, Deserialize, Clone)]
index e605028..de4b26f 100644 (file)
@@ -273,7 +273,7 @@ pub fn setup_page(recipe_id: i64) -> Result<(), JsValue> {
             ))
             .await
             {
-                let body = ron_api::Remove { recipe_id };
+                let body = ron_api::Id { id: recipe_id };
                 let _ = request::delete::<(), _>("recipe/remove", body).await;
                 window().location().set_href("/").unwrap();
 
@@ -316,12 +316,11 @@ pub fn setup_page(recipe_id: i64) -> Result<(), JsValue> {
     {
         let button_add_group: HtmlInputElement = by_id("input-add-group");
         let on_click_add_group = EventListener::new(&button_add_group, "click", move |_event| {
-            let body = ron_api::AddRecipeGroup { recipe_id };
+            let body = ron_api::Id { id: recipe_id };
             spawn_local(async move {
-                let response: ron_api::AddRecipeGroupResult =
-                    request::post("recipe/add_group", body).await.unwrap();
+                let response: ron_api::Id = request::post("recipe/add_group", body).await.unwrap();
                 create_group_element(&ron_api::Group {
-                    id: response.group_id,
+                    id: response.id,
                     name: "".to_string(),
                     comment: "".to_string(),
                     steps: vec![],
@@ -344,13 +343,13 @@ fn create_group_element(group: &ron_api::Group) -> Element {
 
     set_draggable(&group_element, "group", |_element| {
         spawn_local(async move {
-            let group_ids = by_id::<Element>("groups-container")
+            let ids = by_id::<Element>("groups-container")
                 .selector_all::<Element>(".group")
                 .into_iter()
                 .map(|e| e.id()[6..].parse::<i64>().unwrap())
                 .collect();
 
-            let body = ron_api::SetGroupOrders { group_ids };
+            let body = ron_api::Ids { ids };
             let _ = request::put::<(), _>("recipe/set_groups_order", body).await;
         });
     });
@@ -400,7 +399,7 @@ fn create_group_element(group: &ron_api::Group) -> Element {
             .value();
         spawn_local(async move {
             if modal_dialog::show(&format!("Are you sure to delete the group '{}'", name)).await {
-                let body = ron_api::RemoveRecipeGroup { group_id };
+                let body = ron_api::Id { id: group_id };
                 let _ = request::delete::<(), _>("recipe/remove_group", body).await;
                 let group_element = by_id::<Element>(&format!("group-{}", group_id));
                 group_element.next_element_sibling().unwrap().remove();
@@ -414,13 +413,12 @@ fn create_group_element(group: &ron_api::Group) -> Element {
     let add_step_button: HtmlInputElement = group_element.selector(".input-add-step");
     EventListener::new(&add_step_button, "click", move |_event| {
         spawn_local(async move {
-            let body = ron_api::AddRecipeStep { group_id };
-            let response: ron_api::AddRecipeStepResult =
-                request::post("recipe/add_step", body).await.unwrap();
+            let body = ron_api::Id { id: group_id };
+            let response: ron_api::Id = request::post("recipe/add_step", body).await.unwrap();
             create_step_element(
                 &selector::<Element>(&format!("#group-{} .steps", group_id)),
                 &ron_api::Step {
-                    id: response.step_id,
+                    id: response.id,
                     action: "".to_string(),
                     ingredients: vec![],
                 },
@@ -494,7 +492,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
     set_draggable(&step_element, "step", |element| {
         let element = element.clone();
         spawn_local(async move {
-            let step_ids = element
+            let ids = element
                 .parent_element()
                 .unwrap()
                 .selector_all::<Element>(".step")
@@ -502,7 +500,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
                 .map(|e| e.id()[5..].parse::<i64>().unwrap())
                 .collect();
 
-            let body = ron_api::SetStepOrders { step_ids };
+            let body = ron_api::Ids { ids };
             let _ = request::put::<(), _>("recipe/set_steps_order", body).await;
         });
     });
@@ -534,7 +532,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
             .value();
         spawn_local(async move {
             if modal_dialog::show(&format!("Are you sure to delete the step '{}'", action)).await {
-                let body = ron_api::RemoveRecipeStep { step_id };
+                let body = ron_api::Id { id: step_id };
                 let _ = request::delete::<(), _>("recipe/remove_step", body).await;
                 let step_element = by_id::<Element>(&format!("step-{}", step_id));
                 step_element.next_element_sibling().unwrap().remove();
@@ -548,13 +546,12 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
     let add_ingredient_button: HtmlInputElement = step_element.selector(".input-add-ingredient");
     EventListener::new(&add_ingredient_button, "click", move |_event| {
         spawn_local(async move {
-            let body = ron_api::AddRecipeIngredient { step_id };
-            let response: ron_api::AddRecipeIngredientResult =
-                request::post("recipe/add_ingredient", body).await.unwrap();
+            let body = ron_api::Id { id: step_id };
+            let response: ron_api::Id = request::post("recipe/add_ingredient", body).await.unwrap();
             create_ingredient_element(
                 &selector::<Element>(&format!("#step-{} .ingredients", step_id)),
                 &ron_api::Ingredient {
-                    id: response.ingredient_id,
+                    id: response.id,
                     name: "".to_string(),
                     comment: "".to_string(),
                     quantity_value: None,
@@ -577,7 +574,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
     set_draggable(&ingredient_element, "ingredient", |element| {
         let element = element.clone();
         spawn_local(async move {
-            let ingredient_ids = element
+            let ids = element
                 .parent_element()
                 .unwrap()
                 .selector_all::<Element>(".ingredient")
@@ -585,7 +582,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
                 .map(|e| e.id()[11..].parse::<i64>().unwrap())
                 .collect();
 
-            let body = ron_api::SetIngredientOrders { ingredient_ids };
+            let body = ron_api::Ids { ids };
             let _ = request::put::<(), _>("recipe/set_ingredients_order", body).await;
         });
     });
@@ -682,7 +679,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
             if modal_dialog::show(&format!("Are you sure to delete the ingredient '{}'", name))
                 .await
             {
-                let body = ron_api::RemoveRecipeIngredient { ingredient_id };
+                let body = ron_api::Id { id: ingredient_id };
                 let _ = request::delete::<(), _>("recipe/remove_ingredient", body).await;
                 by_id::<Element>(&format!("ingredient-{}", ingredient_id)).remove();
             }