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
This commit is contained in:
BBaoVanC 2020-09-24 20:57:28 -05:00
parent d0abbef8d1
commit 2cc2a3917a
Signed by: bbaovanc
GPG Key ID: 18089E4E3CCF1D3A

View File

@ -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("/<location>/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("/<location>/api/v1/item/<item>/<brand>/<key>", methods = ["DELETE"])
def api_rm_brand_key(location, item, brand, key):
@app.route("/<location>/api/v1/item/<item>/<brand>/<field>", 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("/<location>/api/v1/item/<item>/<brand>/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("/<location>/api/v1/item/<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("/<location>/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("/<location>/api/v1/item/<item>/<brand>/add_amount/<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!")