-
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.
Merge pull request #4 from Esekyi/feature/search
Feature/search - Implemented Search feature ✅
- Loading branch information
Showing
16 changed files
with
189 additions
and
34 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
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,25 @@ | ||
from flask import Blueprint, request, render_template | ||
from app.services.search_service import search_recipes | ||
|
||
search_bp = Blueprint('search', __name__) | ||
|
||
|
||
@search_bp.route('/search', methods=['GET'], strict_slashes=False) | ||
def search(): | ||
query = request.args.get('q', '').strip() | ||
page = int(request.args.get('page', 1)) | ||
per_page = int(request.args.get('per_page', 10)) | ||
|
||
if not query: | ||
return render_template('search_results.html', query=query, recipes=[]) | ||
|
||
results = search_recipes(query, page, per_page) | ||
|
||
return render_template( | ||
'search_results.html', | ||
query=query, | ||
recipes=results['items'], | ||
total_pages=results['total_pages'], | ||
current_page=results['current_page'], | ||
per_page=results['per_page'], | ||
) |
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,32 @@ | ||
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 | ||
from app.services.pagination_service import paginate | ||
from sqlalchemy import or_ | ||
|
||
|
||
def search_recipes(query, page=1, per_page=10): | ||
""" | ||
Search for recipes based on a query string. | ||
The query can match recipe names, ingredients, author or categories. | ||
""" | ||
query = f"%{query.lower()}%" | ||
|
||
|
||
recipes = Recipe.query.join(Ingredient).join(Category).join(User).filter( | ||
or_( | ||
Recipe.title.ilike(query), | ||
Recipe.description.ilike(query), | ||
Ingredient.name.ilike(query), | ||
Category.name.ilike(query), | ||
User.first_name.ilike(query), | ||
User.last_name.ilike(query), | ||
User.username.ilike(query) | ||
) | ||
).distinct() | ||
|
||
|
||
return paginate(recipes.all(), page, per_page) |
This file was deleted.
Oops, something went wrong.
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
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,43 @@ | ||
{% extends "recipes/readPages/read_layout.html" %} | ||
{% block content %} | ||
<div class="container mx-auto mt-8"> | ||
<h1 class="text-3xl font-semibold mb-6 text-indigo-600">Search Results</h1> | ||
|
||
{% if query %} | ||
<p class="mb-4">Results for "<strong>{{ query }}</strong>":</p> | ||
{% else %} | ||
<p class="mb-4">No search query provided.</p> | ||
{% endif %} | ||
|
||
{% if recipes %} | ||
<ul class="space-y-4"> | ||
{% for recipe in recipes %} | ||
<li class="border-b pb-4"> | ||
<a href="{{ url_for('recipe_routes.view_recipe', recipe_id=recipe.id) }}" | ||
class="text-xl text-blue-600 hover:underline">{{ recipe.title }}</a> | ||
<p class="text-gray-700">{{ recipe.description[:150] }}...</p> | ||
<p class="text-gray-500 text-sm">By {{ recipe.author.username }} | Category: {{ recipe.category.name }}</p> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
<!-- Pagination --> | ||
<div class="flex justify-center mt-6"> | ||
<div class="inline-flex items-center space-x-4"> | ||
{% if current_page > 1 %} | ||
<a href="?q={{query}}&page={{ current_page - 1 }}&per_page={{ per_page }}" | ||
class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">Previous</a> | ||
{% endif %} | ||
<span class="text-gray-600">Page {{ current_page }} of {{ total_pages }}</span> | ||
{% if current_page < total_pages %} | ||
<a href="?q={{ query }}&page={{ current_page + 1 }}&per_page={{ per_page }}" | ||
class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">Next</a> | ||
{% endif %} | ||
</div> | ||
</div> | ||
|
||
{% else %} | ||
<p class="text-gray-500">No recipes found matching your search criteria.</p> | ||
{% endif %} | ||
</div> | ||
{% endblock %} |
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