From 7c9c24189bfd005b05c08ef078014e2439dbedfb Mon Sep 17 00:00:00 2001 From: jableader Date: Wed, 17 Jan 2024 22:43:16 +1100 Subject: [PATCH] Added ability to delete meals --- main.py | 12 +++++++++++- meals/db.py | 29 +++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 65ade11..d1c0ffd 100644 --- a/main.py +++ b/main.py @@ -112,7 +112,8 @@ async def create_recipe(item: recipes.Recipe, conn: sqlite3.Connection = Depends async def get_meals(date_from: Annotated[datetime.datetime, Query(alias='from')], to: datetime.datetime, conn: sqlite3.Connection = Depends(get_db)) -> List[meals.Meal]: result = [] for meal in await meals.find_meals_by_date_range(conn, date_from, to): - meals.load_recipes(conn, meal) + await meals.load_recipes(conn, meal) + await meals.load_extra_ingredients(conn, meal) result.append(meal) return result @@ -149,6 +150,15 @@ async def create_meal(meal: meals.Meal, conn: sqlite3.Connection = Depends(get_d await conn.commit() return meal +@app.delete("/meals/{meal_id}") +async def delete_meal(meal_id: int, conn: sqlite3.Connection = Depends(get_db)) -> meals.Meal: + meal = await meals.find_meal_by_id(conn, meal_id) + if not meal: + return JSONResponse(status_code=404, content={'message': 'Meal not found'}) + + await meals.delete_meal(conn, meal.id) + await conn.commit() + return meal @app.get("/persons/") async def get_persons(conn: sqlite3.Connection = Depends(get_db)) -> List[meals.Person]: diff --git a/meals/db.py b/meals/db.py index 83b6066..59aa983 100644 --- a/meals/db.py +++ b/meals/db.py @@ -11,7 +11,7 @@ class Meal(BaseModel): KEYS: ClassVar[List[str]] = ['id', 'date'] id: int date: datetime.datetime - chef: List[Person] = [] + chefs: List[Person] = [] cleanup: List[Person] = [] consumers: List[Person] = [] recipes: List[Recipe] = [] @@ -60,7 +60,7 @@ async def insert_meal(conn, meal: Meal): ''', (meal.date,)) as cursor: meal.id = cursor.lastrowid - for person in meal.chef: + for person in meal.chefs: await insert_meal_participant(conn, meal.id, person.id, 'chef') for person in meal.cleanup: @@ -112,7 +112,7 @@ async def load_participants(conn, meal: Meal) -> None: async for row in cursor: person = await Person.find_person_by_id(conn, row[0]) if row[1] == 'chef': - meal.chef.append(person) + meal.chefs.append(person) elif row[1] == 'cleanup': meal.cleanup.append(person) elif row[1] == 'consumer': @@ -141,4 +141,25 @@ async def load_extra_ingredients(conn, meal: Meal) -> None: async for row in cursor: ingredient = Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row[:len(Ingredient.KEYS)])}) ingredient.product = Product(**{k:v for k,v in zip(Product.KEYS, row[len(Ingredient.KEYS):])}) - meal.extra_ingredients.append(ingredient) \ No newline at end of file + meal.extra_ingredients.append(ingredient) + +async def delete_meal(conn, meal_id: int) -> None: + await conn.execute(''' + DELETE FROM MealParticipant + WHERE meal_id = ? + ''', (meal_id,)) + + await conn.execute(''' + DELETE FROM MealRecipe + WHERE meal_id = ? + ''', (meal_id,)) + + await conn.execute(''' + DELETE FROM Ingredient + WHERE meal_id = ? + ''', (meal_id,)) + + await conn.execute(''' + DELETE FROM Meal + WHERE id = ? + ''', (meal_id,)) \ No newline at end of file