Skip to content

Commit

Permalink
[Updates📢] Implemented a search recipe service
Browse files Browse the repository at this point in the history
  • Loading branch information
[esekyi] committed Sep 7, 2024
1 parent 1110fc4 commit d1b88b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 0 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Config:
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = True
REMEMBER_COOKIE_DURATION = timedelta(days=14)
SERVER_NAME = '127.0.0.1:5000'


# AWS S3 configuration
Expand Down
9 changes: 7 additions & 2 deletions app/routes/recipe_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ def add_recipe():
except Exception as e:
# Rollback db session in case of an error
db.session.rollback()
flash(
f"An error occurred while creating the recipe: {str(e)}", "error")
flash(f"An error occurred while creating the recipe: {str(e)}", "error")
# If recipe creation fails, delete the uploaded image if it was uploaded
if image_url:
delete_image_from_s3(image_url) # delete the image if it was uploaded
return redirect(url_for('recipe_routes.add_recipe'))


Expand Down Expand Up @@ -140,6 +142,9 @@ def edit_recipe(recipe_id):
# Rollback db session in case of an error
db.session.rollback()
flash(f"An error occurred while updating the recipe: {str(e)}", "error")
# delete the image if it was uploaded
if image_url:
delete_image_from_s3(image_url)
return redirect(url_for('recipe_routes.edit_recipe', recipe_id=recipe_id))

categories = CategoryService.get_all_categories()
Expand Down
42 changes: 42 additions & 0 deletions app/services/search_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from app import db
from app.models.recipe import Recipe
from app.models.category import Category
from app.models.ingredient import Ingredient
from app.models.instruction import Instruction
from app.models.user import User


def search_recipes(query):
"""
Search for recipes based on a query string.
The query can match recipe names, ingredients, author or categories.
"""
query = f"%{query.lower()}%"

# Search by title
recipes_by_title = Recipe.query.filter(Recipe.title.ilike(query)).all()

# Search by description
recipes_by_description = Recipe.query.filter(
Recipe.description.ilike(query)).all()

# Search by ingredients
recipes_by_ingredient = db.session.query(Recipe).join(
Ingredient).filter(Ingredient.name.ilike(query)).all()

# Search by category name
recipes_by_category = db.session.query(Recipe).join(
Category).filter(Category.name.ilike(query)).all()

# Search by author (first name, last name or username)
author_recipes = db.session.query(Recipe).join(User).filter(
(User.first_name.ilike(query)) |
(User.last_name.ilike(query)) |
(User.username.ilike(query))
).all()

# calling a set function on it to remove duplicates
all_recipes = set(recipes_by_title + recipes_by_category +
recipes_by_description + recipes_by_ingredient + author_recipes)

return list(all_recipes)

0 comments on commit d1b88b0

Please sign in to comment.