munch-ease-backend/ingredients/repository.py

160 lines
5.7 KiB
Python
Raw Permalink Normal View History

Squashed commit of the following: commit fcd005b8624023547f28b7b28e59e6099bcfc7d4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 20:24:07 2025 +1100 Openapi tightening commit f93bd8f641d561052c7bd075bae321b4ff3b676d Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 19:03:52 2025 +1100 Removed refactor strategy doc commit 0c5a61092f522be0c47cbbe86917c8a7e4e2d339 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 17:48:33 2025 +1100 mypy & ruff checks commit 23d66d6b18984127e17c73c3063f6120385935e9 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 16:49:35 2025 +1100 Final removal of db.py files commit f454aed1ca9783cc558cc203f29a7fe31b62a975 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:42:31 2025 +1100 Finalise restructure, remove db.py files commit 7187f6dd89489521538791c6bdebb426514beb99 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:34:54 2025 +1100 commit 6fea227ae20d32b8eb1e7a4885006a620fcc7bb1 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:32:53 2025 +1100 commit 27415e7e02d89195ad514cb017a9dbbf84d7a5e4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:31:10 2025 +1100 commit b773428033d855f9ad82005602e049c1a2e3c585 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:28:58 2025 +1100 commit 116592c95278d995f4c516e87f2cea43cf5b7735 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:25:21 2025 +1100 commit 03ec565faea088971968ee2f9bb83e2de16b21f3 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:21:29 2025 +1100 Plan
2025-10-19 09:24:23 +00:00
from typing import AsyncIterator, List, Optional
2025-10-18 03:26:42 +00:00
Squashed commit of the following: commit fcd005b8624023547f28b7b28e59e6099bcfc7d4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 20:24:07 2025 +1100 Openapi tightening commit f93bd8f641d561052c7bd075bae321b4ff3b676d Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 19:03:52 2025 +1100 Removed refactor strategy doc commit 0c5a61092f522be0c47cbbe86917c8a7e4e2d339 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 17:48:33 2025 +1100 mypy & ruff checks commit 23d66d6b18984127e17c73c3063f6120385935e9 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 16:49:35 2025 +1100 Final removal of db.py files commit f454aed1ca9783cc558cc203f29a7fe31b62a975 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:42:31 2025 +1100 Finalise restructure, remove db.py files commit 7187f6dd89489521538791c6bdebb426514beb99 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:34:54 2025 +1100 commit 6fea227ae20d32b8eb1e7a4885006a620fcc7bb1 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:32:53 2025 +1100 commit 27415e7e02d89195ad514cb017a9dbbf84d7a5e4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:31:10 2025 +1100 commit b773428033d855f9ad82005602e049c1a2e3c585 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:28:58 2025 +1100 commit 116592c95278d995f4c516e87f2cea43cf5b7735 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:25:21 2025 +1100 commit 03ec565faea088971968ee2f9bb83e2de16b21f3 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:21:29 2025 +1100 Plan
2025-10-19 09:24:23 +00:00
from ingredients.models import Ingredient
from products.models import Product
2025-10-18 03:26:42 +00:00
2024-01-17 07:21:16 +00:00
async def create(conn):
2025-10-18 03:26:42 +00:00
await conn.execute(
"""
2024-01-17 07:21:16 +00:00
CREATE TABLE IF NOT EXISTS Ingredient (
id INTEGER PRIMARY KEY,
name TEXT,
line TEXT,
preparation TEXT,
unit TEXT,
quantity REAL,
product_id INTEGER,
recipe_id INTEGER,
meal_id INTEGER,
FOREIGN KEY (product_id) REFERENCES Product(id),
FOREIGN KEY (recipe_id) REFERENCES Recipe(id),
FOREIGN KEY (meal_id) REFERENCES Meal(id)
2025-10-18 03:26:42 +00:00
);"""
)
# Useful indexes
await conn.execute(
"CREATE INDEX IF NOT EXISTS idx_ingredient_recipe_id ON Ingredient(recipe_id);"
)
await conn.execute("CREATE INDEX IF NOT EXISTS idx_ingredient_meal_id ON Ingredient(meal_id);")
2025-10-18 03:26:42 +00:00
2024-01-17 07:21:16 +00:00
async def insert_ingredient(conn, ingredient: Ingredient):
2024-05-20 10:09:57 +00:00
if ingredient.product:
2024-04-25 04:57:39 +00:00
ingredient.product_id = ingredient.product.id
2025-07-30 08:19:20 +00:00
if ingredient.product_id is None or ingredient.product_id < 0:
ingredient.product_id = None
2024-04-25 04:57:39 +00:00
2025-10-18 03:26:42 +00:00
async with conn.execute(
"""
2024-01-17 07:21:16 +00:00
INSERT INTO Ingredient (name, line, preparation, unit, quantity, product_id, recipe_id, meal_id)
2024-01-17 08:36:54 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
2025-10-18 03:26:42 +00:00
""",
(
ingredient.name,
ingredient.line,
ingredient.preparation,
ingredient.unit,
ingredient.quantity,
ingredient.product_id,
ingredient.recipe_id,
ingredient.meal_id,
),
) as cursor:
2024-01-17 07:21:16 +00:00
ingredient.id = cursor.lastrowid
2025-10-18 03:26:42 +00:00
2025-07-30 08:19:20 +00:00
async def find_ingredient_by_id(conn, ingredient_id: int) -> Optional[Ingredient]:
2025-10-18 03:26:42 +00:00
ingredient_cols = [f"ingredient.{key}" for key in Ingredient.KEYS]
product_cols = [f"product.{key}" for key in Product.KEYS]
2025-07-30 08:19:20 +00:00
2025-10-18 03:26:42 +00:00
async with conn.execute(
f"""
SELECT {",".join(ingredient_cols + product_cols)} FROM Ingredient
2025-07-30 08:19:20 +00:00
LEFT JOIN Product ON Ingredient.product_id = Product.id
2025-07-30 08:55:51 +00:00
WHERE Ingredient.id = ?
2025-10-18 03:26:42 +00:00
""",
(ingredient_id,),
) as cursor:
2025-07-30 08:19:20 +00:00
async for row in cursor:
2025-10-18 03:26:42 +00:00
product_map = {k: v for k, v in zip(Product.KEYS, row[len(Ingredient.KEYS) :])}
product = Product(**product_map) if product_map["id"] else None
return Ingredient(
**{k: v for k, v in zip(Ingredient.KEYS, row[: len(Ingredient.KEYS)])},
product=product,
)
2025-07-30 08:19:20 +00:00
return None
2025-10-18 03:26:42 +00:00
2024-05-13 03:59:46 +00:00
async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> AsyncIterator[Ingredient]:
2025-10-18 03:26:42 +00:00
ingredient_cols = [f"ingredient.{key}" for key in Ingredient.KEYS]
product_cols = [f"product.{key}" for key in Product.KEYS]
2025-10-18 03:26:42 +00:00
async with conn.execute(
f"""
SELECT {",".join(ingredient_cols + product_cols)} FROM Ingredient
LEFT JOIN Product ON Ingredient.product_id = Product.id
2024-01-17 07:21:16 +00:00
WHERE recipe_id = ?
2025-10-18 03:26:42 +00:00
""",
(recipe_id,),
) as cursor:
2024-01-17 07:21:16 +00:00
async for row in cursor:
2025-10-18 03:26:42 +00:00
product_map = {k: v for k, v in zip(Product.KEYS, row[len(Ingredient.KEYS) :])}
product = Product(**product_map) if product_map["id"] else None
yield Ingredient(
**{k: v for k, v in zip(Ingredient.KEYS, row[: len(Ingredient.KEYS)])},
product=product,
)
2024-01-17 07:21:16 +00:00
async def find_ingredients_by_recipe_ids(
conn, recipe_ids: List[int]
) -> dict[int, List[Ingredient]]:
"""Fetch ingredients for many recipes in one query. Returns recipe_id -> [Ingredient]."""
if not recipe_ids:
return {}
placeholders = ",".join(["?"] * len(recipe_ids))
ingredient_cols = [f"ingredient.{key}" for key in Ingredient.KEYS]
product_cols = [f"product.{key}" for key in Product.KEYS]
query = f"""
SELECT {",".join(ingredient_cols + product_cols)}
FROM Ingredient AS ingredient
LEFT JOIN Product AS product ON ingredient.product_id = product.id
WHERE ingredient.recipe_id IN ({placeholders})
ORDER BY ingredient.recipe_id, ingredient.id
"""
result: dict[int, List[Ingredient]] = {rid: [] for rid in recipe_ids}
async with conn.execute(query, recipe_ids) as cursor:
async for row in cursor:
product_map = {k: v for k, v in zip(Product.KEYS, row[len(Ingredient.KEYS) :])}
product = Product(**product_map) if product_map["id"] else None
ing = Ingredient(
**{k: v for k, v in zip(Ingredient.KEYS, row[: len(Ingredient.KEYS)])},
product=product,
)
if ing.recipe_id is not None:
result.setdefault(int(ing.recipe_id), []).append(ing)
return result
2024-05-13 03:59:46 +00:00
async def find_ingredients_by_meal_id(conn, meal_id: int) -> AsyncIterator[Ingredient]:
2025-10-18 03:26:42 +00:00
ingredient_cols = [f"ingredient.{key}" for key in Ingredient.KEYS]
product_cols = [f"product.{key}" for key in Product.KEYS]
2025-10-18 03:26:42 +00:00
async with conn.execute(
f"""
SELECT {",".join(ingredient_cols + product_cols)} FROM Ingredient
LEFT JOIN Product ON Ingredient.product_id = Product.id
2024-01-17 07:21:16 +00:00
WHERE meal_id = ?
2025-10-18 03:26:42 +00:00
""",
(meal_id,),
) as cursor:
2024-01-17 07:21:16 +00:00
async for row in cursor:
2025-10-18 03:26:42 +00:00
product_map = {k: v for k, v in zip(Product.KEYS, row[len(Ingredient.KEYS) :])}
product = Product(**product_map) if product_map["id"] else None
yield Ingredient(
**{k: v for k, v in zip(Ingredient.KEYS, row[: len(Ingredient.KEYS)])},
product=product,
)
2024-05-02 11:20:52 +00:00
async def delete_ingredients_by_meal_id(conn, meal_id: int):
2025-10-18 03:26:42 +00:00
await conn.execute(
"""
2024-05-02 11:20:52 +00:00
DELETE FROM Ingredient
WHERE meal_id = ?
2025-10-18 03:26:42 +00:00
""",
(meal_id,),
)