2020-10-02 night
This commit is contained in:
parent
1ce7954476
commit
5a44b65bf3
247
webapi.py
247
webapi.py
@ -53,9 +53,13 @@ def close_conn(e):
|
|||||||
def get_locations():
|
def get_locations():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
with db.cursor() as cur:
|
with db.cursor() as cur:
|
||||||
cur.execute("SELECT name,description FROM location")
|
cur.execute("SELECT name,description FROM location")
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
print(data)
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
print("ran SELECT name,description FROM location")
|
||||||
data2 = {}
|
data2 = {}
|
||||||
for row in data:
|
for row in data:
|
||||||
data2[row[0]] = {}
|
data2[row[0]] = {}
|
||||||
@ -63,92 +67,199 @@ def get_locations():
|
|||||||
# data2 = []
|
# data2 = []
|
||||||
# for row in data:
|
# for row in data:
|
||||||
# data2.append({'name': row[0], 'description': row[1]})
|
# data2.append({'name': row[0], 'description': row[1]})
|
||||||
|
print("processed data:")
|
||||||
print(data2)
|
print(data2)
|
||||||
return jsonify(data2)
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v1/location/<location>/current_quantities", methods = ["GET"])
|
@app.route("/api/v1/location/name/<search>", methods = ["GET"])
|
||||||
|
def api_get_location_information(search):
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
cur.execute("SELECT name,description FROM location WHERE name = %s", (search,))
|
||||||
|
data = cur.fetchall()
|
||||||
|
print("ran SELECT name,description FROM location WHERE name = %s")
|
||||||
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
data2 = {}
|
||||||
|
row = data[0]
|
||||||
|
data2['name'] = row[0]
|
||||||
|
data2['description'] = row[1]
|
||||||
|
print("processed data:")
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/location/<location>/quantities", methods = ["GET"])
|
||||||
def api_get_current_quantities(location):
|
def api_get_current_quantities(location):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
with db.cursor() as cur:
|
with db.cursor() as cur:
|
||||||
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = %s", (location,))
|
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = %s", (location,))
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
print(data)
|
print("ran SELECT product_upc,quantity FROM product_quantity WHERE location = %s")
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
else:
|
else:
|
||||||
data2 = {}
|
data2 = {}
|
||||||
for row in data:
|
for row in data:
|
||||||
data2[row[0]] = row[1]
|
data2[row[0]] = row[1]
|
||||||
return jsonify(data2)
|
print("processed data:")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v1/location/<location>/current_quantity/<upc>", methods = ["GET"])
|
|
||||||
def api_get_quantity_of_product_in_location(location, upc):
|
|
||||||
db = get_db()
|
|
||||||
with db.cursor() as cur:
|
|
||||||
cur.execute("SELECT quantity FROM product_quantity WHERE location = %s AND product_upc = %s", (location, upc))
|
|
||||||
data = cur.fetchall()
|
|
||||||
if len(data) <= 0:
|
|
||||||
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
|
||||||
else:
|
|
||||||
return jsonify(data[0][0])
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v1/product/<product_upc>", methods = ["GET"])
|
|
||||||
def api_get_product_information(product_upc):
|
|
||||||
db = get_db()
|
|
||||||
with db.cursor() as cur:
|
|
||||||
cur.execute("SELECT upc,brand,name,size,sizeunit,description FROM product WHERE upc = %s", (product_upc,))
|
|
||||||
data = cur.fetchall()
|
|
||||||
if len(data) <= 0:
|
|
||||||
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
|
||||||
else:
|
|
||||||
row = data[0]
|
|
||||||
print(row)
|
|
||||||
|
|
||||||
data2 = {}
|
|
||||||
data2['upc'] = row[0]
|
|
||||||
data2['brand'] = row[1]
|
|
||||||
data2['name'] = row[2]
|
|
||||||
data2['size'] = row[3]
|
|
||||||
data2['sizeunit'] = row[4]
|
|
||||||
data2['description'] = row[5]
|
|
||||||
|
|
||||||
print(data2)
|
print(data2)
|
||||||
return jsonify(data2)
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/item/<item>/<brand>", methods = ["GET", "DELETE"])
|
@app.route("/api/v1/location/<location>/quantity/<searchmethod>/<search>", methods = ["GET"])
|
||||||
# def api_item_brand(location, item, brand):
|
def api_get_quantity_of_product_in_location(location, searchmethod, search):
|
||||||
# if request.method == "GET":
|
db = get_db()
|
||||||
# try:
|
with db.cursor() as cur:
|
||||||
# itemresp = storage.get_items(location)[item.lower()][brand.lower()]
|
methodmap = {'upc': 'product_upc', 'name': 'name'}
|
||||||
# print("Got {0} of brand {1} at {2}".format(item, brand, location))
|
if searchmethod in methodmap.keys():
|
||||||
# return jsonify(itemresp)
|
method = methodmap[searchmethod]
|
||||||
# except KeyError:
|
cur.execute("SELECT quantity FROM product_quantity WHERE location = %s AND {0} = %s".format(method), (location, search))
|
||||||
# print("KeyError, returning 404")
|
data = cur.fetchall()
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
print("ran SELECT quantity FROM product_quantity WHERE location = %s AND {0} = %s".format(method))
|
||||||
|
if len(data) <= 0:
|
||||||
# elif request.method == "DELETE":
|
print("no results returned, sending 404")
|
||||||
# try:
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
# storage.rm_brand(location, item, brand)
|
else:
|
||||||
# print("Deleted {0} of brand {1} at {2}".format(item, brand, location))
|
data2 = data[0][0]
|
||||||
# return jsonify({'status': 'success'})
|
print("processed data:")
|
||||||
# except KeyError:
|
print(data2)
|
||||||
# print("KeyError, returning 404")
|
return jsonify(data2)
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
else:
|
||||||
|
print("invalid search method, sending 400")
|
||||||
|
return jsonify({'error': 'INVALID_SEARCH_METHOD'}), status.HTTP_400_BAD_REQUEST
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/item/<item>/<brand>/<field>", methods = ["DELETE"])
|
@app.route("/api/v1/products", methods = ["GET"])
|
||||||
# def api_rm_brand_field(location, item, brand, field):
|
def api_get_products():
|
||||||
# try:
|
db = get_db()
|
||||||
# storage.rm_brand_field(location, item, brand, field)
|
with db.cursor() as cur:
|
||||||
# print("Deleted field {0} from brand {1} of item {2} at {3}".format(field, brand, item, location))
|
cur.execute("SELECT upc,brand,name,size,sizeunit,description FROM product")
|
||||||
# return jsonify({'status': 'success'})
|
data = cur.fetchall()
|
||||||
# except KeyError:
|
print("ran SELECT upc,brand,name,size,sizeunit,description FROM product")
|
||||||
# print("KeyError, returning 404")
|
if len(data) <= 0:
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
data2 = {}
|
||||||
|
for row in data:
|
||||||
|
data2[row[0]] = {'brand': row[1], 'name': row[2], 'size': row[3], 'sizeunit': row[4], 'description': row[5]}
|
||||||
|
print("processed data:")
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
@app.route("/api/v1/product/<searchmethod>/<search>", methods = ["GET"])
|
||||||
|
def api_get_product_information(searchmethod, search):
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
if searchmethod in ['name', 'upc']:
|
||||||
|
cur.execute("SELECT upc,brand,name,size,sizeunit,description FROM product WHERE {0} = %s".format(searchmethod), (search,))
|
||||||
|
data = cur.fetchall()
|
||||||
|
print("ran SELECT upc,brand,name,size,sizeunit,description FROM product WHERE {0} = %s")
|
||||||
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
row = data[0]
|
||||||
|
|
||||||
|
data2 = {}
|
||||||
|
data2['upc'] = row[0]
|
||||||
|
data2['brand'] = row[1]
|
||||||
|
data2['name'] = row[2]
|
||||||
|
data2['size'] = row[3]
|
||||||
|
data2['sizeunit'] = row[4]
|
||||||
|
data2['description'] = row[5]
|
||||||
|
|
||||||
|
print("processed data:")
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
else:
|
||||||
|
print("invalid search method, sending 400")
|
||||||
|
return jsonify({'error': 'INVALID_SEARCH_METHOD'}), status.HTTP_400_BAD_REQUEST
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/brands", methods = ["GET"])
|
||||||
|
def api_list_brands():
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
cur.execute("SELECT name,description FROM brand")
|
||||||
|
data = cur.fetchall()
|
||||||
|
print("ran SELECT name,description FROM brand")
|
||||||
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
data2 = {}
|
||||||
|
for row in data:
|
||||||
|
data2[row[0]] = {'description': row[1]}
|
||||||
|
print("processed data:")
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/brand/name/<search>", methods = ["GET"])
|
||||||
|
def api_get_brand_by_name(search):
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
cur.execute("SELECT name,description FROM brand WHERE name = %s", (search,))
|
||||||
|
data = cur.fetchall()
|
||||||
|
print("ran SELECT name,description FROM brand WHERE name = %s")
|
||||||
|
if len(data) <= 0:
|
||||||
|
print("no results returned, sending 404")
|
||||||
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
|
else:
|
||||||
|
data2 = {}
|
||||||
|
for row in data:
|
||||||
|
data2[row[0]] = {'description': row[1]}
|
||||||
|
print("processed data:")
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/create_location", methods = ["POST"])
|
||||||
|
def api_create_location():
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
if "name" not in request.form:
|
||||||
|
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
||||||
|
else:
|
||||||
|
locname = request.form['name']
|
||||||
|
if "description" in request.form:
|
||||||
|
locdesc = request.form['description']
|
||||||
|
else:
|
||||||
|
locdesc = None
|
||||||
|
|
||||||
|
print(locname, locdesc)
|
||||||
|
cur.execute("INSERT INTO location (name, description) VALUES (%s, %s)", (locname, locdesc))
|
||||||
|
print("ran INSERT INTO location (name, description) VALUES (%s, %s)")
|
||||||
|
db.commit()
|
||||||
|
print("committed changes to database")
|
||||||
|
return jsonify({'api_endpoint': '/api/v1/location/name/{0}'.format(locname)})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/create_brand", methods = ["POST"])
|
||||||
|
def api_create_brand():
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cur:
|
||||||
|
if "name" not in request.form:
|
||||||
|
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
||||||
|
else:
|
||||||
|
locname = request.form['name']
|
||||||
|
if "description" in request.form:
|
||||||
|
locdesc = request.form['description']
|
||||||
|
else:
|
||||||
|
locdesc = None
|
||||||
|
|
||||||
|
print(locname, locdesc)
|
||||||
|
cur.execute("INSERT INTO brand (name, description) VALUES (%s, %s)", (locname, locdesc))
|
||||||
|
print("ran INSERT INTO brand (name, description) VALUES (%s, %s)")
|
||||||
|
db.commit()
|
||||||
|
print("committed changes to database")
|
||||||
|
return jsonify({'api_endpoint': '/api/v1/brand/name/{0}'.format(locname)})
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/item/<item>/<brand>/new_brand", methods = ["POST"])
|
# @app.route("/<location>/api/v1/item/<item>/<brand>/new_brand", methods = ["POST"])
|
||||||
|
Loading…
Reference in New Issue
Block a user