2024-01-17 07:21:16 +00:00
|
|
|
from products import Product
|
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from typing import List, ClassVar, Optional
|
|
|
|
|
|
|
|
|
|
class Ingredient(BaseModel):
|
|
|
|
|
KEYS: ClassVar[List[str]] = ['id', 'name', 'line', 'preparation', 'unit', 'quantity', 'product_id', 'recipe_id', 'meal_id']
|
|
|
|
|
id: int
|
|
|
|
|
name: str
|
|
|
|
|
line: str
|
|
|
|
|
unit: str
|
|
|
|
|
quantity: float
|
|
|
|
|
preparation: str
|
|
|
|
|
product_id: Optional[int] = None
|
|
|
|
|
recipe_id: Optional[int] = None
|
|
|
|
|
meal_id: Optional[int] = None
|
2024-01-17 12:51:07 +00:00
|
|
|
product: Optional[Product] = None
|
2024-01-17 07:21:16 +00:00
|
|
|
|
|
|
|
|
async def create(conn):
|
|
|
|
|
await conn.execute('''
|
|
|
|
|
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)
|
|
|
|
|
);''')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def insert_ingredient(conn, ingredient: Ingredient):
|
|
|
|
|
async with conn.execute('''
|
|
|
|
|
INSERT INTO Ingredient (name, line, preparation, unit, quantity, product_id, recipe_id, meal_id)
|
2024-01-17 08:36:54 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
2024-01-17 07:21:16 +00:00
|
|
|
''', (ingredient.name, ingredient.line, ingredient.preparation, ingredient.unit, ingredient.quantity, ingredient.product_id, ingredient.recipe_id, ingredient.meal_id)) as cursor:
|
|
|
|
|
ingredient.id = cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]:
|
2024-01-17 12:51:07 +00:00
|
|
|
ingredient_keys = [f'ingredient.{key}' for key in Ingredient.KEYS]
|
|
|
|
|
product_keys = [f'product.{key}' for key in Product.KEYS]
|
|
|
|
|
|
2024-01-17 07:21:16 +00:00
|
|
|
async with conn.execute(f'''
|
2024-01-17 12:51:07 +00:00
|
|
|
SELECT {','.join(ingredient_keys + product_keys)} FROM Ingredient
|
|
|
|
|
LEFT JOIN Product ON Ingredient.product_id = Product.id
|
2024-01-17 07:21:16 +00:00
|
|
|
WHERE recipe_id = ?
|
|
|
|
|
''', (recipe_id,)) as cursor:
|
|
|
|
|
async for row in cursor:
|
2024-01-17 12:51:07 +00:00
|
|
|
product_keys = {k:v for k,v in zip(Product.KEYS, row[len(Ingredient.KEYS):])}
|
|
|
|
|
product = Product(**product_keys) if product_keys['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_meal_id(conn, meal_id: int) -> List[Ingredient]:
|
2024-01-17 12:51:07 +00:00
|
|
|
ingredient_keys = [f'ingredient.{key}' for key in Ingredient.KEYS]
|
|
|
|
|
product_keys = [f'product.{key}' for key in Product.KEYS]
|
|
|
|
|
|
2024-01-17 07:21:16 +00:00
|
|
|
async with conn.execute(f'''
|
2024-01-17 12:51:07 +00:00
|
|
|
SELECT {','.join(ingredient_keys + product_keys)} FROM Ingredient
|
|
|
|
|
LEFT JOIN Product ON Ingredient.product_id = Product.id
|
2024-01-17 07:21:16 +00:00
|
|
|
WHERE meal_id = ?
|
|
|
|
|
''', (meal_id,)) as cursor:
|
|
|
|
|
async for row in cursor:
|
2024-01-17 12:51:07 +00:00
|
|
|
product_keys = {k:v for k,v in zip(Product.KEYS, row[len(Ingredient.KEYS):])}
|
|
|
|
|
product = Product(**product_keys) if product_keys['id'] else None
|
|
|
|
|
yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row[:len(Ingredient.KEYS)])}, product=product)
|