def put(self, course_id): db_course = Course.query.filter_by(id=course_id).first() if db_course is None: return create_error_response( 404, "Not found", "No course was found with the ID {}".format(course_id)) if not request.json: return create_error_response(415, "Unsupported media type", "Requests must be JSON") try: validate(request.json, Course.get_schema()) except ValidationError as e: return create_error_response(400, "Invalid JSON document", str(e)) db_course.name = request.json["name"] db_course.num_holes = request.json["num_holes"] db_course.par = request.json["par"] try: db.session.commit() except IntegrityError: return create_error_response( 409, "Already exists", "Course with name '{}' already exists.".format( request.json["name"])) return Response(status=204)
def post(self): if not request.json: return create_error_response(415, "Unsupported media type", "Requests must be JSON") try: validate(request.json, Course.get_schema()) except ValidationError as e: return create_error_response(400, "Invalid JSON document", str(e)) course = Course(name=request.json["name"], ) if "num_holes" in request.json: course.num_holes = request.json["num_holes"] if "par" in request.json: course.par = request.json["par"] try: db.session.add(course) db.session.commit() except IntegrityError: return create_error_response( 409, "Already exists", "Course with name '{}' already exists.".format( request.json["name"])) return Response(status=201, headers={ "Location": url_for("api.courseitem", course_id=course.id) })
def delete(self, course_id): db_course = Course.query.filter_by(id=course_id).first() if db_course is None: return create_error_response( 404, "Not found", "No course found with the id {}".format(course_id)) db.session.delete(db_course) db.session.commit() return Response(status=204)
def delete(self, player_id): db_player = Player.query.filter_by(id=player_id).first() if db_player is None: return create_error_response( 404, "Not found", "No player found with the id {}".format(player_id)) db.session.delete(db_player) db.session.commit() return Response(status=204)
def post(self): if not request.json: return create_error_response(415, "Unsupported media type", "Requests must be JSON") try: validate(request.json, Player.get_schema()) except ValidationError as e: return create_error_response(400, "Invalid JSON document", str(e)) player = Player(name=request.json["name"]) db.session.add(player) db.session.commit() return Response(status=201, headers={ "Location": url_for("api.playeritem", player_id=player.id) })
def put(self, player_id): if not request.json: return create_error_response(415, "Unsupported media type", "Requests must be JSON") db_player = Player.query.filter_by(id=player_id).first() if db_player is None: return create_error_response( 404, "Not found", "No player found with the id {}".format(player_id)) try: validate(request.json, Player.get_schema()) except ValidationError as e: return create_error_response(400, "Invalid JSON document", str(e)) db_player.name = request.json["name"] db.session.commit() return Response(status=204)
def get(self, player_id): db_player = Player.query.filter_by(id=player_id).first() if db_player is None: return create_error_response( 404, "Not found", "No player found with the id {}".format(player_id)) body = FrolftrackerBuilder(player_id=db_player.id, name=db_player.name) body.add_namespace("frolf", LINK_RELATIONS_URL) body.add_control("self", url_for("api.playeritem", player_id=player_id)) body.add_control("profile", PLAYER_PROFILE) body.add_control("collection", url_for("api.playercollection")) body.add_control_delete_player(player_id) body.add_control_modify_player(player_id) body.add_control_get_scores_by_player(player_id) return Response(json.dumps(body), 200, mimetype=MASON)
def get(self, course_id): db_course = Course.query.filter_by(id=course_id).first() if db_course is None: return create_error_response( 404, "Not found", "No course found with the id {}".format(course_id)) body = FrolftrackerBuilder(course_id=db_course.id, name=db_course.name, num_holes=db_course.num_holes, par=db_course.par) body.add_namespace("frolf", LINK_RELATIONS_URL) body.add_control("self", url_for("api.courseitem", course_id=course_id)) body.add_control("profile", COURSE_PROFILE) body.add_control("collection", url_for("api.coursecollection")) body.add_control_delete_course(course_id) body.add_control_modify_course(course_id) body.add_control_get_scores_by_course(course_id) return Response(json.dumps(body), 200, mimetype=MASON)