2024-01-13 08:44:07 +00:00
|
|
|
import json
|
2024-01-13 03:21:38 +00:00
|
|
|
|
2024-01-17 12:51:07 +00:00
|
|
|
from ingredients import Ingredient, find_ingredients_by_recipe_id
|
2024-01-13 03:21:38 +00:00
|
|
|
|
2024-01-17 10:39:48 +00:00
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
from typing import List, ClassVar, Tuple
|
2024-01-13 03:21:38 +00:00
|
|
|
|
|
|
|
|
class Recipe(BaseModel):
|
2024-01-13 08:44:07 +00:00
|
|
|
KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'image_urls', 'raw_data']
|
2024-01-13 03:21:38 +00:00
|
|
|
id: int
|
|
|
|
|
name: str
|
|
|
|
|
link: str
|
|
|
|
|
raw_data: str
|
2024-01-13 08:44:07 +00:00
|
|
|
image_urls: List[str] = []
|
2024-01-13 03:21:38 +00:00
|
|
|
ingredients: List[Ingredient] = []
|
|
|
|
|
|
|
|
|
|
async def create(conn):
|
|
|
|
|
await conn.execute('''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS Recipe (
|
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
|
name TEXT,
|
|
|
|
|
link TEXT,
|
2024-01-13 08:44:07 +00:00
|
|
|
image_urls TEXT,
|
2024-01-13 03:21:38 +00:00
|
|
|
raw_data TEXT
|
|
|
|
|
);''')
|
|
|
|
|
|
|
|
|
|
async def insert_recipe(conn, recipe: Recipe):
|
|
|
|
|
async with conn.execute('''
|
2024-01-13 08:44:07 +00:00
|
|
|
INSERT INTO Recipe (name, link, raw_data, image_urls)
|
2024-01-13 11:37:39 +00:00
|
|
|
VALUES (?, ?, ?, ?)
|
2024-01-13 08:44:07 +00:00
|
|
|
''', (recipe.name, recipe.link, recipe.raw_data, json.dumps(recipe.image_urls))) as cursor:
|
2024-01-13 03:21:38 +00:00
|
|
|
recipe.id = cursor.lastrowid
|
|
|
|
|
|
2024-01-17 10:39:48 +00:00
|
|
|
def row_to_recipe(col_tuples: List[Tuple[str, ...]]) -> Recipe:
|
|
|
|
|
d = {k:v for k,v in col_tuples}
|
2024-01-13 11:37:39 +00:00
|
|
|
d['image_urls'] = json.loads(d['image_urls'])
|
2024-01-13 08:44:07 +00:00
|
|
|
return Recipe(**d)
|
|
|
|
|
|
2024-01-13 03:21:38 +00:00
|
|
|
async def find_recipe_by_id(conn, recipe_id: int) -> Recipe:
|
|
|
|
|
async with conn.execute(f'''
|
|
|
|
|
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
LIMIT 1
|
|
|
|
|
''', (recipe_id,)) as cursor:
|
|
|
|
|
async for row in cursor:
|
2024-01-17 10:39:48 +00:00
|
|
|
return row_to_recipe(zip(Recipe.KEYS, row))
|
2024-01-13 08:44:07 +00:00
|
|
|
|
2024-01-13 11:37:39 +00:00
|
|
|
async def find_recipes_by_name(conn, name: str) -> List[Recipe]:
|
|
|
|
|
async with conn.execute(f'''
|
|
|
|
|
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
|
|
|
|
WHERE name LIKE ?
|
|
|
|
|
''', (f'%{name}%',)) as cursor:
|
|
|
|
|
async for row in cursor:
|
2024-01-17 10:39:48 +00:00
|
|
|
yield row_to_recipe(zip(Recipe.KEYS, row))
|
2024-01-13 11:37:39 +00:00
|
|
|
|
2024-01-13 08:44:07 +00:00
|
|
|
async def get_all(conn) -> List[Recipe]:
|
|
|
|
|
async with conn.execute(f'''
|
|
|
|
|
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
|
|
|
|
''') as cursor:
|
|
|
|
|
async for row in cursor:
|
2024-01-17 10:39:48 +00:00
|
|
|
yield row_to_recipe(zip(Recipe.KEYS, row))
|
|
|
|
|
|
|
|
|
|
async def load_ingredients(conn, recipe: Recipe):
|
2024-01-17 12:51:07 +00:00
|
|
|
async for ingredient in find_ingredients_by_recipe_id(conn, recipe.id):
|
|
|
|
|
recipe.ingredients.append(ingredient)
|