-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Updates📢] Implemented a search recipe service
- Loading branch information
[esekyi]
committed
Sep 7, 2024
1 parent
1110fc4
commit d1b88b0
Showing
3 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |