recipes_api.addRecipe updated to new schema
This commit is contained in:
parent
74b05433e0
commit
e964bb7bd5
Binary file not shown.
Binary file not shown.
@ -64,6 +64,23 @@ def getRecipe(site, payload:tuple, convert=True):
|
|||||||
except (Exception, psycopg2.DatabaseError) as error:
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, sql)
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
|
def postRecipe(site:str, payload:tuple, convert=True):
|
||||||
|
database_config = config.config()
|
||||||
|
record = ()
|
||||||
|
with open("scripts/recipes/sql/postRecipe.sql") as file:
|
||||||
|
sql = file.read().replace("%%site_name%%", site)
|
||||||
|
try:
|
||||||
|
with psycopg2.connect(**database_config) as conn:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(sql, payload)
|
||||||
|
rows = cur.fetchone()
|
||||||
|
if rows and convert:
|
||||||
|
record = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||||
|
elif 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):
|
def postRecipeUpdate(site, payload, convert=True):
|
||||||
database_config = config.config()
|
database_config = config.config()
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import os
|
|||||||
import postsqldb, webpush
|
import postsqldb, webpush
|
||||||
from flasgger import swag_from
|
from flasgger import swag_from
|
||||||
from scripts.recipes import database_recipes
|
from scripts.recipes import database_recipes
|
||||||
|
from scripts import postsqldb as db
|
||||||
from flask_restx import Api, fields
|
from flask_restx import Api, fields
|
||||||
|
|
||||||
recipes_api = Blueprint('recipes_api', __name__)
|
recipes_api = Blueprint('recipes_api', __name__)
|
||||||
@ -93,6 +94,12 @@ def getRecipe():
|
|||||||
|
|
||||||
@recipes_api.route('/recipes/addRecipe', methods=["POST"])
|
@recipes_api.route('/recipes/addRecipe', methods=["POST"])
|
||||||
def addRecipe():
|
def addRecipe():
|
||||||
|
""" post a new recipe into the database by passing a recipe_name and recipe description
|
||||||
|
---
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Recipe was added successfully into the system
|
||||||
|
"""
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
recipe_name = request.get_json()['recipe_name']
|
recipe_name = request.get_json()['recipe_name']
|
||||||
recipe_description = request.get_json()['recipe_description']
|
recipe_description = request.get_json()['recipe_description']
|
||||||
@ -100,15 +107,15 @@ def addRecipe():
|
|||||||
site_name = session['selected_site']
|
site_name = session['selected_site']
|
||||||
user_id = session['user_id']
|
user_id = session['user_id']
|
||||||
with psycopg2.connect(**database_config) as conn:
|
with psycopg2.connect(**database_config) as conn:
|
||||||
recipe = postsqldb.RecipesTable.Payload(
|
recipe = db.RecipesTable.Payload(
|
||||||
name=recipe_name,
|
name=recipe_name,
|
||||||
author=user_id,
|
author=user_id,
|
||||||
description=recipe_description
|
description=recipe_description
|
||||||
)
|
)
|
||||||
recipe = postsqldb.RecipesTable.insert_tuple(conn, site_name, recipe.payload())
|
recipe = database_recipes.postRecipe(site_name, recipe.payload())
|
||||||
webpush.push_ntfy('New Recipe', f"New Recipe added to {site_name}; {recipe_name}!{recipe_description} http://test.treehousefullofstars.com/recipe/view/{recipe['id']} http://test.treehousefullofstars.com/recipe/edit/{recipe['id']}")
|
webpush.push_ntfy('New Recipe', f"New Recipe added to {site_name}; {recipe_name}! {recipe_description} \n http://test.treehousefullofstars.com/recipe/view/{recipe['id']} \n http://test.treehousefullofstars.com/recipe/edit/{recipe['id']}")
|
||||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Add Recipe successful!'})
|
return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe added successful!'})
|
||||||
return jsonify({'recipe': recipe, 'error': True, 'message': 'Add Recipe unsuccessful!'})
|
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method}'})
|
||||||
|
|
||||||
@recipes_api.route('/recipe/getItems', methods=["GET"])
|
@recipes_api.route('/recipe/getItems', methods=["GET"])
|
||||||
def getItems():
|
def getItems():
|
||||||
|
|||||||
4
scripts/recipes/sql/postRecipe.sql
Normal file
4
scripts/recipes/sql/postRecipe.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
INSERT INTO %%site_name%%_recipes
|
||||||
|
(name, author, description, creation_date, instructions, picture_path)
|
||||||
|
VALUES (%s, %s, %s, %s, %s, %s)
|
||||||
|
RETURNING *;
|
||||||
Loading…
x
Reference in New Issue
Block a user