diff --git a/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc b/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc index 686a6ce..5eacd7c 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 d3a72d3..bac3e15 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 0c03116..0284e1d 100644 --- a/scripts/recipes/database_recipes.py +++ b/scripts/recipes/database_recipes.py @@ -132,11 +132,27 @@ def postUpdateRecipe(site:str, payload:tuple, convert:bool=True): with psycopg2.connect(**database_config) as conn: with conn.cursor() as cur: set_clause, values = postsqldb.updateStringFactory(payload['update']) - with open("scripts/recipes/sql/postRecipeUpdate.sql") as file: + with open("scripts/recipes/sql/postUpdateRecipe.sql") as file: sql = file.read().replace("%%site_name%%", site).replace("%%set_clause%%", set_clause) values.append(payload['id']) cur.execute(sql, values) + rows = cur.fetchone() + if rows and convert: + updated = postsqldb.tupleDictionaryFactory(cur.description, rows) + elif rows and not convert: + updated = rows + return updated +def postUpdateRecipeItem(site:str, payload:tuple, convert:bool=True): + database_config = config.config() + updated = () + with psycopg2.connect(**database_config) as conn: + with conn.cursor() as cur: + set_clause, values = postsqldb.updateStringFactory(payload['update']) + with open("scripts/recipes/sql/postUpdateRecipeItem.sql") as file: + sql = file.read().replace("%%site_name%%", site).replace("%%set_clause%%", set_clause) + values.append(payload['id']) + cur.execute(sql, values) rows = cur.fetchone() if rows and convert: updated = postsqldb.tupleDictionaryFactory(cur.description, rows) diff --git a/scripts/recipes/recipes_api.py b/scripts/recipes/recipes_api.py index 5b8e208..b047a18 100644 --- a/scripts/recipes/recipes_api.py +++ b/scripts/recipes/recipes_api.py @@ -285,15 +285,21 @@ def deleteRecipeItem(): return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} is not allowed!'}) @recipes_api.route('/recipe/saveRecipeItem', methods=["POST"]) +@login_required def saveRecipeItem(): + """ post an update to a recipe item in the database by passing the recipe item ID and an update + payload. + --- + responses: + 200: + description: recipe item updated successfully. + """ recipe = {} if request.method == "POST": id = int(request.get_json()['id']) update = request.get_json()['update'] - database_config = config() site_name = session['selected_site'] - with psycopg2.connect(**database_config) as conn: - updated_line = postsqldb.RecipesTable.update_item_tuple(conn, site_name, {'id': id, 'update': update}, convert=True) - recipe = postsqldb.RecipesTable.getRecipe(conn, site_name, (int(updated_line['rp_id']), ), convert=True) + updated_line = database_recipes.postUpdateRecipeItem(site_name, {'id': id, 'update': update}) + recipe = database_recipes.getRecipe(site_name, (int(updated_line['rp_id']), )) return jsonify({'recipe': recipe, 'error': False, 'message': f'Recipe Item {updated_line['item_name']} was updated successful!'}) - return jsonify({'recipe': recipe, 'error': True, 'message': 'Recipe Item was not updated unsuccessful!'}) \ No newline at end of file + return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} not allowed!'}) \ No newline at end of file diff --git a/scripts/recipes/sql/postRecipeUpdate.sql b/scripts/recipes/sql/postUpdateRecipe.sql similarity index 100% rename from scripts/recipes/sql/postRecipeUpdate.sql rename to scripts/recipes/sql/postUpdateRecipe.sql diff --git a/scripts/recipes/sql/postUpdateRecipeItem.sql b/scripts/recipes/sql/postUpdateRecipeItem.sql new file mode 100644 index 0000000..2d75227 --- /dev/null +++ b/scripts/recipes/sql/postUpdateRecipeItem.sql @@ -0,0 +1 @@ +UPDATE %%site_name%%_recipe_items SET %%set_clause%% WHERE id=%s RETURNING *; \ No newline at end of file