Added ability to delete meals
This commit is contained in:
parent
f76eed3cde
commit
7c9c24189b
2 changed files with 36 additions and 5 deletions
12
main.py
12
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]:
|
||||
|
|
|
|||
29
meals/db.py
29
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)
|
||||
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,))
|
||||
Loading…
Reference in a new issue