diff --git a/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc b/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc index 7046928..832ef5d 100644 Binary files a/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc and b/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc differ diff --git a/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc b/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc index 3277892..66bf535 100644 Binary files a/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc and b/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc differ diff --git a/scripts/recipes/database_recipes.py b/scripts/recipes/database_recipes.py index 7b3f7bc..7581761 100644 --- a/scripts/recipes/database_recipes.py +++ b/scripts/recipes/database_recipes.py @@ -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 = () diff --git a/scripts/recipes/recipes_api.py b/scripts/recipes/recipes_api.py index 2629590..da215a3 100644 --- a/scripts/recipes/recipes_api.py +++ b/scripts/recipes/recipes_api.py @@ -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(): diff --git a/scripts/recipes/sql/getRecipeByID.sql b/scripts/recipes/sql/getRecipeByID.sql new file mode 100644 index 0000000..88982b8 --- /dev/null +++ b/scripts/recipes/sql/getRecipeByID.sql @@ -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) \ No newline at end of file