Add some more routes to webapi.py

This commit is contained in:
BBaoVanC 2020-10-02 08:52:58 -05:00
parent 51ebe81bba
commit 1ce7954476
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2
3 changed files with 41 additions and 89 deletions

View File

@ -1,54 +0,0 @@
#!/usr/bin/env python3
# Imports
import psycopg2
from configparser import ConfigParser
class Database:
def __init__(self):
filename = "postgres.ini"
section = "postgresql"
parser = ConfigParser()
parser.read(filename)
config = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
config[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
self.host = config["host"]
self.user = config["user"]
self.password = config["password"]
self.port = 5432
self.dbname = config["database"]
self.conn = None
def connect(self):
if self.conn is None:
try:
self.conn = psycopg2.connect(host=self.host, user=self.user, password=self.password, port=self.port, dbname=self.dbname)
except psycopg2.DatabaseError as e:
print(e)
raise e
finally:
print("Connection opened successfully.")
def runcmd_unsafe(self, cmd):
self.connect()
with self.conn.cursor() as cur:
cur.execute(cmd)
return cur.fetchall()
def list_types(self):
self.connect()
with self.conn.cursor() as cur:
cur.execute("SELECT name FROM types")
types = [row for row in cur.fetchall()]
return types

View File

@ -1,4 +1,3 @@
Flask
Flask_API
psycopg2
sqlalchemy
psycopg2-binary

View File

@ -67,49 +67,56 @@ def get_locations():
return jsonify(data2)
@app.route("/<location>/api/v1/current_quantities", methods = ["GET"])
@app.route("/api/v1/location/<location>/current_quantities", methods = ["GET"])
def api_get_current_quantities(location):
db = get_db()
with db.cursor() as cur:
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = '{0}'".format(location))
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = %s", (location,))
data = cur.fetchall()
print(data)
data2 = {}
for row in data:
data2[row[0]] = row[1]
return jsonify(data2)
if len(data) <= 0:
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
else:
data2 = {}
for row in data:
data2[row[0]] = row[1]
return jsonify(data2)
# @app.route("/<location>/api/v1/items", methods = ["GET"])
# def api_get_items(location):
# try:
# items = storage.get_items(location)
# print("Got items at {0}".format(location))
# return jsonify(items)
# except KeyError:
# print("KeyError, returning 404")
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
@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("/<location>/api/v1/item/<item>", methods = ["GET", "DELETE"])
# def api_get_item(location, item):
# if request.method == "GET":
# try:
# itemresp = storage.get_items(location)[item.lower()]
# print("Got {0}".format(item))
# return jsonify(itemresp)
# except KeyError:
# print("KeyError, returning 404")
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
@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)
# elif request.method == "DELETE":
# try:
# storage.rm_item(location, item)
# print("Delete item {0} at {1}".format(item, location))
# return jsonify({'status': 'success'})
# except KeyError:
# print("KeyError, returning 404")
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
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)
return jsonify(data2)
# @app.route("/<location>/api/v1/item/<item>/<brand>", methods = ["GET", "DELETE"])