Skip to content

Commit

Permalink
Recipe Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TangMartin committed Dec 4, 2021
1 parent 9b8f6f6 commit a237e11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 114 deletions.
116 changes: 6 additions & 110 deletions backend/app/indexer/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,116 +336,12 @@ def get_recipe_by_id(conn, cursor, recipe_id):
print("MYSQL ERROR:", sql)
logging.error(e)

def get_createdrecipe_by_userid(conn, cursor, user_id):
'''
Returns the available recipe details given a recipe_id
Note: macros may still be empty if they were not added
to the recipe or corresponding ingredients
'''
sql = 'getCreatedRecipeById'
try:
cursor.callproc(sql, (user_id, ))
raw_result = cursor.fetchall()
res = {
"recipe_id": "",
"name": "",
"recipe_description": "",
"user_id": user_id,
"creator_username": "",
"protein": 0,
"carbs": 0,
"fat": 0,
"fiber": 0,
"calories": 0,
"servings": 0,
"vegetarian": False,
"vegan": False,
"cooking_time": 0,
"steps": [],
"ingredients": []
}

if len(raw_result):
res["name"] = raw_result[0][1]
res["recipe_description"] = raw_result[0][2]
res["user_id"] = raw_result[0][3]
res["creator_username"] = raw_result[0][4]
res["protein"] = raw_result[0][5]
res["carbs"] = raw_result[0][6]
res["fat"] = raw_result[0][7]
res["fiber"] = raw_result[0][8]
res["calories"] = raw_result[0][9]
res["servings"] = raw_result[0][10]
res["vegetarian"] = bool(raw_result[0][11])
res["vegan"] = bool(raw_result[0][12])
res["cooking_time"] = raw_result[0][13]

step_ids = set()
ingredient_ids = set()

for result in raw_result:
if result[14] not in step_ids:
res["steps"].append(
{
"step_id": result[14],
"description": result[15],
"time": result[16],
}
)
step_ids.add(result[14])

if result[17] not in ingredient_ids:
res["ingredients"].append(
{
"ingredient_id": result[17],
"ingredient_name": result[18],
"category": result[19]
}
)
ingredient_ids.add(result[17])

res["steps"] = sorted(res["steps"], key=lambda step: step["step_id"])
res["ingredients"] = sorted(res["ingredients"], key=lambda ingredient: ingredient["ingredient_id"])

if is_missing_macros(res):
ingredient_macros = get_ingredient_macros(conn, cursor, res["ingredients"])

# Make sure the values for macros are not None so we can
# add, and to fit our model
res["protein"] = 0
res["carbs"] = 0
res["fat"] = 0
res["fiber"] = 0
res["calories"] = 0

# Do something with the macros, assuming they do not
# need to be scaled, i.e. they have proper macro values
# according to the recipe
for ingredient_macro in ingredient_macros:
res["protein"] += ingredient_macro["protein"]
res["carbs"] += ingredient_macro["carbs"]
res["fat"] += ingredient_macro["fat"]
res["fiber"] += ingredient_macro["fiber"]
res["calories"] += ingredient_macro["calories"]
def get_createdrecipe_by_userid(cursor, user_id):
sql_proc = 'getCreatedRecipeById'

# update recipe macros so we don't need to repeat this
# multiple times for the same recipe
cursor.close()
new_cursor = conn.cursor()
new_sql = 'updateRecipeMacros'
new_cursor.callproc(new_sql,
(
res["recipe_id"],
res["protein"],
res["carbs"],
res["fat"],
res["fiber"],
res["calories"]
)
)
conn.commit()

return res
try:
cursor.callproc(sql_proc, (user_id, ))
return cursor.fetchall()
except Exception as e:
print("MYSQL ERROR:", sql)
print("MYSQL ERROR:", sql_proc)
logging.error(e)
2 changes: 1 addition & 1 deletion backend/app/route/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async def read_recipe_by_id(recipe_id: str = ""):
"status_code": 400
}

@router.get("/{user_id}", response_model=RecipeDetailsOut)
@router.get("/{user_id}")
async def read_createdrecipe_by_userid(user_id: str=''):
'''get recipe info, macros, steps, and ingredients'''
try:
Expand Down
4 changes: 1 addition & 3 deletions database/sproc/recipeSproc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ USE `umami_db`$$
CREATE PROCEDURE `getCreatedRecipeById` (IN `_user_id` VARCHAR(255))
BEGIN

SELECT r.*, i.ingredient_id, i.ingredient_name, i.category
SELECT r.*
FROM `recipes_table` r
JOIN `recipe_steps_table` rs ON r.recipe_id = rs.recipe_id
JOIN `ingredients_table` i ON r.recipe_id = i.recipe_id
WHERE r.user_id = `_user_id`
;

Expand Down

0 comments on commit a237e11

Please sign in to comment.