diff --git a/main.py b/main.py index 7534355..4d7d802 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ import sqlite3 -import products, recipes, db, meals +import products, recipes, db, meals, persons import datetime from pydantic import BaseModel @@ -64,6 +64,14 @@ async def load_full_recipe(conn: sqlite3.Connection, id: int) -> recipes.Recipe: return r +@app.get("/recipes/") +async def get_recipes(conn: sqlite3.Connection = Depends(get_db)) -> List[recipes.Recipe]: + result = [] + async for recipe in recipes.get_all(conn): + result.append(recipe) + + return result + @app.get("/recipes/{recipe_id}") async def get_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_db)) -> recipes.Recipe: r = await load_full_recipe(conn, recipe_id) @@ -112,4 +120,18 @@ async def create_meal(meal: meals.Meal, conn: sqlite3.Connection = Depends(get_d await meals.insert_meal(conn, meal) await conn.commit() - return meal \ No newline at end of file + return meal + +@app.get("/persons/") +async def get_persons(conn: sqlite3.Connection = Depends(get_db)) -> List[meals.Person]: + result = [] + async for person in persons.get_all(conn): + result.append(person) + + return result + +@app.post("/persons/") +async def create_person(person: persons.Person, conn: sqlite3.Connection = Depends(get_db)) -> persons.Person: + await persons.insert(conn, person) + await conn.commit() + return person \ No newline at end of file diff --git a/persons/__init__.py b/persons/__init__.py index 9bde8ba..7d6d950 100644 --- a/persons/__init__.py +++ b/persons/__init__.py @@ -1 +1 @@ -from persons.db import Person \ No newline at end of file +from persons.db import * \ No newline at end of file diff --git a/persons/db.py b/persons/db.py index c22241a..4fd313e 100644 --- a/persons/db.py +++ b/persons/db.py @@ -6,7 +6,35 @@ class Person(BaseModel): name: str async def create(conn): - return None + await conn.execute(''' + CREATE TABLE IF NOT EXISTS Person ( + id INTEGER PRIMARY KEY, + name TEXT UNIQUE + );''') -def get(conn, id: int) -> Person: - return Person(id=id, name='test') \ No newline at end of file +async def get_by_id(conn, id: int) -> Person: + cursor = await conn.execute(''' + SELECT id, name + FROM Person + WHERE id = ? + ''', (id,)) + row = await cursor.fetchone() + if not row: + return None + return Person(id=row[0], name=row[1]) + +async def get_all(conn) -> List[Person]: + async with conn.execute(''' + SELECT id, name + FROM Person + ''') as cursor: + async for row in cursor: + yield Person(id=row[0], name=row[1]) + +async def insert(conn, person: Person) -> Person: + cursor = await conn.execute(''' + INSERT INTO Person (name) + VALUES (?) + ''', (person.name,)) + person.id = cursor.lastrowid + return person \ No newline at end of file diff --git a/recipes/__init__.py b/recipes/__init__.py index ae8e28d..af051ea 100644 --- a/recipes/__init__.py +++ b/recipes/__init__.py @@ -1,10 +1,9 @@ -from products import Product -from recipes.db import Recipe, Ingredient, insert_recipe, insert_ingredient, find_recipe_by_id, find_ingredients_by_recipe_id +from products import Product, find_product_by_tag +from recipes.db import Recipe, Ingredient, insert_recipe, insert_ingredient, find_recipe_by_id, find_ingredients_by_recipe_id, get_all from recipes.scraping import scrape_recipe from ingredient_parser import parse_multiple_ingredients from typing import List -from products import find_product_by_tag import json, units @@ -51,10 +50,13 @@ async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[I async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict: ingredients = parse_ingredient_from_nlp(ldata['recipeIngredient']) ingredients = await match_existing_products(conn, ingredients) + name = ldata['name'] if 'name' in ldata else url + images = ldata['image'] if 'image' in ldata else [] return Recipe( id=0, - name=ldata['name'], + name=name, link=url, + image_urls=images, raw_data=json.dumps(ldata), ingredients=ingredients ) diff --git a/recipes/db.py b/recipes/db.py index 5082f76..7049a7d 100644 --- a/recipes/db.py +++ b/recipes/db.py @@ -1,3 +1,4 @@ +import json import aiosqlite from products import Product @@ -18,11 +19,12 @@ class Ingredient(BaseModel): product: Product = None class Recipe(BaseModel): - KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'raw_data'] + KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'image_urls', 'raw_data'] id: int name: str link: str raw_data: str + image_urls: List[str] = [] ingredients: List[Ingredient] = [] async def create(conn): @@ -31,6 +33,7 @@ async def create(conn): id INTEGER PRIMARY KEY, name TEXT, link TEXT, + image_urls TEXT, raw_data TEXT );''') @@ -57,11 +60,16 @@ async def insert_ingredient(conn, ingredient: Ingredient): async def insert_recipe(conn, recipe: Recipe): async with conn.execute(''' - INSERT INTO Recipe (name, link, raw_data) + INSERT INTO Recipe (name, link, raw_data, image_urls) VALUES (?, ?, ?) - ''', (recipe.name, recipe.link, recipe.raw_data)) as cursor: + ''', (recipe.name, recipe.link, recipe.raw_data, json.dumps(recipe.image_urls))) as cursor: recipe.id = cursor.lastrowid +def row_to_recipe(row) -> Recipe: + d = {k:v for k,v in zip(Recipe.KEYS, row)} + d['img_urls'] = json.loads(d['img_urls']) + return Recipe(**d) + async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: async with conn.execute(f''' SELECT {','.join(Recipe.KEYS)} FROM Recipe @@ -69,7 +77,7 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: LIMIT 1 ''', (recipe_id,)) as cursor: async for row in cursor: - return Recipe(**{k:v for k,v in zip(Recipe.KEYS, row)}) + return row_to_recipe(row) async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]: async with conn.execute(f''' @@ -77,4 +85,11 @@ async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient WHERE recipe_id = ? ''', (recipe_id,)) as cursor: async for row in cursor: - yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row)}) \ No newline at end of file + yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row)}) + +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: + yield row_to_recipe(row) \ No newline at end of file