2025-10-18 03:26:42 +00:00
|
|
|
import datetime
|
2025-10-19 09:24:23 +00:00
|
|
|
from typing import AsyncIterator, List, Optional
|
2024-04-25 04:57:39 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
from ingredients import (
|
|
|
|
|
Ingredient,
|
|
|
|
|
delete_ingredients_by_meal_id,
|
|
|
|
|
find_ingredients_by_meal_id,
|
|
|
|
|
insert_ingredient,
|
|
|
|
|
)
|
2025-10-19 09:24:23 +00:00
|
|
|
from meals.models import Meal, MealRecipe
|
|
|
|
|
from persons.models import Person
|
|
|
|
|
from persons.repository import get_by_ids as persons_get_by_ids
|
|
|
|
|
from recipes.models import Recipe
|
|
|
|
|
from recipes.repository import load_recipe_ingredients, row_to_recipe
|
2024-09-28 04:54:54 +00:00
|
|
|
|
2025-10-19 09:24:23 +00:00
|
|
|
from .roles import ROLE_CHEF, ROLE_CLEANUP, ROLE_CONSUMER
|
2024-05-25 02:33:41 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-01-13 07:18:25 +00:00
|
|
|
async def create(conn):
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-01-13 07:18:25 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS Meal (
|
|
|
|
|
id INTEGER PRIMARY KEY,
|
2024-10-14 05:59:05 +00:00
|
|
|
suggested_date DATETIME,
|
|
|
|
|
consumed_date DATETIME DEFAULT NULL,
|
2024-10-14 06:36:56 +00:00
|
|
|
deleted_date DATETIME DEFAULT NULL,
|
|
|
|
|
purchase_date DATETIME DEFAULT NULL
|
2025-10-18 03:26:42 +00:00
|
|
|
);"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-01-13 07:18:25 +00:00
|
|
|
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)
|
2025-10-18 03:26:42 +00:00
|
|
|
);"""
|
|
|
|
|
)
|
2025-10-19 02:12:28 +00:00
|
|
|
# Useful indexes
|
2025-10-19 13:12:16 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"CREATE INDEX IF NOT EXISTS idx_meal_participants_meal_role ON MealParticipant(meal_id, role);"
|
|
|
|
|
)
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-01-13 07:18:25 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS MealRecipe (
|
|
|
|
|
meal_id INTEGER,
|
|
|
|
|
recipe_id INTEGER,
|
2024-09-28 04:54:54 +00:00
|
|
|
servings REAL,
|
2024-01-13 07:18:25 +00:00
|
|
|
FOREIGN KEY(meal_id) REFERENCES Meal(id),
|
|
|
|
|
FOREIGN KEY(recipe_id) REFERENCES Recipe(id)
|
2025-10-18 03:26:42 +00:00
|
|
|
);"""
|
|
|
|
|
)
|
|
|
|
|
|
2025-10-19 02:12:28 +00:00
|
|
|
# Index for faster lookup of recipes by meal
|
2025-10-19 13:12:16 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"CREATE INDEX IF NOT EXISTS idx_meal_recipes_meal_id ON MealRecipe(meal_id);"
|
|
|
|
|
)
|
2025-10-19 02:12:28 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-01-13 07:42:23 +00:00
|
|
|
async def insert_meal_participant(conn, meal_id: int, person_id: int, role: str):
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-01-13 07:42:23 +00:00
|
|
|
INSERT INTO MealParticipant (meal_id, person_id, role)
|
|
|
|
|
VALUES (?, ?, ?)
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal_id, person_id, role),
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-13 07:42:23 +00:00
|
|
|
|
2024-05-02 11:20:52 +00:00
|
|
|
async def sync_meal_participants(conn, meal_id: int, participants: List[Person], role: str):
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-05-02 11:20:52 +00:00
|
|
|
DELETE FROM MealParticipant
|
|
|
|
|
WHERE meal_id = ? AND role = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal_id, role),
|
|
|
|
|
)
|
2024-05-02 11:20:52 +00:00
|
|
|
|
|
|
|
|
for person in participants:
|
|
|
|
|
await insert_meal_participant(conn, meal_id, person.id, role)
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
async def insert_meal_recipe(conn, r: MealRecipe):
|
|
|
|
|
if r.meal_id < 0:
|
2025-10-18 03:26:42 +00:00
|
|
|
raise ValueError("Meal must be inserted before meal recipe")
|
|
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
if r.recipe_id < 0 and r.recipe:
|
|
|
|
|
r.recipe_id = r.recipe.id
|
|
|
|
|
|
|
|
|
|
if r.recipe_id < 0:
|
2025-10-18 03:26:42 +00:00
|
|
|
raise ValueError("Recipe must be inserted before meal")
|
|
|
|
|
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-09-28 04:54:54 +00:00
|
|
|
INSERT INTO MealRecipe (meal_id, recipe_id, servings)
|
|
|
|
|
VALUES (?, ?, ?)
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(r.meal_id, r.recipe_id, r.servings),
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-17 10:39:48 +00:00
|
|
|
|
2024-01-13 07:18:25 +00:00
|
|
|
async def insert_meal(conn, meal: Meal):
|
2025-10-18 03:26:42 +00:00
|
|
|
async with conn.execute(
|
|
|
|
|
"""
|
2024-05-25 02:09:32 +00:00
|
|
|
INSERT INTO Meal (suggested_date)
|
2024-01-13 07:18:25 +00:00
|
|
|
VALUES (?)
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal.suggested_date.isoformat(),),
|
|
|
|
|
) as cursor:
|
2024-01-13 07:42:23 +00:00
|
|
|
meal.id = cursor.lastrowid
|
|
|
|
|
|
2025-10-19 02:51:27 +00:00
|
|
|
await sync_meal_participants(conn, meal.id, meal.chefs, ROLE_CHEF)
|
|
|
|
|
await sync_meal_participants(conn, meal.id, meal.cleanup, ROLE_CLEANUP)
|
|
|
|
|
await sync_meal_participants(conn, meal.id, meal.consumers, ROLE_CONSUMER)
|
2024-01-13 07:18:25 +00:00
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
for meal_recipe in meal.recipes:
|
|
|
|
|
meal_recipe.meal_id = meal.id
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
await insert_meal_recipe(conn, meal_recipe)
|
2024-01-17 10:39:48 +00:00
|
|
|
|
2024-05-02 11:20:52 +00:00
|
|
|
await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients)
|
2024-01-17 10:39:48 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
async def find_meal_by_id(conn, meal_id: int) -> Optional[Meal]:
|
|
|
|
|
async with conn.execute(
|
|
|
|
|
f"""
|
2025-10-19 13:12:16 +00:00
|
|
|
SELECT {",".join(Meal.KEYS)} FROM Meal
|
2024-01-13 07:18:25 +00:00
|
|
|
WHERE id = ?
|
|
|
|
|
LIMIT 1
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal_id,),
|
2025-11-01 03:30:45 +00:00
|
|
|
) 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
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def find_meal_by_id_scoped(conn, meal_id: int, household_id: int) -> Optional[Meal]:
|
|
|
|
|
async with conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT {",".join(Meal.KEYS)} FROM Meal
|
|
|
|
|
WHERE id = ? AND household_id = ?
|
|
|
|
|
LIMIT 1
|
|
|
|
|
""",
|
|
|
|
|
(meal_id, household_id),
|
2025-10-18 03:26:42 +00:00
|
|
|
) as cursor:
|
2024-01-13 07:18:25 +00:00
|
|
|
async for row in cursor:
|
2025-10-18 03:26:42 +00:00
|
|
|
meal = Meal(**{k: v for k, v in zip(Meal.KEYS, row)})
|
2024-05-20 23:57:56 +00:00
|
|
|
|
2024-05-18 07:05:01 +00:00
|
|
|
await load_participants(conn, meal)
|
|
|
|
|
await load_recipes(conn, meal)
|
|
|
|
|
await load_extra_ingredients(conn, meal)
|
|
|
|
|
return meal
|
2025-10-18 03:26:42 +00:00
|
|
|
return None
|
2024-01-13 07:18:25 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
async def find_upcoming_meals_by_date_range(
|
|
|
|
|
conn, start: datetime.datetime, end: datetime.datetime
|
|
|
|
|
) -> AsyncIterator[Meal]:
|
|
|
|
|
async with conn.execute(
|
|
|
|
|
f"""
|
2025-10-19 13:12:16 +00:00
|
|
|
SELECT {",".join(Meal.KEYS)} FROM Meal
|
2024-09-28 05:04:12 +00:00
|
|
|
WHERE suggested_date >= ? AND suggested_date <= ? AND consumed_date IS NULL AND deleted_date IS NULL
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(start, end),
|
|
|
|
|
) as cursor:
|
2024-01-13 07:42:23 +00:00
|
|
|
async for row in cursor:
|
2025-10-18 03:26:42 +00:00
|
|
|
yield Meal(**{k: v for k, v in zip(Meal.KEYS, row)})
|
|
|
|
|
|
|
|
|
|
|
2025-11-01 03:21:34 +00:00
|
|
|
async def find_upcoming_meals_by_date_range_scoped(
|
|
|
|
|
conn, start: datetime.datetime, end: datetime.datetime, household_id: int
|
|
|
|
|
) -> 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 AND household_id = ?
|
|
|
|
|
""",
|
|
|
|
|
(start, end, household_id),
|
|
|
|
|
) as cursor:
|
|
|
|
|
async for row in cursor:
|
|
|
|
|
yield Meal(**{k: v for k, v in zip(Meal.KEYS, row)})
|
|
|
|
|
|
|
|
|
|
|
2024-01-13 07:42:23 +00:00
|
|
|
async def load_participants(conn, meal: Meal) -> None:
|
2025-10-19 02:12:28 +00:00
|
|
|
# Fetch all participant links
|
|
|
|
|
links: list[tuple[int, str]] = []
|
2025-10-18 03:26:42 +00:00
|
|
|
async with conn.execute(
|
|
|
|
|
"""
|
2024-01-13 07:42:23 +00:00
|
|
|
SELECT person_id, role FROM MealParticipant
|
|
|
|
|
WHERE meal_id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal.id,),
|
|
|
|
|
) as cursor:
|
2024-01-13 07:42:23 +00:00
|
|
|
async for row in cursor:
|
2025-10-19 02:12:28 +00:00
|
|
|
links.append((int(row[0]), str(row[1])))
|
|
|
|
|
|
|
|
|
|
if not links:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Bulk load persons by id
|
|
|
|
|
unique_ids = sorted({pid for pid, _ in links})
|
|
|
|
|
people = await persons_get_by_ids(conn, unique_ids)
|
|
|
|
|
|
|
|
|
|
for pid, role in links:
|
|
|
|
|
person = people.get(pid)
|
2025-10-19 02:51:27 +00:00
|
|
|
if role == ROLE_CHEF:
|
2025-10-19 02:12:28 +00:00
|
|
|
if person:
|
|
|
|
|
meal.chefs.append(person)
|
2025-10-19 02:51:27 +00:00
|
|
|
elif role == ROLE_CLEANUP:
|
2025-10-19 02:12:28 +00:00
|
|
|
if person:
|
|
|
|
|
meal.cleanup.append(person)
|
2025-10-19 02:51:27 +00:00
|
|
|
elif role == ROLE_CONSUMER:
|
2025-10-19 02:12:28 +00:00
|
|
|
if person:
|
|
|
|
|
meal.consumers.append(person)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown role: {role}")
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
|
2025-10-19 02:21:56 +00:00
|
|
|
async def bulk_load_participants(conn, meals: List[Meal]) -> None:
|
|
|
|
|
"""Populate participants for many meals in one query to avoid N+1.
|
|
|
|
|
|
|
|
|
|
For each meal, fills meal.chefs, meal.cleanup, meal.consumers using a bulk
|
|
|
|
|
lookup of MealParticipant rows and a single persons.get_by_ids fetch.
|
|
|
|
|
"""
|
|
|
|
|
if not meals:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
meal_ids = [m.id for m in meals]
|
|
|
|
|
placeholders = ",".join(["?"] * len(meal_ids))
|
|
|
|
|
|
|
|
|
|
# Collect (meal_id -> [(person_id, role), ...]) and dedupe person IDs
|
|
|
|
|
links_by_meal: dict[int, list[tuple[int, str]]] = {mid: [] for mid in meal_ids}
|
|
|
|
|
person_ids: set[int] = set()
|
|
|
|
|
async with conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT meal_id, person_id, role
|
|
|
|
|
FROM MealParticipant
|
|
|
|
|
WHERE meal_id IN ({placeholders})
|
|
|
|
|
""",
|
|
|
|
|
meal_ids,
|
|
|
|
|
) as cursor:
|
|
|
|
|
async for row in cursor:
|
|
|
|
|
mid, pid, role = int(row[0]), int(row[1]), str(row[2])
|
|
|
|
|
links_by_meal.setdefault(mid, []).append((pid, role))
|
|
|
|
|
person_ids.add(pid)
|
|
|
|
|
|
|
|
|
|
if not person_ids:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Bulk load persons once
|
|
|
|
|
people = await persons_get_by_ids(conn, sorted(person_ids))
|
|
|
|
|
|
|
|
|
|
# Assign per meal
|
|
|
|
|
by_id = {m.id: m for m in meals}
|
|
|
|
|
for mid, links in links_by_meal.items():
|
|
|
|
|
meal = by_id.get(mid)
|
|
|
|
|
if not meal:
|
|
|
|
|
continue
|
|
|
|
|
# Reset roles to avoid duplicates
|
|
|
|
|
meal.chefs = []
|
|
|
|
|
meal.cleanup = []
|
|
|
|
|
meal.consumers = []
|
|
|
|
|
for pid, role in links:
|
|
|
|
|
person = people.get(pid)
|
|
|
|
|
if not person:
|
|
|
|
|
continue
|
2025-10-19 02:51:27 +00:00
|
|
|
if role == ROLE_CHEF:
|
2025-10-19 02:21:56 +00:00
|
|
|
meal.chefs.append(person)
|
2025-10-19 02:51:27 +00:00
|
|
|
elif role == ROLE_CLEANUP:
|
2025-10-19 02:21:56 +00:00
|
|
|
meal.cleanup.append(person)
|
2025-10-19 02:51:27 +00:00
|
|
|
elif role == ROLE_CONSUMER:
|
2025-10-19 02:21:56 +00:00
|
|
|
meal.consumers.append(person)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown role: {role}")
|
|
|
|
|
|
|
|
|
|
|
2024-01-17 10:39:48 +00:00
|
|
|
async def load_recipes(conn, meal: Meal) -> None:
|
2025-10-18 03:26:42 +00:00
|
|
|
async with conn.execute(
|
|
|
|
|
f"""
|
2025-10-19 13:12:16 +00:00
|
|
|
SELECT {",".join(Recipe.KEYS)}, MealRecipe.servings as requested_servings
|
2024-09-28 04:54:54 +00:00
|
|
|
FROM Recipe
|
2024-01-17 10:39:48 +00:00
|
|
|
JOIN MealRecipe ON MealRecipe.recipe_id = Recipe.id
|
|
|
|
|
WHERE MealRecipe.meal_id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal.id,),
|
|
|
|
|
) as cursor:
|
2024-01-17 10:39:48 +00:00
|
|
|
async for row in cursor:
|
2025-10-18 03:26:42 +00:00
|
|
|
recipe = row_to_recipe(list(zip(Recipe.KEYS, row[:-1])))
|
2024-04-25 04:57:39 +00:00
|
|
|
await load_recipe_ingredients(conn, recipe)
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
meal.recipes.append(
|
|
|
|
|
MealRecipe(meal_id=meal.id, recipe_id=recipe.id, servings=row[-1], recipe=recipe)
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-17 10:39:48 +00:00
|
|
|
|
|
|
|
|
async def load_extra_ingredients(conn, meal: Meal) -> None:
|
2024-01-17 12:51:07 +00:00
|
|
|
async for ingredient in find_ingredients_by_meal_id(conn, meal.id):
|
|
|
|
|
meal.extra_ingredients.append(ingredient)
|
2024-01-17 11:43:16 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-01-17 11:43:16 +00:00
|
|
|
async def delete_meal(conn, meal_id: int) -> None:
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-09-28 05:04:12 +00:00
|
|
|
UPDATE Meal
|
|
|
|
|
SET deleted_date = ?
|
2024-01-17 11:43:16 +00:00
|
|
|
WHERE id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(datetime.datetime.now().astimezone().isoformat(), meal_id),
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-02 11:20:52 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
async def sync_meal_recipes(conn, meal_id: int, recipes: List[MealRecipe]) -> None:
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-05-02 11:20:52 +00:00
|
|
|
DELETE FROM MealRecipe
|
|
|
|
|
WHERE meal_id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal_id,),
|
|
|
|
|
)
|
2024-05-02 11:20:52 +00:00
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
for meal_recipe in recipes:
|
|
|
|
|
if meal_recipe.meal_id >= 0 and meal_recipe.meal_id != meal_id:
|
2025-10-18 03:26:42 +00:00
|
|
|
raise ValueError("Already associated with another meal")
|
|
|
|
|
|
2024-09-28 04:54:54 +00:00
|
|
|
meal_recipe.meal_id = meal_id
|
|
|
|
|
await insert_meal_recipe(conn, meal_recipe)
|
2024-05-02 11:20:52 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-05-02 11:20:52 +00:00
|
|
|
async def update_meal(conn, meal: Meal) -> None:
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-05-02 11:20:52 +00:00
|
|
|
UPDATE Meal
|
2024-05-25 02:09:32 +00:00
|
|
|
SET suggested_date = ?
|
2024-05-02 11:20:52 +00:00
|
|
|
WHERE id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal.suggested_date.isoformat(), meal.id),
|
|
|
|
|
)
|
2024-05-02 11:20:52 +00:00
|
|
|
|
2025-10-19 02:51:27 +00:00
|
|
|
await sync_meal_participants(conn, meal.id, meal.chefs, ROLE_CHEF)
|
|
|
|
|
await sync_meal_participants(conn, meal.id, meal.cleanup, ROLE_CLEANUP)
|
|
|
|
|
await sync_meal_participants(conn, meal.id, meal.consumers, ROLE_CONSUMER)
|
2024-05-02 11:20:52 +00:00
|
|
|
|
|
|
|
|
await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients)
|
2024-09-28 04:54:54 +00:00
|
|
|
await sync_meal_recipes(conn, meal.id, meal.recipes)
|
2024-05-20 23:57:56 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-10-14 05:59:05 +00:00
|
|
|
async def mark_consumed(conn, meal: Meal, date: datetime.datetime) -> None:
|
2024-05-25 02:33:41 +00:00
|
|
|
meal.consumed_date = date
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-05-25 02:33:41 +00:00
|
|
|
UPDATE Meal
|
|
|
|
|
SET consumed_date = ?
|
|
|
|
|
WHERE id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(date.isoformat(), meal.id),
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-25 02:33:41 +00:00
|
|
|
|
2024-10-13 08:19:57 +00:00
|
|
|
async def mark_purchased(conn, meal: Meal) -> Meal:
|
2024-10-14 05:59:05 +00:00
|
|
|
meal.purchase_date = datetime.datetime.now().astimezone()
|
2024-10-13 08:19:57 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
await conn.execute(
|
|
|
|
|
"""
|
2024-10-13 08:19:57 +00:00
|
|
|
UPDATE Meal
|
|
|
|
|
SET purchase_date = ?
|
|
|
|
|
WHERE id = ?
|
2025-10-18 03:26:42 +00:00
|
|
|
""",
|
|
|
|
|
(meal.purchase_date.isoformat(), meal.id),
|
|
|
|
|
)
|
2024-10-13 08:19:57 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
return meal
|