Beispiel #1
0
def all_answers(answer_id):
    if request.method == 'GET':
        if answer_id is None:
            answers = Answer.query.all()
        else:
            answers = Answer.query.filter_by(id=answer_id).all()
        return serialize_answers(answers)

    if request.method == 'POST':
        posted_answers = json.loads(request.get_data(as_text=True))
        for answer in posted_answers['answers']:
            new_answer = Answer(text=answer['text'],
                                correct=answer['correct'],
                                question_id=answer['question_id'])
            db.session.add(new_answer)
            db.session.commit()
        return '201'

    if request.method == 'PUT':
        updates = json.loads(request.get_data(as_text=True))
        answer = Answer.query.filter_by(id=answer_id).first()
        for update in updates['update']:
            if update['what'] == 'text':
                answer.text = update['new']
            if update['what'] == 'correct':
                answer.correct = update['new']
            if update['what'] == 'question_id':
                answer.question_id = update['new']
        db.session.commit()
        return '200'
Beispiel #2
0
def question_answers(question_id):
    if request.method == 'GET':
        question = Question.query.filter_by(id=question_id).first()
        answers = question.answers
        return serialize_answers(answers)