Recipe can now be scheduled

This commit is contained in:
Greg Burri 2025-02-04 22:29:56 +01:00
parent ae6da1a5ae
commit fbef990022
18 changed files with 233 additions and 51 deletions

View file

@ -752,7 +752,7 @@ VALUES ($1, $2)
Ok(())
}
pub async fn add_schedule_recipe(
pub async fn add_scheduled_recipe(
&self,
user_id: i64,
recipe_id: i64,
@ -775,7 +775,7 @@ VALUES ($1, $2, $3, $4)
.map_err(DBError::from)
}
pub async fn remove_scheduled_recipe(
pub async fn rm_scheduled_recipe(
&self,
user_id: i64,
recipe_id: i64,
@ -964,13 +964,13 @@ VALUES
let tomorrow = today + Days::new(1);
connection
.add_schedule_recipe(user_id, recipe_id_1, today, 4)
.add_scheduled_recipe(user_id, recipe_id_1, today, 4)
.await?;
connection
.add_schedule_recipe(user_id, recipe_id_2, yesterday, 4)
.add_scheduled_recipe(user_id, recipe_id_2, yesterday, 4)
.await?;
connection
.add_schedule_recipe(user_id, recipe_id_1, tomorrow, 4)
.add_scheduled_recipe(user_id, recipe_id_1, tomorrow, 4)
.await?;
assert_eq!(
@ -1008,13 +1008,13 @@ VALUES
);
connection
.remove_scheduled_recipe(user_id, recipe_id_1, today)
.rm_scheduled_recipe(user_id, recipe_id_1, today)
.await?;
connection
.remove_scheduled_recipe(user_id, recipe_id_2, yesterday)
.rm_scheduled_recipe(user_id, recipe_id_2, yesterday)
.await?;
connection
.remove_scheduled_recipe(user_id, recipe_id_1, tomorrow)
.rm_scheduled_recipe(user_id, recipe_id_1, tomorrow)
.await?;
assert_eq!(

View file

@ -181,6 +181,14 @@ async fn main() {
"/calendar/get_scheduled_recipes",
get(services::ron::get_scheduled_recipes),
)
.route(
"/calendar/schedule_recipe",
post(services::ron::schedule_recipe),
)
.route(
"/calendar/remove_scheduled_recipe",
delete(services::ron::rm_scheduled_recipe),
)
.fallback(services::ron::not_found);
let fragments_routes = Router::new().route(

View file

@ -631,6 +631,36 @@ pub async fn get_scheduled_recipes(
}
}
#[debug_handler]
pub async fn schedule_recipe(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::ScheduleRecipe>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
if let Some(user) = user {
connection
.add_scheduled_recipe(user.id, ron.recipe_id, ron.date, ron.servings)
.await?;
}
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn rm_scheduled_recipe(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::ScheduledRecipe>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
if let Some(user) = user {
connection
.rm_scheduled_recipe(user.id, ron.recipe_id, ron.date)
.await?;
}
Ok(StatusCode::OK)
}
/// 404 ///
#[debug_handler]

View file

@ -141,6 +141,9 @@ pub enum Sentence {
CalendarOctober,
CalendarNovember,
CalendarDecember,
CalendarAddToPlanner,
CalendarAddToPlannerSuccess,
CalendarDateFormat, // See https://docs.rs/chrono/latest/chrono/format/strftime/index.html.
}
pub const DEFAULT_LANGUAGE_CODE: &str = "en";
@ -181,6 +184,10 @@ impl Tr {
pub fn current_lang_code(&self) -> &str {
&self.lang.code
}
pub fn current_lang_and_territory_code(&self) -> String {
format!("{}-{}", self.lang.code, self.lang.territory)
}
}
// #[macro_export]
@ -200,6 +207,7 @@ impl Tr {
#[derive(Debug, Deserialize)]
struct StoredLanguage {
code: String,
territory: String,
name: String,
translation: Vec<(Sentence, String)>,
}
@ -207,6 +215,7 @@ struct StoredLanguage {
#[derive(Debug)]
struct Language {
code: String,
territory: String,
name: String,
translation: Vec<String>,
}
@ -215,6 +224,7 @@ impl Language {
pub fn from_stored_language(stored_language: StoredLanguage) -> Self {
Self {
code: stored_language.code,
territory: stored_language.territory,
name: stored_language.name,
translation: {
let mut translation = vec![String::new(); Sentence::COUNT];