From 315626b3ed18259c2515b02e4c6545a082290376 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 31 Mar 2025 21:24:20 +0200 Subject: [PATCH] Remove useless verbs in web api urls --- backend/src/main.rs | 84 ++++++++++++++----------------- frontend/Cargo.toml | 9 ++-- frontend/src/calendar.rs | 3 +- frontend/src/lib.rs | 34 +++++++++---- frontend/src/pages/recipe_edit.rs | 56 ++++++++++----------- frontend/src/recipe_scheduler.rs | 4 +- frontend/src/shopping_list.rs | 4 +- 7 files changed, 100 insertions(+), 94 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 81d2ed3..9ddbbbd 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -131,113 +131,105 @@ async fn main() { // Disabled: update user profile is now made with a post data ('edit_user_post'). // .route("/user/update", put(services::ron::update_user)) .route("/set_lang", put(services::ron::set_lang)) - .route("/recipe/get_titles", get(services::ron::recipe::get_titles)) - .route("/recipe/set_title", patch(services::ron::recipe::set_title)) + .route("/recipe/titles", get(services::ron::recipe::get_titles)) + .route("/recipe/title", patch(services::ron::recipe::set_title)) .route( - "/recipe/set_description", + "/recipe/description", patch(services::ron::recipe::set_description), ) .route( - "/recipe/set_servings", + "/recipe/servings", patch(services::ron::recipe::set_servings), ) .route( - "/recipe/set_estimated_time", + "/recipe/estimated_time", patch(services::ron::recipe::set_estimated_time), ) - .route("/recipe/get_tags", get(services::ron::recipe::get_tags)) - .route("/recipe/add_tags", post(services::ron::recipe::add_tags)) - .route("/recipe/rm_tags", delete(services::ron::recipe::rm_tags)) .route( - "/recipe/set_difficulty", + "/recipe/tags", + get(services::ron::recipe::get_tags) + .post(services::ron::recipe::add_tags) + .delete(services::ron::recipe::rm_tags), + ) + .route( + "/recipe/difficulty", patch(services::ron::recipe::set_difficulty), ) .route( - "/recipe/set_language", + "/recipe/language", patch(services::ron::recipe::set_language), ) .route( - "/recipe/set_is_published", + "/recipe/is_published", patch(services::ron::recipe::set_is_published), ) - .route("/recipe/remove", delete(services::ron::recipe::rm)) - .route("/recipe/get_groups", get(services::ron::recipe::get_groups)) - .route("/recipe/add_group", post(services::ron::recipe::add_group)) + .route("/recipe", delete(services::ron::recipe::rm)) + .route("/recipe/groups", get(services::ron::recipe::get_groups)) .route( - "/recipe/remove_group", - delete(services::ron::recipe::rm_group), + "/recipe/group", + post(services::ron::recipe::add_group).delete(services::ron::recipe::rm_group), ) .route( - "/recipe/set_group_name", + "/recipe/group_name", patch(services::ron::recipe::set_group_name), ) .route( - "/recipe/set_group_comment", + "/recipe/group_comment", patch(services::ron::recipe::set_group_comment), ) .route( - "/recipe/set_groups_order", + "/recipe/groups_order", patch(services::ron::recipe::set_groups_order), ) - .route("/recipe/add_step", post(services::ron::recipe::add_step)) .route( - "/recipe/remove_step", - delete(services::ron::recipe::rm_step), + "/recipe/step", + post(services::ron::recipe::add_step).delete(services::ron::recipe::rm_step), ) .route( - "/recipe/set_step_action", + "/recipe/step_action", patch(services::ron::recipe::set_step_action), ) .route( - "/recipe/set_steps_order", + "/recipe/steps_order", patch(services::ron::recipe::set_steps_order), ) .route( - "/recipe/add_ingredient", - post(services::ron::recipe::add_ingredient), - ) - .route( - "/recipe/remove_ingredient", - delete(services::ron::recipe::rm_ingredient), + "/recipe/ingredient", + post(services::ron::recipe::add_ingredient) + .delete(services::ron::recipe::rm_ingredient), ) .route( - "/recipe/set_ingredient_name", + "/recipe/ingredient_name", patch(services::ron::recipe::set_ingredient_name), ) .route( - "/recipe/set_ingredient_comment", + "/recipe/ingredient_comment", patch(services::ron::recipe::set_ingredient_comment), ) .route( - "/recipe/set_ingredient_quantity", + "/recipe/ingredient_quantity", patch(services::ron::recipe::set_ingredient_quantity), ) .route( - "/recipe/set_ingredient_unit", + "/recipe/ingredient_unit", patch(services::ron::recipe::set_ingredient_unit), ) .route( - "/recipe/set_ingredients_order", + "/recipe/ingredients_order", patch(services::ron::recipe::set_ingredients_order), ) .route( - "/calendar/get_scheduled_recipes", + "/calendar/scheduled_recipes", get(services::ron::calendar::get_scheduled_recipes), ) .route( "/calendar/schedule_recipe", - post(services::ron::calendar::schedule_recipe), - ) - .route( - "/calendar/remove_scheduled_recipe", - delete(services::ron::calendar::rm_scheduled_recipe), - ) - .route( - "/shopping_list/get_list", - get(services::ron::shopping_list::get), + post(services::ron::calendar::schedule_recipe) + .delete(services::ron::calendar::rm_scheduled_recipe), ) + .route("/shopping_list", get(services::ron::shopping_list::get)) .route( - "/shopping_list/set_checked", + "/shopping_list/checked", patch(services::ron::shopping_list::set_entry_checked), ) .fallback(services::ron::not_found); diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 9636a9c..5dc37f6 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -24,8 +24,6 @@ futures = "0.3" scanf = "1.2" -wasm-bindgen = "0.2" -wasm-bindgen-futures = "0.4" web-sys = { version = "0.3", features = [ "console", "Document", @@ -40,6 +38,7 @@ web-sys = { version = "0.3", features = [ "KeyboardEvent", "Element", "DomStringMap", + "HtmlDocument", "HtmlElement", "HtmlDivElement", "HtmlLabelElement", @@ -49,10 +48,14 @@ web-sys = { version = "0.3", features = [ "HtmlDialogElement", ] } -gloo = "0.11" +gloo = { version = "0.11", features = ["futures"] } + +wasm-cookies = "0.2" # The `console_error_panic_hook` crate provides better debugging of panics by # logging them with `console.error`. This is great for development, but requires # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for # code size when deploying. console_error_panic_hook = { version = "0.1", optional = true } +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" diff --git a/frontend/src/calendar.rs b/frontend/src/calendar.rs index d50cf43..9b41be7 100644 --- a/frontend/src/calendar.rs +++ b/frontend/src/calendar.rs @@ -176,8 +176,7 @@ pub fn setup( date, remove_ingredients_from_shopping_list, }; - let _ = - request::delete::<(), _>("calendar/remove_scheduled_recipe", body).await; + let _ = request::delete::<(), _>("calendar/scheduled_recipe", body).await; window().location().reload().unwrap(); } }); diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 42d2e6e..034b586 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -83,20 +83,32 @@ pub fn main() -> Result<(), JsValue> { // Dark/light theme handling. let toggle_theme: HtmlInputElement = selector("#toggle-theme input"); EventListener::new(&toggle_theme.clone(), "change", move |_event| { - wasm_cookies::set( - common::consts::COOKIE_DARK_THEME, - &(!toggle_theme.checked()).to_string(), - &wasm_cookies::CookieOptions { - path: Some("/"), - domain: None, - expires: None, - secure: false, - same_site: wasm_cookies::SameSite::Strict, - }, - ); + set_cookie_dark_theme(!toggle_theme.checked()); window().location().reload().unwrap(); }) .forget(); Ok(()) } + +/// `wasm_cookies::set` is specific for the wasm32 target architecture and Rust Analyzer says +/// it's an error, it's not possible to configure different target configurations into the same +/// workspace. Here is the issue: +/// https://users.rust-lang.org/t/can-i-configure-rust-analyzer-vscode-to-use-a-different-target-for-different-crates-in-my-workspce/123661 +#[cfg(target_arch = "wasm32")] +fn set_cookie_dark_theme(dark_theme: bool) { + wasm_cookies::set( + common::consts::COOKIE_DARK_THEME, + &dark_theme.to_string(), + &wasm_cookies::CookieOptions { + path: Some("/"), + domain: None, + expires: None, + secure: false, + same_site: wasm_cookies::SameSite::Strict, + }, + ); +} + +#[cfg(not(target_arch = "wasm32"))] +fn set_cookie_dark_theme(_dark_theme: bool) {} diff --git a/frontend/src/pages/recipe_edit.rs b/frontend/src/pages/recipe_edit.rs index d1da43b..74dc219 100644 --- a/frontend/src/pages/recipe_edit.rs +++ b/frontend/src/pages/recipe_edit.rs @@ -35,7 +35,7 @@ pub fn setup_page(recipe_id: i64) { title: title.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_title", body).await; + let _ = request::patch::<(), _>("recipe/title", body).await; reload_recipes_list(recipe_id).await; }); } @@ -56,7 +56,7 @@ pub fn setup_page(recipe_id: i64) { description: description.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_description", body).await; + let _ = request::patch::<(), _>("recipe/description", body).await; }); } }) @@ -87,7 +87,7 @@ pub fn setup_page(recipe_id: i64) { servings, }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_servings", body).await; + let _ = request::patch::<(), _>("recipe/servings", body).await; }); } }) @@ -119,7 +119,7 @@ pub fn setup_page(recipe_id: i64) { estimated_time: time, }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_estimated_time", body).await; + let _ = request::patch::<(), _>("recipe/estimated_time", body).await; }); } }) @@ -143,7 +143,7 @@ pub fn setup_page(recipe_id: i64) { .unwrap(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_difficulty", body).await; + let _ = request::patch::<(), _>("recipe/difficulty", body).await; }); } }) @@ -154,7 +154,7 @@ pub fn setup_page(recipe_id: i64) { { spawn_local(async move { let tags: ron_api::Tags = request::get( - "recipe/get_tags", + "recipe/tags", ron_api::Id { id: recipe_id }, /*[("id", &recipe_id.to_string())]*/ ) .await @@ -171,7 +171,7 @@ pub fn setup_page(recipe_id: i64) { recipe_id, tags: tag_list.clone(), }; - let _ = request::post::<(), _>("recipe/add_tags", body).await; + let _ = request::post::<(), _>("recipe/tags", body).await; create_tag_elements(recipe_id, &tag_list); } by_id::("input-tags").set_value(""); @@ -219,7 +219,7 @@ pub fn setup_page(recipe_id: i64) { lang: language.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_language", body).await; + let _ = request::patch::<(), _>("recipe/language", body).await; }); } }) @@ -235,7 +235,7 @@ pub fn setup_page(recipe_id: i64) { is_published: is_published.checked(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_is_published", body).await; + let _ = request::patch::<(), _>("recipe/is_published", body).await; reload_recipes_list(recipe_id).await; }); }) @@ -261,7 +261,7 @@ pub fn setup_page(recipe_id: i64) { .is_some() { let body = ron_api::Id { id: recipe_id }; - let _ = request::delete::<(), _>("recipe/remove", body).await; + let _ = request::delete::<(), _>("recipe", body).await; window() .location() .set_href(&format!("/{}/", get_current_lang())) @@ -275,7 +275,7 @@ pub fn setup_page(recipe_id: i64) { { spawn_local(async move { let groups: Vec = - request::get("recipe/get_groups", ron_api::Id { id: recipe_id }) + request::get("recipe/groups", ron_api::Id { id: recipe_id }) .await .unwrap(); @@ -303,7 +303,7 @@ pub fn setup_page(recipe_id: i64) { EventListener::new(&button_add_group, "click", move |_event| { let body = ron_api::Id { id: recipe_id }; spawn_local(async move { - let response: ron_api::Id = request::post("recipe/add_group", body).await.unwrap(); + let response: ron_api::Id = request::post("recipe/group", body).await.unwrap(); create_group_element(&ron_api::Group { id: response.id, name: "".to_string(), @@ -333,7 +333,7 @@ fn create_group_element(group: &ron_api::Group) -> Element { .collect(); let body = ron_api::Ids { ids }; - let _ = request::patch::<(), _>("recipe/set_groups_order", body).await; + let _ = request::patch::<(), _>("recipe/groups_order", body).await; }); }); @@ -349,7 +349,7 @@ fn create_group_element(group: &ron_api::Group) -> Element { name: name.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_group_name", body).await; + let _ = request::patch::<(), _>("recipe/group_name", body).await; }) } }) @@ -367,7 +367,7 @@ fn create_group_element(group: &ron_api::Group) -> Element { comment: comment.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_group_comment", body).await; + let _ = request::patch::<(), _>("recipe/group_comment", body).await; }); } }) @@ -393,7 +393,7 @@ fn create_group_element(group: &ron_api::Group) -> Element { .is_some() { let body = ron_api::Id { id: group_id }; - let _ = request::delete::<(), _>("recipe/remove_group", body).await; + let _ = request::delete::<(), _>("recipe/group", body).await; let group_element = by_id::(&format!("group-{}", group_id)); group_element.next_element_sibling().unwrap().remove(); group_element.remove(); @@ -407,7 +407,7 @@ fn create_group_element(group: &ron_api::Group) -> Element { EventListener::new(&add_step_button, "click", move |_event| { spawn_local(async move { let body = ron_api::Id { id: group_id }; - let response: ron_api::Id = request::post("recipe/add_step", body).await.unwrap(); + let response: ron_api::Id = request::post("recipe/step", body).await.unwrap(); create_step_element( &selector::(&format!("#group-{} .steps", group_id)), &ron_api::Step { @@ -468,7 +468,7 @@ where recipe_id, tags: vec![tag], }; - let _ = request::delete::<(), _>("recipe/rm_tags", body).await; + let _ = request::delete::<(), _>("recipe/tags", body).await; tag_span.remove(); }); }) @@ -494,7 +494,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element .collect(); let body = ron_api::Ids { ids }; - let _ = request::patch::<(), _>("recipe/set_steps_order", body).await; + let _ = request::patch::<(), _>("recipe/steps_order", body).await; }); }); @@ -510,7 +510,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element action: action.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_step_action", body).await; + let _ = request::patch::<(), _>("recipe/step_action", body).await; }); } }) @@ -536,7 +536,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element .is_some() { let body = ron_api::Id { id: step_id }; - let _ = request::delete::<(), _>("recipe/remove_step", body).await; + let _ = request::delete::<(), _>("recipe/step", body).await; let step_element = by_id::(&format!("step-{}", step_id)); step_element.next_element_sibling().unwrap().remove(); step_element.remove(); @@ -550,7 +550,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element EventListener::new(&add_ingredient_button, "click", move |_event| { spawn_local(async move { let body = ron_api::Id { id: step_id }; - let response: ron_api::Id = request::post("recipe/add_ingredient", body).await.unwrap(); + let response: ron_api::Id = request::post("recipe/ingredient", body).await.unwrap(); create_ingredient_element( &selector::(&format!("#step-{} .ingredients", step_id)), &ron_api::Ingredient { @@ -586,7 +586,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre .collect(); let body = ron_api::Ids { ids }; - let _ = request::patch::<(), _>("recipe/set_ingredients_order", body).await; + let _ = request::patch::<(), _>("recipe/ingredients_order", body).await; }); }); @@ -602,7 +602,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre name: name.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_ingredient_name", body).await; + let _ = request::patch::<(), _>("recipe/ingredient_name", body).await; }); } }) @@ -620,7 +620,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre comment: comment.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_ingredient_comment", body).await; + let _ = request::patch::<(), _>("recipe/ingredient_comment", body).await; }); } }) @@ -647,7 +647,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre quantity: q, }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_ingredient_quantity", body).await; + let _ = request::patch::<(), _>("recipe/ingredient_quantity", body).await; }); } }) @@ -665,7 +665,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre unit: unit.value(), }; spawn_local(async move { - let _ = request::patch::<(), _>("recipe/set_ingredient_unit", body).await; + let _ = request::patch::<(), _>("recipe/ingredient_unit", body).await; }); } }) @@ -691,7 +691,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre .is_some() { let body = ron_api::Id { id: ingredient_id }; - let _ = request::delete::<(), _>("recipe/remove_ingredient", body).await; + let _ = request::delete::<(), _>("recipe/ingredient", body).await; let ingredient_element = by_id::(&format!("ingredient-{}", ingredient_id)); ingredient_element.next_element_sibling().unwrap().remove(); ingredient_element.remove(); diff --git a/frontend/src/recipe_scheduler.rs b/frontend/src/recipe_scheduler.rs index c089afb..2376092 100644 --- a/frontend/src/recipe_scheduler.rs +++ b/frontend/src/recipe_scheduler.rs @@ -81,7 +81,7 @@ impl RecipeScheduler { } let titles: ron_api::Strings = request::get( - "recipe/get_titles", + "recipe/titles", ron_api::Ids { ids: recipe_ids_and_dates .iter() @@ -98,7 +98,7 @@ impl RecipeScheduler { .collect::>()) } else { let scheduled_recipes: ron_api::ScheduledRecipes = request::get( - "calendar/get_scheduled_recipes", + "calendar/scheduled_recipes", ron_api::DateRange { start_date, end_date, diff --git a/frontend/src/shopping_list.rs b/frontend/src/shopping_list.rs index 1af9e7d..39f59c0 100644 --- a/frontend/src/shopping_list.rs +++ b/frontend/src/shopping_list.rs @@ -29,7 +29,7 @@ impl ShoppingList { if self.is_local { Ok(vec![]) // TODO } else { - Ok(request::get("shopping_list/get_list", ()).await?) + Ok(request::get("shopping_list", ()).await?) } } @@ -38,7 +38,7 @@ impl ShoppingList { todo!(); } else { request::patch( - "shopping_list/set_checked", + "shopping_list/checked", ron_api::Value { id: item_id, value: is_checked, -- 2.49.0