def search_questions(): data = request.get_json() search_term = data.get('searchTerm', '') # Return 422 status code if empty search term is sent if search_term == '': abort(422) try: # get all questions that has the search term substring questions = Question.query.filter( Question.question.ilike(f'%{search_term}%')).all() # if there are no questions for search term return 404 if len(questions) == 0: abort(404) # paginate questions paginated_questions = get_paginated_questions( request, questions, QUESTIONS_PER_PAGE) # return response if successful return jsonify({ 'success': True, 'questions': paginated_questions, 'total_questions': len(Question.query.all()) }), 200 except Exception: # This error code is returned when 404 abort # raises exception from try block abort(404)
def get_questions(): questions = Question.query.order_by(Question.id).all() total_questions = len(questions) categories = Category.query.order_by(Category.id).all() # Get paginated questions current_questions = get_paginated_questions(request, questions, QUESTIONS_PER_PAGE) # return 404 if there are no questions for the page number if (len(current_questions) == 0): abort(404) categories_dict = {} for category in categories: categories_dict[category.id] = category.type # return values if there are no errors return jsonify({ 'success': True, 'total_questions': total_questions, 'categories': categories_dict, 'questions': current_questions }), 200
def get_questions(): """Get paginated questions This endpoint gets a list of paginated questions based on the page query string parameter and returns a 404 when the page is out of bound QUESTIONS_PER_PAGE is a global variable """ # get paginated questions and categories questions = Question.query.order_by(Question.id).all() total_questions = len(questions) categories = Category.query.order_by(Category.id).all() # Get paginated questions current_questions = get_paginated_questions( request, questions, QUESTIONS_PER_PAGE) # return 404 if there are no questions for the page number if (len(current_questions) == 0): abort(404) categories_dict = {} for category in categories: categories_dict[category.id] = category.type # return values if there are no errors return jsonify({ 'success': True, 'total_questions': total_questions, 'categories': categories_dict, 'questions': current_questions }), 200
def search_questions(): data = request.get_json() search_term = data.get('searchTerm', '') if search_term == '': abort(422) try: questions = Question.query.filter( Question.question.ilike(f'%{search_term}%')).all() if len(questions) == 0: abort(404) paginated_questions = get_paginated_questions( request, questions, QUESTIONS_PER_PAGE) return jsonify({ 'success': True, 'questions': paginated_questions, 'total_questions': len(Question.query.all()) }), 200 except: abort(404)
def get_questions_by_category(id): category = Category.query.filter_by(id=id).one_or_none() if (category is None): abort(422) questions = Question.query.filter_by(category=id).all() paginated_questions = get_paginated_questions(request, questions,QUESTIONS_PER_PAGE) return jsonify({ 'success': True, 'questions': paginated_questions, 'total_questions': len(questions), 'current_category': category.type })
def get_questions(): questions = Question.query.order_by(Question.id).all() total_questions = len(questions) categories = Category.query.order_by(Category.id).all() current_questions = get_paginated_questions(request, questions,QUESTIONS_PER_PAGE) if (len(current_questions) == 0): abort(404) categories_dict = {} for category in categories: categories_dict[category.id] = category.type return jsonify({ 'success': True, 'total_questions': total_questions, 'categories': categories_dict, 'questions': current_questions }), 200
def get_questions_by_category(id): # get the category by id category = Category.query.filter_by(id=id).one_or_none() # abort 400 for bad request if category isn't found if (category is None): abort(422) questions = Question.query.filter_by(category=id).all() # paginate questions paginated_questions = get_paginated_questions(request, questions, QUESTIONS_PER_PAGE) # return the results return jsonify({ 'success': True, 'questions': paginated_questions, 'total_questions': len(questions), 'current_category': category.type })