recipes_api.getRecipe view updated for new schema
This commit is contained in:
parent
df5b575e10
commit
74b05433e0
Binary file not shown.
Binary file not shown.
@ -47,6 +47,24 @@ def getRecipes(site:str, payload:tuple, convert=True):
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
return recordset, count
|
||||
|
||||
def getRecipe(site, payload:tuple, convert=True):
|
||||
database_config = config.config()
|
||||
try:
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
with open(f"scripts/recipes/sql/getRecipeByID.sql", "r+") as file:
|
||||
sql = file.read().replace("%%site_name%%", site)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
rows = cur.fetchone()
|
||||
if rows and convert:
|
||||
record = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||
if rows and not convert:
|
||||
record = rows
|
||||
return record
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
|
||||
def postRecipeUpdate(site, payload, convert=True):
|
||||
database_config = config.config()
|
||||
updated = ()
|
||||
|
||||
@ -55,7 +55,6 @@ def recipe(id, mode='view'):
|
||||
if mode == "view":
|
||||
return render_template("recipes/recipe_view.html", recipe_id=id, current_site=session['selected_site'])
|
||||
|
||||
|
||||
@recipes_api.route('/recipes/getRecipes', methods=["GET"])
|
||||
def getRecipes():
|
||||
""" Get a subquery of recipes from the database by passing a page, limit
|
||||
@ -77,14 +76,20 @@ def getRecipes():
|
||||
|
||||
@recipes_api.route('/recipe/getRecipe', methods=["GET"])
|
||||
def getRecipe():
|
||||
""" Get a query for recipe id from database by passing an id
|
||||
---
|
||||
responses:
|
||||
200:
|
||||
description: id queried successfully!
|
||||
|
||||
"""
|
||||
recipe = {}
|
||||
if request.method == "GET":
|
||||
id = int(request.args.get('id', 1))
|
||||
database_config = config()
|
||||
site_name = session['selected_site']
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
recipe = postsqldb.RecipesTable.getRecipe(conn, site_name, (id,), convert=True)
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'bleh'})
|
||||
recipe = database_recipes.getRecipe(site_name, (id,), convert=True)
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe returned successfully!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} not allowed'})
|
||||
|
||||
@recipes_api.route('/recipes/addRecipe', methods=["POST"])
|
||||
def addRecipe():
|
||||
|
||||
27
scripts/recipes/sql/getRecipeByID.sql
Normal file
27
scripts/recipes/sql/getRecipeByID.sql
Normal file
@ -0,0 +1,27 @@
|
||||
WITH passed_id AS (SELECT %s AS passed_id),
|
||||
cte_recipe_items AS (
|
||||
SELECT items.*,
|
||||
COALESCE(%%site_name%%_items.barcode, items.uuid) AS uuid,
|
||||
(SELECT COALESCE(row_to_json(units.*), '{}') FROM units WHERE units.id=%%site_name%%_item_info.uom) AS item_uom,
|
||||
COALESCE(%%site_name%%_items.item_name, items.item_name) AS item_name,
|
||||
COALESCE(%%site_name%%_items.links, items.links) AS links,
|
||||
row_to_json(units.*) as uom,
|
||||
(SELECT COALESCE(array_agg(jsonb_build_object('conversion', conv, 'unit', units)), '{}')
|
||||
FROM %%site_name%%_conversions conv
|
||||
LEFT JOIN units ON conv.uom_id = units.id
|
||||
WHERE conv.item_id = %%site_name%%_items.id) AS conversions
|
||||
FROM %%site_name%%_recipe_items items
|
||||
LEFT JOIN %%site_name%%_items ON items.item_id = %%site_name%%_items.id
|
||||
LEFT JOIN %%site_name%%_item_info ON %%site_name%%_items.item_info_id = %%site_name%%_item_info.id
|
||||
LEFT JOIN units ON units.id = items.uom
|
||||
WHERE items.rp_id = (SELECT passed_id FROM passed_id)
|
||||
ORDER BY items.item_name ASC
|
||||
)
|
||||
|
||||
SELECT (SELECT passed_id FROM passed_id) AS passed_id,
|
||||
%%site_name%%_recipes.*,
|
||||
logins.username as author,
|
||||
(SELECT COALESCE(array_agg(row_to_json(ris)), '{}') FROM cte_recipe_items ris) AS recipe_items
|
||||
FROM %%site_name%%_recipes
|
||||
JOIN logins ON %%site_name%%_recipes.author = logins.id
|
||||
WHERE %%site_name%%_recipes.id=(SELECT passed_id FROM passed_id)
|
||||
Loading…
x
Reference in New Issue
Block a user