From 1ce795447626141a308ce60c7709b09dd30c5f0d Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Fri, 2 Oct 2020 08:52:58 -0500 Subject: [PATCH] Add some more routes to webapi.py --- libinv.py | 54 ----------------------------------- requirements.txt | 3 +- webapi.py | 73 ++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 89 deletions(-) delete mode 100644 libinv.py diff --git a/libinv.py b/libinv.py deleted file mode 100644 index 2046bcc..0000000 --- a/libinv.py +++ /dev/null @@ -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 diff --git a/requirements.txt b/requirements.txt index 293972b..f93f46a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ Flask Flask_API -psycopg2 -sqlalchemy +psycopg2-binary diff --git a/webapi.py b/webapi.py index 82b72ba..765d657 100644 --- a/webapi.py +++ b/webapi.py @@ -67,49 +67,56 @@ def get_locations(): return jsonify(data2) -@app.route("//api/v1/current_quantities", methods = ["GET"]) +@app.route("/api/v1/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("//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//current_quantity/", 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/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/", 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("//api/v1/item//", methods = ["GET", "DELETE"])