Add a RON API to search recipes by title + a bit of refactoring

This commit is contained in:
Greg Burri 2025-05-21 19:58:31 +02:00
parent a3f61e3711
commit 084f7ef445
26 changed files with 499 additions and 333 deletions

View file

@ -1,16 +1,16 @@
use chrono::{Datelike, Days, Months, NaiveDate};
use common::ron_api;
use common::web_api;
use gloo::storage::{LocalStorage, Storage};
use ron::ser::{PrettyConfig, to_string_pretty};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{calendar, request};
use crate::{calendar, ron_request};
#[derive(Error, Debug)]
pub enum Error {
#[error("Request error: {0}")]
Request(#[from] request::Error),
Request(#[from] ron_request::Error),
}
type Result<T> = std::result::Result<T, Error>;
@ -25,11 +25,11 @@ pub enum ScheduleRecipeResult {
RecipeAlreadyScheduledAtThisDate,
}
impl From<ron_api::ScheduleRecipeResult> for ScheduleRecipeResult {
fn from(api_res: ron_api::ScheduleRecipeResult) -> Self {
impl From<web_api::ScheduleRecipeResult> for ScheduleRecipeResult {
fn from(api_res: web_api::ScheduleRecipeResult) -> Self {
match api_res {
ron_api::ScheduleRecipeResult::Ok => Self::Ok,
ron_api::ScheduleRecipeResult::RecipeAlreadyScheduledAtThisDate => {
web_api::ScheduleRecipeResult::Ok => Self::Ok,
web_api::ScheduleRecipeResult::RecipeAlreadyScheduledAtThisDate => {
Self::RecipeAlreadyScheduledAtThisDate
}
}
@ -80,12 +80,14 @@ impl RecipeScheduler {
return Ok(vec![]);
}
let titles: Vec<String> = request::get_with_params(
"recipe/titles",
recipe_ids_and_dates
.iter()
.map(|r| r.recipe_id)
.collect::<Vec<_>>(),
let titles: Vec<String> = ron_request::get_with_params(
"/ron-api/recipe/titles",
web_api::GetTitlesParams {
ids: recipe_ids_and_dates
.iter()
.map(|r| r.recipe_id)
.collect::<Vec<_>>(),
},
)
.await?;
@ -95,9 +97,9 @@ impl RecipeScheduler {
.map(|(id_and_date, title)| (id_and_date.date, title, id_and_date.recipe_id))
.collect::<Vec<_>>())
} else {
let scheduled_recipes: ron_api::ScheduledRecipes = request::get_with_params(
"calendar/scheduled_recipes",
ron_api::DateRange {
let scheduled_recipes: web_api::ScheduledRecipes = ron_request::get_with_params(
"/ron-api/calendar/scheduled_recipes",
web_api::DateRange {
start_date,
end_date,
},
@ -127,9 +129,9 @@ impl RecipeScheduler {
save_scheduled_recipes(recipe_ids_and_dates, date.year(), date.month0());
Ok(ScheduleRecipeResult::Ok)
} else {
request::post::<ron_api::ScheduleRecipeResult, _>(
"calendar/scheduled_recipe",
Some(ron_api::ScheduleRecipe {
ron_request::post::<web_api::ScheduleRecipeResult, _>(
"/ron-api/calendar/scheduled_recipe",
Some(web_api::ScheduleRecipe {
recipe_id,
date,
servings,
@ -138,7 +140,7 @@ impl RecipeScheduler {
)
.await
.map_err(Error::from)
.map(From::<ron_api::ScheduleRecipeResult>::from)
.map(From::<web_api::ScheduleRecipeResult>::from)
}
}