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)
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
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
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
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)
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)
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)
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)
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)
def goals(): if request.method == "GET": goals = Goal.query.all() goals_response = [] for goal in goals: goals_response.append(goal.to_json()) return jsonify(goals_response) # using the "PUT" to add a new goal else: request_body = request.get_json() if "title" not in request_body: return make_response(jsonify({"details": "Invalid data"}), 400) goal = Goal(title=request_body["title"]) db.session.add(goal) db.session.commit() return jsonify({"goal": goal.to_json()}), 201
def handle_goals(): if request.method == "POST": request_body = request.get_json() if not request_body: return jsonify({ "details": "Invalid data" }), 400 new_goal = Goal(title=request_body["title"]) db.session.add(new_goal) db.session.commit() return new_goal.to_json(), 201 if request.method == "GET": title_query = request.args.get("title") if title_query: goals = Goal.query.filter_by(title=title_query) else: goals = Goal.query.all() goal_list=[] for goal in goals: goal_list.append({ "id": goal.goal_id, "title": goal.title, }) return jsonify(goal_list)