def recipes(): if request.method == "POST": data = request.get_json() recipe_name = data['name'] recipe = Recipes.query.filter_by(name=recipe_name).first() if not recipe: new_recipe = Recipes(name=data['name'], prep_time=data['prep_time'], difficulty=data['difficulty'], vegetarian=data['vegetarian'], rating=data['rating']) new_recipe.rating_numbering = 1 new_recipe.save() response = jsonify({ 'id': new_recipe.id, 'name': new_recipe.name, 'prep_time': new_recipe.prep_time, 'difficulty': new_recipe.difficulty, 'vegetarian': new_recipe.vegetarian, 'rating': new_recipe.rating }) response.status_code = 201 return response else: response = jsonify({'message': 'recipie already exist'}) response.status_code = 304 return response else: # GET recipes = Recipes.get_all() results = [] for recipe in recipes: obj = { 'id': recipe.id, 'name': recipe.name, 'prep_time': recipe.prep_time, 'difficulty': recipe.difficulty, 'vegetarian': recipe.vegetarian } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def get(self, user_in_session, category_id): # This method will retrieve all the recipes under the category given """ Retrieve recipes --- tags: - Recipes Endpoints parameters: - in: path name: category_id description: category id type: integer required: true - in: query name: q description: Search parameter type: string - in: query name: page description: page number type: integer default: 1 - in: query name: limit description: Limit type: integer default: 10 responses: 201: description: Category created successfully 400: description: Invalid page number or limit 401: description: Recipe not found 422: description: Please fill all the fields """ page_number = request.args.get("page", default=1, type=int) no_items_per_page = request.args.get("limit", default=8, type=int) search_recipe = request.args.get('q') # These are the arguments to be provided in the url check_category_exists = Categories.query.filter_by( id=category_id).first() category_recipes = Recipes.get_all(category_id, user_in_session).\ paginate(page_number, no_items_per_page, error_out=True) # This retrieves all the recipes belonging to a specific category. recipe_list = [] if check_category_exists: if search_recipe: # This filters the recipes when the user provides the search parameters q. search_recipes = Recipes.query.filter(Recipes.users_id == user_in_session, Recipes.recipe_name.ilike('%' + search_recipe + '%')). \ paginate(page_number, no_items_per_page, error_out=False) for recipes in search_recipes.items: # This loops retrieves all the recipes that match the search string. searched_recipes = { 'id': recipes.id, 'recipe_name': recipes.recipe_name, 'recipe_description': recipes.recipe_description, 'category_id': recipes.category_id, 'date_created': recipes.created_at, 'date_updated': recipes.updated_at } recipe_list.append(searched_recipes) if len(recipe_list) <= 0: # This checks whether the searched recipe list contains data. return make_response( jsonify({'message': "Recipe does not exist."})), 404 list_of_recipes = { 'recipes': recipe_list, "total_items": search_recipes.total, "total_pages": search_recipes.pages, "current_page": search_recipes.page } return make_response(jsonify(list_of_recipes)), 200 else: for recipes in category_recipes.items: # This loops all the recipes of the category with no search parameters. all_recipes = { 'id': recipes.id, 'recipe_name': recipes.recipe_name, 'recipe_description': recipes.recipe_description, 'category_id': recipes.category_id, 'date_created': recipes.created_at, 'date_updated': recipes.updated_at, 'category_name': check_category_exists.category_name } recipe_list.append(all_recipes) if len(recipe_list) <= 0: return make_response( jsonify({'message': "No recipes for this category."})), 402 list_of_recipes = { 'recipes': recipe_list, "total_items": category_recipes.total, "total_pages": category_recipes.pages, "current_page": category_recipes.page } return make_response(jsonify(list_of_recipes)), 200 return make_response(jsonify({'message': "Category does not exist."})), 406