Ingredients can now be remove from shopping list when a recipe is unscheduled.

This commit is contained in:
Greg Burri 2025-02-12 02:05:38 +01:00
parent a3f2b4a86a
commit da5ea57787
7 changed files with 92 additions and 36 deletions

View file

@ -795,7 +795,7 @@ VALUES ($1, $2)
recipe_id: i64,
date: NaiveDate,
servings: u32,
add_ingredients_element: bool,
add_ingredients_to_shopping_list: bool,
) -> Result<AddScheduledRecipeResult> {
let mut tx = self.tx().await?;
@ -823,7 +823,7 @@ VALUES ($1, $2, $3, $4)
}
Ok(insert_result) => {
if add_ingredients_element {
if add_ingredients_to_shopping_list {
sqlx::query(
r#"
INSERT INTO [ShoppingEntry] ([ingredient_id], [user_id], [recipe_scheduled_id], [servings])
@ -853,20 +853,42 @@ INSERT INTO [ShoppingEntry] ([ingredient_id], [user_id], [recipe_scheduled_id],
user_id: i64,
recipe_id: i64,
date: NaiveDate,
remove_ingredients_from_shopping_list: bool,
) -> Result<()> {
let mut tx = self.tx().await?;
if remove_ingredients_from_shopping_list {
sqlx::query(
r#"
DELETE FROM [ShoppingEntry]
WHERE [recipe_scheduled_id] IN (
SELECT [id] FROM [RecipeScheduled]
WHERE [user_id] = $1 AND [recipe_id] = $2 AND [date] = $3
)
"#,
)
.bind(user_id)
.bind(recipe_id)
.bind(date)
.execute(&mut *tx)
.await?;
}
sqlx::query(
r#"
DELETE FROM [RecipeScheduled]
WHERE [user_id] = $1 AND [recipe_id] = $2 AND [date] = $3
DELETE FROM [RecipeScheduled]
WHERE [user_id] = $1 AND [recipe_id] = $2 AND [date] = $3
"#,
)
.bind(user_id)
.bind(recipe_id)
.bind(date)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
.execute(&mut *tx)
.await?;
tx.commit().await?;
Ok(())
}
pub async fn get_scheduled_recipes(

View file

@ -681,12 +681,17 @@ pub async fn schedule_recipe(
pub async fn rm_scheduled_recipe(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::ScheduledRecipe>,
ExtractRon(ron): ExtractRon<common::ron_api::RemoveScheduledRecipe>,
) -> 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)
.rm_scheduled_recipe(
user.id,
ron.recipe_id,
ron.date,
ron.remove_ingredients_from_shopping_list,
)
.await?;
}
Ok(StatusCode::OK)

View file

@ -146,6 +146,7 @@ pub enum Sentence {
CalendarAddToPlannerAlreadyExists,
CalendarDateFormat, // See https://docs.rs/chrono/latest/chrono/format/strftime/index.html.
CalendarAddIngredientsToShoppingList,
CalendarRemoveIngredientsFromShoppingList,
CalendarUnscheduleConfirmation,
}