From 2cc2a3917a39da1a3a594b31e3fdce9f607a66aa Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Thu, 24 Sep 2020 20:57:28 -0500 Subject: [PATCH] Add more routes to webapi.py - Add API endpoint to create a new brand - Add API endpoint to create a new item - Add API endpoint to create a new location - Add API endpoint to increment the amount of one item --- webapi.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/webapi.py b/webapi.py index debc1eb..72981c0 100644 --- a/webapi.py +++ b/webapi.py @@ -12,8 +12,13 @@ import storage app = Flask(__name__) # app is the Flask app +@app.route("/api/v1/locations", methods = ["GET"]) +def api_get_locations(): + return jsonify(storage.get_locations()) + + @app.route("//api/v1/items", methods = ["GET"]) -def api_items(location): +def api_get_items(location): try: items = storage.get_items(location) print("Got items at {0}".format(location)) @@ -65,16 +70,52 @@ def api_item_brand(location, item, brand): return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND -@app.route("//api/v1/item///", methods = ["DELETE"]) -def api_rm_brand_key(location, item, brand, key): +@app.route("//api/v1/item///", methods = ["DELETE"]) +def api_rm_brand_field(location, item, brand, field): try: - storage.rm_brand_key(location, item, brand, key) - print("Deleted key {0} from brand {1} of item {2} at {3}".format(key, brand, item, location)) + storage.rm_brand_field(location, item, brand, field) + print("Deleted field {0} from brand {1} of item {2} at {3}".format(field, brand, item, location)) return jsonify({'status': 'success'}) except KeyError: print("KeyError, returning 404") return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND +@app.route("//api/v1/item///new_brand", methods = ["POST"]) +def api_new_brand(location, item, brand): + try: + storage.init_brand(location, item, brand) + print("Created brand {0} under item {1} at {2}".format(brand, item, location)) + return jsonify({'status': 'success'}) + except storage.AlreadyExistsError: + return jsonify({'status': 'error', 'error': 'BRAND_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN + + +@app.route("//api/v1/item//new_item", methods = ["POST"]) +def api_new_item(location, item): + try: + storage.init_item(location, item) + print("Created item {0} at {1}".format(item, location)) + return jsonify({'status': 'success'}) + except storage.AlreadyExistsError: + return jsonify({'status': 'error', 'error': 'ITEM_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN + + +@app.route("//api/v1/new_location", methods = ["POST"]) +def api_new_location(location): + try: + storage.init_location(location) + print("Created location {0}".format(location)) + return jsonify({'status': 'success'}) + except storage.AlreadyExistsError: + return jsonify({'status': 'error', 'error': 'ITEM_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN + + +@app.route("//api/v1/item///add_amount/", methods = ["POST"]) +def api_add_amount(location, item, brand, amount): + storage.increment_amount(location, item, brand, amount) + return jsonify({'status': 'success'}) + + if __name__ == "__main__": print("Run with `flask` or a WSGI server!")