From 3113bacc1196cd554de5bbe3904ebc6a4f29fe29 Mon Sep 17 00:00:00 2001 From: jableader Date: Thu, 2 May 2024 22:25:11 +1000 Subject: [PATCH] Allow deleting recipes --- main.py | 10 ++++++++++ tests.py | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/main.py b/main.py index fc5edd0..9ffffa0 100644 --- a/main.py +++ b/main.py @@ -119,6 +119,16 @@ async def create_recipe(item: recipes.Recipe, conn: sqlite3.Connection = Depends return item +@app.delete('/recipes/{recipe_id}') +async def delete_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_db), user: persons.Person = Depends(cookie_person)) -> recipes.Recipe: + recipe = await recipes.find_recipe_by_id(conn, recipe_id) + if not recipe: + return JSONResponse(status_code=404, content={'message': 'Recipe not found'}) + + await recipes.hide_recipe(conn, recipe_id, user) + await conn.commit() + return recipe + @app.get("/meals/") 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 = [] diff --git a/tests.py b/tests.py index 3689d73..342b572 100644 --- a/tests.py +++ b/tests.py @@ -92,6 +92,19 @@ class TestRecipe(unittest.IsolatedAsyncioTestCase): self.assertEqual(recipe_by_id.created_by.id, person.id) self.assertEqual(recipe_by_id.created_by.name, person.name) + all_recipes = await main.get_recipes(None, self.conn) + self.assertIsNotNone(all_recipes) + self.assertIsInstance(all_recipes, list, msg=all_recipes.body if hasattr(all_recipes, 'body') else all_recipes) + self.assertEqual(len(all_recipes), 1) + self.assertEqual(all_recipes[0].id, recipe.id) + + await main.delete_recipe(recipe.id, self.conn, test_data.Persons.jacob) + + all_recipes = await main.get_recipes(None, self.conn) + self.assertIsNotNone(all_recipes) + self.assertIsInstance(all_recipes, list, msg=all_recipes.body if hasattr(all_recipes, 'body') else all_recipes) + self.assertEqual(len(all_recipes), 0) + import meals class TestMeals(unittest.IsolatedAsyncioTestCase):