diff --git a/main.py b/main.py index 82ba80e..0664b41 100644 --- a/main.py +++ b/main.py @@ -51,6 +51,19 @@ class ProductUrl(BaseModel): async def create_product(url: ProductUrl, conn: sqlite3.Connection = Depends(get_db)) -> product.Product: return await product.get_or_create(conn, url.url, url.tags) +@app.get("/recipes/{recipe_id}") +async def get_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_db)) -> recipe.Recipe: + r = await recipe.find_recipe_by_id(conn, recipe_id) + if not r: + return JSONResponse(status_code=404, content={'message': 'Recipe not found'}) + + r.ingredients = [] + async for ingredient in recipe.find_ingredients_by_recipe_id(conn, recipe_id): + ingredient.product = await product.find_product_by_id(conn, ingredient.product_id) + r.ingredients.append(ingredient) + + return r + @app.post('/recipes/') async def create_recipe(item: recipe.Recipe, conn: sqlite3.Connection = Depends(get_db)) -> recipe.Recipe: if not item.ingredients: diff --git a/product/__init__.py b/product/__init__.py index 1479f18..866ae37 100644 --- a/product/__init__.py +++ b/product/__init__.py @@ -1,6 +1,6 @@ import json -from product.db import Product, find_product_by_tag, find_product_by_product_id, insert_product, get_tags, add_tag +from product.db import Product, find_product_by_tag, find_product_by_product_id, insert_product, get_tags, add_tag, find_product_by_id from product.scraping import scrape_woolies_data, get_product_id, get_product_details_url from typing import List diff --git a/product/db.py b/product/db.py index b7a1ec7..aad5046 100644 --- a/product/db.py +++ b/product/db.py @@ -1,5 +1,3 @@ -import aiosqlite - from typing import List, ClassVar from pydantic import BaseModel @@ -44,6 +42,15 @@ async def find_product_by_tag(conn, tag: str) -> List[Product]: async for row in cursor: yield Product(**{k:v for k,v in zip(Product.KEYS, row)}) +async def find_product_by_id(conn, product_id: str) -> Product: + async with conn.execute(f''' + SELECT {','.join(Product.KEYS)} FROM Product + WHERE id = ? + LIMIT 1 + ''', (product_id,)) as cursor: + async for row in cursor: + return Product(**{k:v for k,v in zip(Product.KEYS, row)}) + async def find_product_by_product_id(conn, product_id: str) -> Product: async with conn.execute(f''' SELECT {','.join(Product.KEYS)} FROM Product diff --git a/recipe/__init__.py b/recipe/__init__.py index 236b905..40cb1f5 100644 --- a/recipe/__init__.py +++ b/recipe/__init__.py @@ -1,5 +1,5 @@ from product import Product -from recipe.db import Recipe, Ingredient, insert_recipe, insert_ingredient +from recipe.db import Recipe, Ingredient, insert_recipe, insert_ingredient, find_recipe_by_id, find_ingredients_by_recipe_id from recipe.scraping import scrape_recipe from ingredient_parser import parse_multiple_ingredients @@ -21,10 +21,13 @@ def parse_ingredient_from_nlp(ingredients: List[str]) -> Ingredient: quantity, unit = 1, units.ITEMS.name if ingredient.amount: amount = ingredient.amount[0] + quantity = amount.quantity if amount.quantity else 1 real_unit = units.get_unit(unit) - unit = real_unit.name if real_unit else units.ITEMS.name - quantity = amount.quantity if amount.quantity else 1 + if real_unit: + unit = real_unit.name + elif not name: + name = unit results.append(Ingredient(id=0, line=ingredient.sentence, diff --git a/recipe/db.py b/recipe/db.py index d9ff3c9..ead9d0b 100644 --- a/recipe/db.py +++ b/recipe/db.py @@ -71,7 +71,7 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: async for row in cursor: return Recipe(**{k:v for k,v in zip(Recipe.KEYS, row)}) -async def get_ingredients(conn, recipe_id: int) -> List[Ingredient]: +async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]: async with conn.execute(f''' SELECT {','.join(Ingredient.KEYS)} FROM Ingredient WHERE recipe_id = ?