예제 #1
0
def handle_goals():
    if request.method == "GET":
        direction = request.args.get("sort")
        if direction == "asc":
            goals = Goal.query.order_by(asc("title"))
        elif direction == "desc":
            goals = Goal.query.order_by(desc("title"))
        else:
            goals = Goal.query.all()

        goals_response = [goal.to_dict() for goal in goals]

        return jsonify(goals_response)

    elif request.method == "POST":
        request_body = request.get_json()
        try:
            new_goal = Goal(title=request_body["title"])
        except KeyError:
            return make_response({"details": "Invalid data"}, 400)

        db.session.add(new_goal)
        db.session.commit()

        return make_response({"goal": new_goal.to_dict()}, 201)
예제 #2
0
def three_goals(app):
    db.session.add_all([
        Goal(title="Water the garden 🌷"),
        Goal(title="Answer forgotten email 📧"),
        Goal(title="Pay my outstanding tickets 😭")
    ])
    db.session.commit()
예제 #3
0
def handle_goals():
    if request.method == "GET":
        query_param_value = request.args.get("sort")
        if query_param_value == "asc":
            goals = Task.query.order_by(Goal.title.asc())
        elif query_param_value == "desc":
            goals = Task.query.order_by(Goal.title.desc())
        else:
            goals = Goal.query.all()

        goals_response = []
        for goal in goals:
            goals_response.append(goal.to_dict())
        return jsonify(goals_response), 200

    elif request.method == "POST":
        request_body = request.get_json()

        # Invalid goal if missing title
        if "title" not in request_body:
            return jsonify({"details": "Invalid data"}), 400
        else:
            new_goal = Goal(title=request_body["title"])

            db.session.add(new_goal)
            db.session.commit()

            return make_response({"goal": new_goal.to_dict()}, 201)
예제 #4
0
def three_goals(app):
    db.session.add_all([
        Goal(title="Build a habit of going outside daily"),
        Goal(title="Build a habit of reading daily"),
        Goal(title="Build a habit of singing daily")
    ])
    db.session.commit()
예제 #5
0
def three_goals(app):
    db.session.add_all([
        Goal(title="Run an ultramarathon"),
        Goal(title="Go solo paragliding"),
        Goal(title="Save a million dollars")
    ])
    db.session.commit()
예제 #6
0
def handle_goals():
    if request.method == "GET":
        goals = Goal.query.all()

        goal_response = []
        for goal in goals:
            goal_response.append({
                "id": goal.goal_id,
                "title": goal.title,
            })
        return jsonify(goal_response)

    elif request.method == "POST":
        request_body = request.get_json()

        if "title" not in request_body.keys():
            return make_response({"details": "Invalid data"}, 400)

        else:
            new_goal = Goal(title=request_body["title"])

            db.session.add(new_goal)
            db.session.commit()

            return make_response({"goal": new_goal.create_response()}, 201)
예제 #7
0
def handle_goal():

    if request.method == "GET":

        goals = Goal.query.all()
        response = []

        if not goals:
            return jsonify(response), 200

        for goal in goals:
            response.append({"id": goal.goal_id, "title": goal.title})

        return jsonify(response), 200

    elif request.method == "POST":

        request_body = request.get_json()

        if request_body:
            new_goal = Goal(title=request_body["title"])

            db.session.add(new_goal)
            db.session.commit()

            response = {"goal": new_goal.json_response()}

            return jsonify(response), 201

        else:
            response = {"details": "Invalid data"}
            return jsonify(response), 400
예제 #8
0
def create_new_goal():
    new_goal_data = request.get_json()
    if new_goal_data.keys() >= {"title"}:
        new_goal = Goal(**new_goal_data)
        db.session.add(new_goal)
        db.session.commit()
        return {"goal": new_goal.goal_to_json()}, 201
    return {"details": "Invalid data"}, 400
예제 #9
0
def handle_goals():
    request_body = request.get_json()
    if not "title" in request_body or not request_body.get("title"):
        return missing_data()
    new_goal = Goal(title=request_body["title"])
    db.session.add(new_goal)
    db.session.commit()
    return make_response({"goal": new_goal.to_json()}, 201)
예제 #10
0
def post_goals():
    form_data = request.get_json()
    if "title" not in form_data:
        return ({"details": "Invalid data"}, 400)
    new_goal = Goal(title=form_data["title"])
    db.session.add(new_goal)
    db.session.commit()
    return {"goal": new_goal.goal_dict()}, 201
예제 #11
0
def create_goal():
    request_body = request.get_json()
    if "title" in request_body:
        new_goal = Goal(title=request_body["title"])
        db.session.add(new_goal)
        db.session.commit()
        return jsonify({"goal": new_goal.to_dict()}), 201
    return make_response({"details": "Invalid data"}, 400)
예제 #12
0
def post_new_goal():
    request_body = request.get_json()

    if "title" in request_body:
        new_goal = Goal(title=request_body["title"])
        db.session.add(new_goal)
        db.session.commit()
        return jsonify({"goal": new_goal.now_json()}), 201
    else:
        return make_response({"details": "Invalid data"}), 400
예제 #13
0
def create_goal():
    """Create a goal for the database"""
    request_body = request.get_json()

    if "title" not in request_body:
        return make_response({"details": "Invalid data"}, 400)
    new_goal = Goal(title=request_body["title"])
    
    db.session.add(new_goal)
    db.session.commit()
    return jsonify({"goal": new_goal.to_json()}), 201
예제 #14
0
def create_goal():
    try:
        # This portion is the POST request
        request_body = request.get_json()
        new_goal = Goal(title=request_body["title"])

        db.session.add(new_goal)
        db.session.commit()
        return {"goal": new_goal.to_json_goal()}, 201
    except KeyError:
        return {"details": "Invalid data"}, 400
예제 #15
0
def post_goal():
    '''posts one goal to database'''

    request_body = request.get_json()
    if "title" not in request_body.keys():
        return make_response({"details": "Invalid data"}, 400)
    new_goal = Goal(title=request_body["title"])
    db.session.add(new_goal)
    db.session.commit()

    return make_response({"goal": new_goal.build_dict()}, 201)
예제 #16
0
def create_goal():
    request_body = request.get_json()

    if "title" not in request_body.keys():
        return {"details": "Invalid data"}, 400

    new_goal = Goal(title=request_body["title"])
    db.session.add(new_goal)
    db.session.commit()

    return {"goal": new_goal.to_json()}, 201
예제 #17
0
def create_goal():
    request_body = request.get_json()

    try:
        goal = Goal(title=request_body["title"])
    except KeyError:
        return make_response({"details": "Invalid data"}, 400)

    db.session.add(goal)
    db.session.commit()
    return jsonify({"goal": goal.to_json()}), 201
예제 #18
0
def create_goal():
    request_body = request.get_json()

    if "title" not in request_body:
        return ({"details": "Invalid data"}), 400
    else:
        add_goal = Goal(title=request_body["title"])

        db.session.add(add_goal)
        db.session.commit()

        return make_response({"goal": add_goal.to_json_goal()}, 201)
예제 #19
0
def create_goals():
    request_body = request.get_json()

    if "title" not in request_body.keys():
        return make_response({"details": "Invalid data"}, 400)

    goal = Goal(title=request_body["title"])

    db.session.add(goal)
    db.session.commit()

    return {"goal": goal.json_response()}, 201
예제 #20
0
def goals():
    request_body = request.get_json()

    if "title" not in request_body:
        return make_response({"details": "Invalid data"}, 400)
    else:
        new_goal = Goal(title=request_body["title"])

        db.session.add(new_goal)
        db.session.commit()

        return make_response(jsonify({"goal": new_goal.to_json()}), 201)
예제 #21
0
def add_goal():
    goal_request = request.get_json()

    if not validate_field("title", goal_request):
        return {"details": "Invalid data"}, 400

    goal_to_add = Goal(title=goal_request["title"])

    db.session.add(goal_to_add)
    db.session.commit()

    return jsonify({"goal": goal_to_add.goal_to_json_format()}), 201
예제 #22
0
def create_goal():

    request_body = request.get_json()

    if (not request_body) or ("title" not in request_body):
        return {"details": "Invalid data"}, 400

    new_goal = Goal(title=request_body["title"])

    db.session.add(new_goal)
    db.session.commit()

    return make_response({"goal": (new_goal.convert_to_json())}, 201)
예제 #23
0
def post_goals():
    request_body = request.get_json()
    if "title" in request_body.keys():
        goal = Goal(title=request_body["title"],
                    )
        db.session.add(goal)
        db.session.commit()
        return jsonify({"goal": goal.api_response()}), 201
    else:
        return make_response(
            {"details": "Invalid data"
            }
        ), 400
예제 #24
0
def create_a_goal():
    request_body = request.get_json()

    if not ("title" in request_body):
        return make_response(jsonify({"details": "Invalid data"}), 400)

    new_goal = Goal(title=request_body["title"])

    db.session.add(new_goal)
    db.session.commit()

    response = {"goal": new_goal.to_json()}
    return make_response(jsonify(response), 201)
예제 #25
0
def post_new_goal():
    request_body = request.get_json()

    try:
        new_goal = Goal(title=request_body['title'])
    except KeyError:
        return make_response({"details": "Invalid data"}, 400)

    db.session.add(new_goal)
    db.session.commit()

    response = {"goal": new_goal.to_json()}

    return make_response(jsonify(response), 201)
예제 #26
0
def one_goal():

    if request.method == "GET":
        goals = Goal.query.all()  # get all items in goal list

        goals_response = []

        for goal in goals:
            goals_response.append({"id": goal.goal_id, "title": goal.title})

        return jsonify(goals_response)

    if request.method == "POST":
        request_body = request.get_json()
        if ("title" not in request_body):
            return make_response(jsonify({"details": "Invalid data"}), 400)
        else:
            goal = Goal(title=request_body["title"])

        db.session.add(goal)
        db.session.commit()

        return make_response(
            {"goal": {
                "id": goal.goal_id,
                "title": goal.title
            }}, 201)
예제 #27
0
def handle_goals():
    if request.method == 'GET':
        goals = Goal.query.all()
        goal_response = []
        for goal in goals:
            goal_response.append(goal.serialize())
        return jsonify(goal_response)

    elif request.method == 'POST':
        request_body = request.get_json()
        if "title" in request_body:
            new_goal = Goal(title=request_body['title'])
            db.session.add(new_goal)
            db.session.commit()
            return {'goal': new_goal.serialize()}, 201
        else:
            return ({"details": f'Invalid data'}, 400)
예제 #28
0
def post_goal():
    """
    Posts new goal
    """
    request_body = request.get_json()

    if "title" not in request_body:
        return ({
        "details": "Invalid data"
        }, 400)

    new_goal = Goal(
        title=request_body["title"])

    db.session.add(new_goal)
    db.session.commit()

    return ({"goal": new_goal.to_json()}, 201)
예제 #29
0
def create_or_get_goals():

    goals = Goal.query.all()
    if request.method == "GET":
        goal_response = []

        for goal in goals:
            goal_response.append({"id": goal.goal_id, "title": goal.title})
        return jsonify(goal_response), 200
    elif request.method == "POST":
        request_body = request.get_json()
        try:
            new_goal = Goal(title=request_body["title"])
        except KeyError:
            return make_response({"details": "Invalid data"}, 400)
        db.session.add(new_goal)
        db.session.commit()
    return make_response({"goal": new_goal.to_json()}, 201)
예제 #30
0
def post_goal():
    """
    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 "title" not in request_body:
        return make_response({"details": "Invalid data"}, 400)

    goal = Goal(title=request_body["title"])

    db.session.add(goal)
    db.session.commit()

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