Commit c4e6d25e authored by Zach Perkins's avatar Zach Perkins
Browse files

getting more restful by the second

parent c0a86139
...@@ -5,6 +5,7 @@ from where.model.field_types import FieldType ...@@ -5,6 +5,7 @@ from where.model.field_types import FieldType
app = Flask(__name__) app = Flask(__name__)
def create_resource(session, model_cls, data, get_function): def create_resource(session, model_cls, data, get_function):
model = model_cls(**data) model = model_cls(**data)
session.add(model) session.add(model)
...@@ -14,6 +15,13 @@ def create_resource(session, model_cls, data, get_function): ...@@ -14,6 +15,13 @@ def create_resource(session, model_cls, data, get_function):
return response return response
def search_resource(session, model_cls, data):
query = session.query(model_cls).filter_by(**data)
results = list(map(lambda m: m.as_json(), query.limit(100).all()))
response = make_response(jsonify(results), 200)
return response
@app.route('/') @app.route('/')
def index(): def index():
return """ return """
...@@ -117,38 +125,43 @@ def get_category(session, id): ...@@ -117,38 +125,43 @@ def get_category(session, id):
abort(404) abort(404)
@app.route('/point/<id>') @app.route('/category/<id>/children')
@with_session @with_session
def get_point(session, id): def get_category_children(session, id):
result = session.query(Point).filter_by(id=id).first() data = dict(request.args)
if result: data['parent_id'] = id
return jsonify(result.as_json()) return search_resource(session, Point, data)
@app.route('/add-point', methods=['POST']) @app.route('/point', methods=['GET', 'POST'])
@with_session @with_session
def add_point(session): def handle_point(session):
if request.method == 'GET':
return search_resource(session, Point, dict(request.args))
elif request.method == 'POST':
data = request.get_json() data = request.get_json()
data['category'] = session.query(Category).get(data['category']) data['category'] = session.query(Category).get(data['category'])
data['parent_id'] = data.pop('parent', None) data['parent_id'] = data.pop('parent', None)
return create_resource(session, Point, data, 'get_point') return create_resource(session, Point, data, 'get_point')
@app.route('/point', methods=['GET'])
def search_points(session):
q = session.query(Point)
if 'category_id' in request.args:
q = q.filter(Point.category_id == request.args.get('category_id'))
if 'parent_id' in request.args: @app.route('/point/<id>', methods=['GET', 'PUT', 'DELETE'])
q = q.filter(Point.parent_id == request.args.get('parent_id')) @with_session
def get_point(session, id):
result = session.query(Point).get(id)
if result:
return jsonify(result.as_json())
return jsonify(list(map(lambda p: p.as_json(), q.limit(100).all())))
@app.route('/point/<id>/children', methods=['GET'])
def get_point_children(session, id):
data = dict(request.args)
data['parent_id'] = id
return search_resource(session, Point, data)
if __name__ == '__main__': if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment