Beispiel #1
0
def post_task():
    """
    Returns a 201 response with a confirmation message as its body in case of
    a successful post

    If any of the expected field is missing in the post request body, it returns
    a 400 response indicating invalid data
    """
    request_body = request.get_json()

    if invalid_post_request_body(request_body):
        return make_response({"details": "Invalid data"}, 400)

    task = Task(title=request_body["title"],
                description=request_body["description"],
                completed_at=request_body["completed_at"])

    db.session.add(task)
    db.session.commit()

    return make_response(jsonify(task=task.as_dict()), 201)