Commit be846ee3 authored by Arhan Banerjee's avatar Arhan Banerjee
Browse files

Updated comments

parent 12d7faf3
......@@ -3,7 +3,7 @@ from datetime import datetime
# Replace with own host and username/password
db = mysql.connect(
host = "marmoset03.shoshin.uwaterloo.ca",
user = "",
......@@ -13,14 +13,13 @@ db = mysql.connect(
cursor = db.cursor()
# Replace with own local database where relevant tables exist
cursor.execute("USE db356_a39baner;")
# Get max Recipe ID
# if param is 0, then ask user for which recipe/review
# Splits user input into appropriate format for ingredients and tags
def splitter(input_string, tag):
temp_list = input_string.split(",")
......@@ -33,6 +32,7 @@ def splitter(input_string, tag):
return output_list
# Creates recipe based on user input (only available to members)
def create_recipe(user_id):
global max_recipe_id
global max_ingredient_id
......@@ -111,7 +111,7 @@ def create_recipe(user_id):
db.commit()
print("\nRecipe created\n")
# Creates review based on user input (only available to members)
def create_review(user_id):
# user can only leave one review per recipe?
......@@ -123,11 +123,12 @@ def create_review(user_id):
review_text = input("Enter any comments about the recipe: ")
sql_create_review_command = "insert into interactions (user_id, recipe_id, date, rating, review) values (" + contributor + ", " + recipe_id + ", " + "'" + date_submitted + "'" + ", " + rating + ", " + "'" + review_text +"'" + ");"
print(sql_create_review_command)
#print(sql_create_review_command)
cursor.execute(sql_create_review_command)
db.commit()
print("\nReview created\n")
# Edits recipe's attributes based on user input (only available to members who initially created the recipe)
def edit_recipe(user_id):
search_command = ""
......@@ -286,6 +287,7 @@ def edit_recipe(user_id):
db.commit()
# Edits review's attributes based on user input (only available to members who initially created the review)
def edit_review(user_id):
......@@ -324,6 +326,7 @@ def edit_review(user_id):
else:
print("\nError: attribute not found\n")
# Deletes recipe based on user input (only available to members who initially created the recipe)
def delete_recipe(user_id):
recipe_id = input("Enter the Recipe ID of the recipe to be deleted: ")
......@@ -352,7 +355,7 @@ def delete_recipe(user_id):
cursor.execute(delete)
db.commit()
# Deletes review based on user input (only available to members who initially created the review)
def delete_review(user_id):
recipe_id = input("Enter the Recipe ID of the recipe from which the review is to be deleted: ")
......@@ -375,6 +378,7 @@ def delete_review(user_id):
print("Deleting")
db.commit()
# Search by attributes in database
def search():
search_command = ""
......@@ -382,7 +386,7 @@ def search():
search_command = input("What attribute would you like to search by? (Press 'help' for list of search commands or 'End search' to exit): ")
if(search_command == "help"):
print("\nRecipeID\nUserID\nRecipe Name\nMinutes\nTags\nCalories\nFat\nSugar\nSodium\nProtein\nSaturated Fat\nCarbs\nSteps Count\nIngredients\nIngredients Count\nRating\nUser Interactions\n")
print("\nRecipeID\nUserID\nRecipe Name\nMinutes\nTags\nCalories\nFat\nSugar\nSodium\nProtein\nSaturated Fat\nCarbs\nSteps Count\nIngredients\nMinimum Rating\nUser Interactions\n")
elif(search_command == "Recipe Name"):
search_attribute = input("Enter part or full recipe name to search by: ")
sql_command = "SELECT * FROM recipes WHERE Name LIKE '%" + search_attribute + "%';"
......@@ -449,12 +453,15 @@ def search():
sql_command = "WITH tagIds AS (SELECT tag_id FROM tag WHERE name IN " + tag_list_string + ") SELECT recipes.* FROM recipes INNER JOIN recipe_tag USING(recipe_id) INNER JOIN tagIds USING(tag_id);"
#print(sql_command)
sql_command = "WITH recipeIds AS (select distinct recipe_id from recipe_tag inner join tag using (tag_id) WHERE name IN " + tag_list_string + "),"\
"tag_join as (select recipe_id, tag.name as 'tag_name' from recipes inner join recipe_tag using (recipe_id) inner join tag using(tag_id) inner join recipeIds using (recipe_id)), "\
"ingredient_join as (select recipe_id, ingredients.name as 'ingredient_name' from recipes inner join recipe_ingredients using (recipe_id) inner join ingredients using(ingredient_id) inner join recipeIds using (recipe_id))"\
"select recipes.*, group_concat(distinct ingredient_name separator ', '), group_concat(distinct tag_name separator ', ') from recipes inner join tag_join using(recipe_id) inner join ingredient_join using (recipe_id) group by recipe_id;"
cursor.execute(sql_command)
tables = cursor.fetchall()
print(len(tables))
if (len(tables) != 0):
for entry in tables:
print("\n***************\n")
......@@ -464,15 +471,13 @@ def search():
print("Contributor ID: " + str(entry[3]))
print("\nSteps: " + entry[13])
print("\nDescription: " + entry[14])
print("Ingredients: " + entry[15])
print("\nTags: " + entry[16])
print("\n***************\n")
else:
print("\n***************\n")
print("No recipes found")
print("\n***************\n")
# SQL code
elif(search_command == "Calories"):
search_attribute = input("Enter max calorie count to search by: ")
......@@ -705,32 +710,12 @@ def search():
#print(ingredient_list)
ingredients_list_string = "('" + "', '".join(ingredient_list) + "')"
sql_command = "WITH ingredientIds AS ( SELECT ingredient_id FROM ingredients WHERE name IN " + ingredients_list_string + " ) SELECT recipes.* FROM recipes INNER JOIN recipe_ingredients USING(recipe_id) INNER JOIN ingredientIds USING(ingredient_id);"
#print(sql_command)
cursor.execute(sql_command)
tables = cursor.fetchall()
if (len(tables) != 0):
for entry in tables:
print("\n***************\n")
print("Recipe Name: " + entry[0])
print("Recipe ID: " + str(entry[1]))
print("Minutes Taken: " + str(entry[2]))
print("Contributor ID: " + str(entry[3]))
print("\nSteps: " + entry[13])
print("\nDescription: " + entry[14])
print("\n***************\n")
else:
print("\n***************\n")
print("No recipes found")
print("\n***************\n")
# SQL code
elif(search_command == "Ingredients Count"):
search_attribute = input("Enter max number of ingredients to search by: ")
sql_command = "select distinct recipes.* from recipes inner join recipe_ingredients using (recipe_id) group by recipe_id having count(*) < " + search_attribute + ";"
#print(sql_command)
sql_command = "WITH recipeIds AS (select distinct recipe_id from recipe_ingredients inner join ingredients using (ingredient_id) WHERE name IN " + ingredients_list_string + "),"\
"tag_join as (select recipe_id, tag.name as 'tag_name' from recipes inner join recipe_tag using (recipe_id) inner join tag using(tag_id) inner join recipeIds using (recipe_id)), "\
"ingredient_join as (select recipe_id, ingredients.name as 'ingredient_name' from recipes inner join recipe_ingredients using (recipe_id) inner join ingredients using(ingredient_id) inner join recipeIds using (recipe_id))"\
"select recipes.*, group_concat(distinct ingredient_name separator ', '), group_concat(distinct tag_name separator ', ') from recipes inner join tag_join using(recipe_id) inner join ingredient_join using (recipe_id) group by recipe_id;"
cursor.execute(sql_command)
tables = cursor.fetchall()
......@@ -743,16 +728,25 @@ def search():
print("Contributor ID: " + str(entry[3]))
print("\nSteps: " + entry[13])
print("\nDescription: " + entry[14])
print("Ingredients: " + entry[15])
print("\nTags: " + entry[16])
print("\n***************\n")
else:
print("\n***************\n")
print("No recipes found")
print("\n***************\n")
# SQL code
elif(search_command == "Minimum Rating"):
search_attribute = input("Enter minimum rating to search by: ")
sql_command = "SELECT DISTINCT recipes.* FROM recipes INNER JOIN interactions USING(recipe_id) WHERE rating >= " + search_attribute + ";"
#sql_command = "SELECT DISTINCT recipes.* FROM recipes INNER JOIN interactions USING(recipe_id) WHERE rating >= " + search_attribute + ";"
sql_command = "WITH recipeIds AS (select distinct recipe_id from interactions where rating >= " + search_attribute + "),"\
"tag_join as (select recipe_id, tag.name as 'tag_name' from recipes inner join recipe_tag using (recipe_id) inner join tag using(tag_id) inner join recipeIds using (recipe_id)), "\
"ingredient_join as (select recipe_id, ingredients.name as 'ingredient_name' from recipes inner join recipe_ingredients using (recipe_id) inner join ingredients using(ingredient_id) inner join recipeIds using (recipe_id))"\
"select recipes.*, group_concat(distinct ingredient_name separator ', '), group_concat(distinct tag_name separator ', ') from recipes inner join tag_join using(recipe_id) inner join ingredient_join using (recipe_id) group by recipe_id;"
#print(sql_command)
cursor.execute(sql_command)
......@@ -766,6 +760,8 @@ def search():
print("Contributor ID: " + str(entry[3]))
print("\nSteps: " + entry[13])
print("\nDescription: " + entry[14])
print("Ingredients: " + entry[15])
print("\nTags: " + entry[16])
print("\n***************\n")
else:
print("\n***************\n")
......@@ -857,13 +853,7 @@ def search():
elif(search_command == "End search"):
continue
else:
print("\nError: attribute not found\n")
print("\nError: attribute not found\n")
def main():
print('''
......@@ -964,9 +954,4 @@ def main():
else:
print("Error: Not a valid command")
main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment