diff --git a/app/config.py b/app/config.py index d420455..5797451 100644 --- a/app/config.py +++ b/app/config.py @@ -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 diff --git a/app/routes/recipe_routes.py b/app/routes/recipe_routes.py index 3ec4be7..ee3be11 100644 --- a/app/routes/recipe_routes.py +++ b/app/routes/recipe_routes.py @@ -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')) @@ -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() diff --git a/app/services/search_service.py b/app/services/search_service.py new file mode 100644 index 0000000..2d0b97d --- /dev/null +++ b/app/services/search_service.py @@ -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)