我创建了一个Flask API,用户可以在其中发布问题,然后其他人可以回答所提出的问题并对其进行投票。我的问题是upvotes和downvotes部分。我应该用什么来实现呢?
我已经创建了一个用于创建用户的api。然后,用户可以提出问题。
这是用于创建问题的API。
@QUESTION.route('/question', methods = ['POST'])
def create_question():
data = request.get_json(force = True)
new_question = {
"question_id":len(meetups)+1,
"question":data['description']
"upvotes":0,
"downvotes":0,
"asked_at":datetime.datetime.now()
}
questions.append(new_question)
return make_response(jsonify(new_question), 201)
@QUESTION.route('/question', methods = ['GET'])
def get_all_questions():
result = sorted(questions, key=itemgetter('asked_at'), reverse=True)
return make_response(jsonify(result), 201)
@QUESTION.route('/question/<question_id>', methods = ['GET'])
def get_single_user(question_id):
for question in questions:
if question['question_id'] == question_id:
return make_response(jsonify(question), 201)
应在问题下方显示赞成/反对的人数。