Ingredients can now be added to shopping list when a recipe is scheduled

This commit is contained in:
Greg Burri 2025-02-10 15:02:20 +01:00
parent 15173c4842
commit ce3821b94e
12 changed files with 98 additions and 39 deletions

View file

@ -795,7 +795,10 @@ VALUES ($1, $2)
recipe_id: i64,
date: NaiveDate,
servings: u32,
add_ingredients_element: bool,
) -> Result<AddScheduledRecipeResult> {
let mut tx = self.tx().await?;
match sqlx::query(
r#"
INSERT INTO [RecipeScheduled] (user_id, recipe_id, date, servings)
@ -806,7 +809,7 @@ VALUES ($1, $2, $3, $4)
.bind(recipe_id)
.bind(date)
.bind(servings)
.execute(&self.pool)
.execute(&mut *tx)
.await
{
Err(Error::Database(error))
@ -815,7 +818,33 @@ VALUES ($1, $2, $3, $4)
{
Ok(AddScheduledRecipeResult::RecipeAlreadyScheduledAtThisDate)
}
_ => Ok(AddScheduledRecipeResult::Ok),
Err(error) => {
Err(DBError::from(error))
}
Ok(insert_result) => {
if add_ingredients_element {
sqlx::query(
r#"
INSERT INTO [ShoppingEntry] ([ingredient_id], [user_id], [recipe_scheduled_id], [servings])
SELECT [Ingredient].[id], $2, $3, $4 FROM [Ingredient]
INNER JOIN [Step] ON [Step].[id] = [Ingredient].[step_id]
INNER JOIN [Group] ON [Group].[id] = [Step].[group_id]
INNER JOIN [Recipe] ON [Recipe].[id] = [Group].[recipe_id]
WHERE [Recipe].[id] = $1
"#)
.bind(recipe_id)
.bind(user_id)
.bind(insert_result.last_insert_rowid())
.bind(servings)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
Ok(AddScheduledRecipeResult::Ok)
}
}
}
@ -1009,13 +1038,13 @@ VALUES
let tomorrow = today + Days::new(1);
connection
.add_scheduled_recipe(user_id, recipe_id_1, today, 4)
.add_scheduled_recipe(user_id, recipe_id_1, today, 4, false)
.await?;
connection
.add_scheduled_recipe(user_id, recipe_id_2, yesterday, 4)
.add_scheduled_recipe(user_id, recipe_id_2, yesterday, 4, false)
.await?;
connection
.add_scheduled_recipe(user_id, recipe_id_1, tomorrow, 4)
.add_scheduled_recipe(user_id, recipe_id_1, tomorrow, 4, false)
.await?;
assert_eq!(
@ -1054,7 +1083,7 @@ VALUES
// Recipe scheduled at the same date is forbidden.
let Ok(AddScheduledRecipeResult::RecipeAlreadyScheduledAtThisDate) = connection
.add_scheduled_recipe(user_id, recipe_id_1, today, 4)
.add_scheduled_recipe(user_id, recipe_id_1, today, 4, false)
.await
else {
panic!("DBError::RecipeAlreadyScheduledAtThisDate must be returned");