Allow deleting recipes

This commit is contained in:
jableader 2024-05-02 22:25:11 +10:00
parent 9f04dcaac3
commit 3113bacc11
2 changed files with 23 additions and 0 deletions

10
main.py
View file

@ -119,6 +119,16 @@ async def create_recipe(item: recipes.Recipe, conn: sqlite3.Connection = Depends
return item 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/") @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]: 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 = [] result = []

View file

@ -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.id, person.id)
self.assertEqual(recipe_by_id.created_by.name, person.name) 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 import meals
class TestMeals(unittest.IsolatedAsyncioTestCase): class TestMeals(unittest.IsolatedAsyncioTestCase):