Load products
This commit is contained in:
parent
bff5607381
commit
a873dbb661
5 changed files with 30 additions and 7 deletions
13
main.py
13
main.py
|
|
@ -51,6 +51,19 @@ class ProductUrl(BaseModel):
|
||||||
async def create_product(url: ProductUrl, conn: sqlite3.Connection = Depends(get_db)) -> product.Product:
|
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)
|
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/')
|
@app.post('/recipes/')
|
||||||
async def create_recipe(item: recipe.Recipe, conn: sqlite3.Connection = Depends(get_db)) -> recipe.Recipe:
|
async def create_recipe(item: recipe.Recipe, conn: sqlite3.Connection = Depends(get_db)) -> recipe.Recipe:
|
||||||
if not item.ingredients:
|
if not item.ingredients:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import json
|
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 product.scraping import scrape_woolies_data, get_product_id, get_product_details_url
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import aiosqlite
|
|
||||||
|
|
||||||
from typing import List, ClassVar
|
from typing import List, ClassVar
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
@ -44,6 +42,15 @@ async def find_product_by_tag(conn, tag: str) -> List[Product]:
|
||||||
async for row in cursor:
|
async for row in cursor:
|
||||||
yield Product(**{k:v for k,v in zip(Product.KEYS, row)})
|
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 def find_product_by_product_id(conn, product_id: str) -> Product:
|
||||||
async with conn.execute(f'''
|
async with conn.execute(f'''
|
||||||
SELECT {','.join(Product.KEYS)} FROM Product
|
SELECT {','.join(Product.KEYS)} FROM Product
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from product import Product
|
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 recipe.scraping import scrape_recipe
|
||||||
|
|
||||||
from ingredient_parser import parse_multiple_ingredients
|
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
|
quantity, unit = 1, units.ITEMS.name
|
||||||
if ingredient.amount:
|
if ingredient.amount:
|
||||||
amount = ingredient.amount[0]
|
amount = ingredient.amount[0]
|
||||||
|
quantity = amount.quantity if amount.quantity else 1
|
||||||
|
|
||||||
real_unit = units.get_unit(unit)
|
real_unit = units.get_unit(unit)
|
||||||
unit = real_unit.name if real_unit else units.ITEMS.name
|
if real_unit:
|
||||||
quantity = amount.quantity if amount.quantity else 1
|
unit = real_unit.name
|
||||||
|
elif not name:
|
||||||
|
name = unit
|
||||||
|
|
||||||
results.append(Ingredient(id=0,
|
results.append(Ingredient(id=0,
|
||||||
line=ingredient.sentence,
|
line=ingredient.sentence,
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe:
|
||||||
async for row in cursor:
|
async for row in cursor:
|
||||||
return Recipe(**{k:v for k,v in zip(Recipe.KEYS, row)})
|
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'''
|
async with conn.execute(f'''
|
||||||
SELECT {','.join(Ingredient.KEYS)} FROM Ingredient
|
SELECT {','.join(Ingredient.KEYS)} FROM Ingredient
|
||||||
WHERE recipe_id = ?
|
WHERE recipe_id = ?
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue