munch-ease-backend/meals/db.py

226 lines
No EOL
7.7 KiB
Python

from typing import AsyncIterator, List, ClassVar, Optional
from pydantic import BaseModel
from ingredients import Ingredient, insert_ingredient, find_ingredients_by_meal_id, delete_ingredients_by_meal_id
from recipes import Recipe, row_to_recipe, load_recipe_ingredients
import persons
from persons import Person
import datetime
class MealRecipe(BaseModel):
meal_id: int
recipe_id: int
servings: float
recipe: Optional[Recipe] = None
class Meal(BaseModel):
KEYS: ClassVar[List[str]] = ['id', 'suggested_date', 'consumed_date', 'purchase_date']
id: int = -1
suggested_date: datetime.datetime
consumed_date: Optional[datetime.datetime] = None
chefs: List[Person] = []
cleanup: List[Person] = []
consumers: List[Person] = []
recipes: List[MealRecipe] = []
extra_ingredients: List[Ingredient] = []
# Set from shopping list
purchase_date: Optional[datetime.datetime] = None
async def create(conn):
await conn.execute('''
CREATE TABLE IF NOT EXISTS Meal (
id INTEGER PRIMARY KEY,
suggested_date DATETIME,
consumed_date DATETIME DEFAULT NULL,
deleted_date TEXT DEFAULT NULL,
purchase_date TEXT DEFAULT NULL
);''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS MealParticipant (
meal_id INTEGER,
person_id INTEGER,
role TEXT,
FOREIGN KEY(meal_id) REFERENCES Meal(id),
FOREIGN KEY(person_id) REFERENCES Person(id)
);''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS MealRecipe (
meal_id INTEGER,
recipe_id INTEGER,
servings REAL,
FOREIGN KEY(meal_id) REFERENCES Meal(id),
FOREIGN KEY(recipe_id) REFERENCES Recipe(id)
);''')
async def insert_meal_participant(conn, meal_id: int, person_id: int, role: str):
await conn.execute('''
INSERT INTO MealParticipant (meal_id, person_id, role)
VALUES (?, ?, ?)
''', (meal_id, person_id, role))
async def sync_meal_participants(conn, meal_id: int, participants: List[Person], role: str):
await conn.execute('''
DELETE FROM MealParticipant
WHERE meal_id = ? AND role = ?
''', (meal_id, role))
for person in participants:
await insert_meal_participant(conn, meal_id, person.id, role)
async def insert_meal_recipe(conn, r: MealRecipe):
if r.meal_id < 0:
raise ValueError('Meal must be inserted before meal recipe')
if r.recipe_id < 0 and r.recipe:
r.recipe_id = r.recipe.id
if r.recipe_id < 0:
raise ValueError('Recipe must be inserted before meal')
await conn.execute('''
INSERT INTO MealRecipe (meal_id, recipe_id, servings)
VALUES (?, ?, ?)
''', (r.meal_id, r.recipe_id, r.servings))
async def insert_meal(conn, meal: Meal):
async with conn.execute('''
INSERT INTO Meal (suggested_date)
VALUES (?)
''', (meal.suggested_date.isoformat(),)) as cursor:
meal.id = cursor.lastrowid
await sync_meal_participants(conn, meal.id, meal.chefs, 'chef')
await sync_meal_participants(conn, meal.id, meal.cleanup, 'cleanup')
await sync_meal_participants(conn, meal.id, meal.consumers, 'consumer')
for meal_recipe in meal.recipes:
meal_recipe.meal_id = meal.id
await insert_meal_recipe(conn, meal_recipe)
await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients)
async def find_meal_by_id(conn, meal_id: int) -> Meal:
async with conn.execute(f'''
SELECT {','.join(Meal.KEYS)} FROM Meal
WHERE id = ?
LIMIT 1
''', (meal_id,)) as cursor:
async for row in cursor:
meal = Meal(**{k:v for k,v in zip(Meal.KEYS, row)})
await load_participants(conn, meal)
await load_recipes(conn, meal)
await load_extra_ingredients(conn, meal)
return meal
async def find_upcoming_meals_by_date_range(conn, start: datetime, end: datetime) -> AsyncIterator[Meal]:
async with conn.execute(f'''
SELECT {','.join(Meal.KEYS)} FROM Meal
WHERE suggested_date >= ? AND suggested_date <= ? AND consumed_date IS NULL AND deleted_date IS NULL
''', (start, end)) as cursor:
async for row in cursor:
yield Meal(**{k:v for k,v in zip(Meal.KEYS, row)})
async def load_participants(conn, meal: Meal) -> None:
async with conn.execute(f'''
SELECT person_id, role FROM MealParticipant
WHERE meal_id = ?
''', (meal.id,)) as cursor:
async for row in cursor:
person = await persons.get_by_id(conn, row[0])
if row[1] == 'chef':
meal.chefs.append(person)
elif row[1] == 'cleanup':
meal.cleanup.append(person)
elif row[1] == 'consumer':
meal.consumers.append(person)
else:
raise Exception(f'Unknown role: {row[1]}')
async def load_recipes(conn, meal: Meal) -> None:
async with conn.execute(f'''
SELECT {','.join(Recipe.KEYS)}, MealRecipe.servings as requested_servings
FROM Recipe
JOIN MealRecipe ON MealRecipe.recipe_id = Recipe.id
WHERE MealRecipe.meal_id = ?
''', (meal.id,)) as cursor:
async for row in cursor:
recipe = row_to_recipe(zip(Recipe.KEYS, row[:-1]))
await load_recipe_ingredients(conn, recipe)
meal.recipes.append(MealRecipe(meal_id=meal.id, recipe_id=recipe.id, servings=row[-1], recipe=recipe))
async def load_extra_ingredients(conn, meal: Meal) -> None:
async for ingredient in find_ingredients_by_meal_id(conn, meal.id):
meal.extra_ingredients.append(ingredient)
async def delete_meal(conn, meal_id: int) -> None:
await conn.execute('''
UPDATE Meal
SET deleted_date = ?
WHERE id = ?
''', (datetime.datetime.now().astimezone().isoformat(), meal_id))
async def sync_extra_ingredients(conn, meal_id: int, ingredients: List[Ingredient]) -> None:
await delete_ingredients_by_meal_id(conn, meal_id)
for ingredient in ingredients:
ingredient.meal_id = meal_id
ingredient.recipe_id = None
await insert_ingredient(conn, ingredient)
async def sync_meal_recipes(conn, meal_id: int, recipes: List[Recipe]) -> None:
await conn.execute('''
DELETE FROM MealRecipe
WHERE meal_id = ?
''', (meal_id,))
for meal_recipe in recipes:
if meal_recipe.meal_id >= 0 and meal_recipe.meal_id != meal_id:
raise ValueError('Already associated with another meal')
meal_recipe.meal_id = meal_id
await insert_meal_recipe(conn, meal_recipe)
async def update_meal(conn, meal: Meal) -> None:
await conn.execute('''
UPDATE Meal
SET suggested_date = ?
WHERE id = ?
''', (meal.suggested_date.isoformat(), meal.id))
await sync_meal_participants(conn, meal.id, meal.chefs, 'chef')
await sync_meal_participants(conn, meal.id, meal.cleanup, 'cleanup')
await sync_meal_participants(conn, meal.id, meal.consumers, 'consumer')
await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients)
await sync_meal_recipes(conn, meal.id, meal.recipes)
async def mark_consumed(conn, meal: Meal, date: datetime.datetime) -> None:
meal.consumed_date = date
await conn.execute('''
UPDATE Meal
SET consumed_date = ?
WHERE id = ?
''', (date.isoformat(), meal.id))
async def mark_purchased(conn, meal: Meal) -> Meal:
meal.purchase_date = datetime.datetime.now().astimezone()
await conn.execute('''
UPDATE Meal
SET purchase_date = ?
WHERE id = ?
''', (meal.purchase_date.isoformat(), meal.id))
return meal