def delete(self, name): category = CategoryModel.find_by_name(name) if category: category.delete_from_db() return {'message': f'category {name} deleted'}
def get(cls, id): ''' Create a GET endpoint to get questions based on category. ''' page_num = request.args.get("page", 1, type=int) category = CategoryModel.find_by_id(id) if category: try: questions = category.questions.paginate( page=page_num, per_page=QUESTIONS_PER_PAGE) current_page = questions.page total_questions = questions.total print(questions.items) return { "current_page": current_page, "total_questions": total_questions, "current_category": category.type, "category_id": category.id, "questions": category_questions_schema.dump(questions.items) }, 200 except: return {"message": "questions not found"}, 404 # return category_questions_schema.dump(category) ,200 return {"message": "Category not found"}, 404
def convert_category_name_to_id(cls, json_obj): ''' helper method to convert category name to category id ''' categories = CategoryModel.find_all() json_obj['category'] = [ x.id for x in categories if x.type.lower() == json_obj['category'] ][0] return json_obj
def get(self, name): try: category = CategoryModel.find_by_name(name) except: return {'message': 'an error occurred'}, 500 if category: return category.json(), 200 return {'message': 'Category not found'}, 404
def put(self, name): data = Category.parser.parse_args() # data = request.get_json() try: category = CategoryModel.find_by_name(name) except: return {'message': 'an error occurred'}, 500 if category is None: category = CategoryModel(name) else: category.price = data['price'] try: category.save_to_db() except: return {'message': 'an error occurred updating category'}, 500 return category.json()
def post(self, name): if CategoryModel.find_by_name(name): return { "message": f"A category with name '{name}' already exists." }, 400 # data = Category.parser.parse_args() category = CategoryModel(name) try: category.save_to_db() except: return {'message': 'an error occurred inserting the category'}, 500 return category.json(), 201
def get(cls): ''' Create an endpoint to handle GET requests for questions, including pagination (every 10 questions). This endpoint should return a list of questions, number of total questions, current category, categories. ''' page_num = request.args.get("page", 1, type=int) try: questions = QuestionModel.query.paginate( page=page_num, per_page=QUESTIONS_PER_PAGE) current_page = questions.page total_questions = questions.total categories = CategoryModel.find_all() return { "current page": current_page, "total_questions": total_questions, "categories": category_list_schema.dump(categories), "questions": question_list_schema.dump(questions.items) }, 200 except: return {"message": "questions not found"}, 404
def post(cls): ''' Create a POST endpoint to get questions to play the quiz. This endpoint should take category and previous question parameters and return a random questions within the given category, if provided, and that is not one of the previous questions. ''' quiz_data = request.get_json() print(quiz_data) if quiz_data['category']: category = CategoryModel.find_by_id(quiz_data['category']) questions = category.questions.all() else: questions = QuestionModel.query.all() # print(questions) if questions: # shuffling the questions so the game wouldnt be the same shuffle(questions) # print(questions) questions = questions[:QUESTIONS_PER_GAME] # print(questions) else: return {"message": "no more questions in that category"}, 404 return {"questions": question_list_schema.dump(questions)}, 200
def get(cls): try: categories = CategoryModel.find_all() except: return {"message": "categories not found"}, 404 return {"categories": category_list_schema.dump(categories)}, 200
def get(cls, id): category = CategoryModel.find_by_id(id) if category: return category_schema.dump(category), 200 return {"message": "Category not found"}, 404