Finish API
This commit is contained in:
parent
8c635adcc7
commit
4bb70b207e
34
libdb.py
34
libdb.py
@ -6,7 +6,9 @@ Library for interacting with InvMan DB using SQLAlchemy
|
||||
"""
|
||||
|
||||
from configparser import ConfigParser
|
||||
from sqlalchemy import create_engine, Column, String, Text, BigInteger, Float
|
||||
from sqlalchemy import create_engine, Column
|
||||
from sqlalchemy import String, Text, Date
|
||||
from sqlalchemy import BigInteger, Integer, SmallInteger, Float
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
@ -74,3 +76,33 @@ class Brand(Base):
|
||||
|
||||
name = Column(String(length=63), primary_key=True)
|
||||
description = Column(Text)
|
||||
|
||||
|
||||
class Unit(Base):
|
||||
"""unit table"""
|
||||
__tablename__ = 'unit'
|
||||
|
||||
name = Column(String(length=32), primary_key=True)
|
||||
description = Column(Text)
|
||||
|
||||
|
||||
class Purchase(Base):
|
||||
"""purchase table"""
|
||||
__tablename__ = 'purchase'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
product_upc = Column(String(length=32))
|
||||
quantity = Column(SmallInteger)
|
||||
date = Column(Date)
|
||||
location = Column(String(length=32))
|
||||
|
||||
|
||||
class Use(Base):
|
||||
"""use table"""
|
||||
__tablename__ = 'use'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
product_upc = Column(String(length=32))
|
||||
quantity = Column(SmallInteger)
|
||||
date = Column(Date)
|
||||
location = Column(String(length=32))
|
||||
|
177
webapi.py
177
webapi.py
@ -10,8 +10,7 @@ from flask import Flask, jsonify, request
|
||||
from flask_api import status
|
||||
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
import sqlalchemy
|
||||
from libdb import Session, Location, ProductQuantity, Product, Brand
|
||||
from libdb import Session, Location, ProductQuantity, Product, Brand, Unit, Purchase, Use
|
||||
|
||||
|
||||
app = Flask(__name__) # app is the Flask app
|
||||
@ -147,14 +146,102 @@ def api_get_brand_by_name(search):
|
||||
"""Route to get information about a location"""
|
||||
session = Session()
|
||||
try:
|
||||
data = session.query(Brand.name, Brand.description). \
|
||||
filter(Brand.name == search).one()
|
||||
data = session.query(Brand.name, Brand.description).filter(
|
||||
Brand.name == search).one()
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
data2 = {'name': data.name, 'description': data.description}
|
||||
return jsonify(data2)
|
||||
|
||||
|
||||
@app.route("/api/v1/units", methods=["GET"])
|
||||
def api_list_units():
|
||||
"""Route to list all units"""
|
||||
session = Session()
|
||||
try:
|
||||
data = {}
|
||||
for name, description in session.query(Unit.name, Unit.description).all():
|
||||
data[name] = {'description': description}
|
||||
return jsonify(data)
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@app.route("/api/v1/unit/name/<search>", methods=["GET"])
|
||||
def api_get_unit_by_name(search):
|
||||
"""Route to get information about a unit"""
|
||||
session = Session()
|
||||
try:
|
||||
data = session.query(Unit.name, Unit.description).filter(
|
||||
Unit.name == search).one()
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
data2 = {'name': data.name, 'description': data.description}
|
||||
return jsonify(data2)
|
||||
|
||||
|
||||
@app.route("/api/v1/purchases", methods=["GET"])
|
||||
def api_list_purchases():
|
||||
"""Route to list all purchases"""
|
||||
session = Session()
|
||||
try:
|
||||
data = {}
|
||||
query = session.query(Purchase.id,
|
||||
Purchase.product_upc, Purchase.quantity, Purchase.date,
|
||||
Purchase.location)
|
||||
for purchase_id, upc, quantity, date, location in query:
|
||||
data[purchase_id] = {'upc': upc, 'quantity': quantity,
|
||||
'date': str(date), 'location': location}
|
||||
return jsonify(data)
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@app.route("/api/v1/purchase/id/<search>", methods=["GET"])
|
||||
def api_get_purchase_by_id(search):
|
||||
"""Route to get information about a purchase"""
|
||||
session = Session()
|
||||
try:
|
||||
data = session.query(Purchase.id, Purchase.product_upc, Purchase.quantity,
|
||||
Purchase.date, Purchase.location).filter(Purchase.id == search).one()
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
data2 = {'id': data.id, 'upc': data.product_upc,
|
||||
'quantity': data.quantity, 'date': str(data.date), 'location': data.location}
|
||||
return jsonify(data2)
|
||||
|
||||
|
||||
@app.route("/api/v1/uses", methods=["GET"])
|
||||
def api_list_uses():
|
||||
"""Route to list all uses"""
|
||||
session = Session()
|
||||
try:
|
||||
data = {}
|
||||
query = session.query(Use.id,
|
||||
Use.product_upc, Use.quantity, Use.date,
|
||||
Use.location)
|
||||
for use_id, upc, quantity, date, location in query:
|
||||
data[use_id] = {'upc': upc, 'quantity': quantity,
|
||||
'date': str(date), 'location': location}
|
||||
return jsonify(data)
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@app.route("/api/v1/use/id/<search>", methods=["GET"])
|
||||
def api_get_use_by_id(search):
|
||||
"""Route to get information about a use"""
|
||||
session = Session()
|
||||
try:
|
||||
data = session.query(Use.id, Use.product_upc, Use.quantity,
|
||||
Use.date, Use.location).filter(Use.id == search).one()
|
||||
except NoResultFound:
|
||||
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
||||
data2 = {'id': data.id, 'upc': data.product_upc,
|
||||
'quantity': data.quantity, 'date': str(data.date), 'location': data.location}
|
||||
return jsonify(data2)
|
||||
|
||||
|
||||
@app.route("/api/v1/create_location", methods=["POST"])
|
||||
def api_create_location():
|
||||
"""Route to create a new location"""
|
||||
@ -168,10 +255,9 @@ def api_create_location():
|
||||
else:
|
||||
locdesc = None
|
||||
|
||||
ins = sqlalchemy.insert(Location).values(name=locname, description=locdesc)
|
||||
print(f"ins is {ins}")
|
||||
session.execute(ins)
|
||||
print("executed ins")
|
||||
newlocation = Location(name=locname, description=locdesc)
|
||||
session.add(newlocation)
|
||||
print("added newlocation")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
@ -191,11 +277,9 @@ def api_create_brand():
|
||||
else:
|
||||
branddesc = None
|
||||
|
||||
ins = sqlalchemy.insert(Brand).values(
|
||||
name=brandname, description=branddesc)
|
||||
print(f"ins is {ins}")
|
||||
session.execute(ins)
|
||||
print("executed ins")
|
||||
newbrand = Brand(name=brandname, description=branddesc)
|
||||
session.add(newbrand)
|
||||
print("added newbrand")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
@ -215,13 +299,12 @@ def api_create_product():
|
||||
else:
|
||||
desc = None
|
||||
|
||||
ins = sqlalchemy.insert(Product).values(upc=request.form['upc'], brand=request.form['brand'],
|
||||
newproduct = Product(upc=request.form['upc'], brand=request.form['brand'],
|
||||
name=request.form['name'], size=request.form['size'],
|
||||
sizeunit=request.form['sizeunit'],
|
||||
description=desc)
|
||||
print(f"ins is {ins}")
|
||||
session.execute(ins)
|
||||
print("executed ins")
|
||||
session.add(newproduct)
|
||||
print("added newproduct")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
@ -229,5 +312,65 @@ def api_create_product():
|
||||
f'/api/v1/product/name/{request.form["name"]}']})
|
||||
|
||||
|
||||
@app.route("/api/v1/create_unit", methods=["POST"])
|
||||
def api_create_unit():
|
||||
"""Route to create a new unit"""
|
||||
session = Session()
|
||||
if "name" not in request.form:
|
||||
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
||||
else:
|
||||
unitname = request.form['name']
|
||||
if "description" in request.form:
|
||||
unitdesc = request.form['description']
|
||||
else:
|
||||
unitdesc = None
|
||||
|
||||
newunit = Unit(name=unitname, description=unitdesc)
|
||||
session.add(newunit)
|
||||
print("added newunit")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
return jsonify({'api_endpoints': [f'/api/v1/unit/name/{unitname}']})
|
||||
|
||||
|
||||
@app.route("/api/v1/create_purchase", methods=["POST"])
|
||||
def api_create_purchase():
|
||||
"""Route to create a new purchase"""
|
||||
session = Session()
|
||||
reqfields = ['upc', 'quantity', 'date', 'location']
|
||||
for field in reqfields:
|
||||
if field not in request.form:
|
||||
return jsonify({'error': f'NO_{field.upper()}_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
||||
|
||||
newpurchase = Purchase(product_upc=request.form['upc'], quantity=request.form['quantity'],
|
||||
date=request.form['date'], location=request.form['location'])
|
||||
session.add(newpurchase)
|
||||
print("added newpurchase")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
return jsonify({'api_endpoints': [f'/api/v1/purchase/id/{newpurchase.id}']})
|
||||
|
||||
|
||||
@app.route("/api/v1/create_use", methods=["POST"])
|
||||
def api_create_use():
|
||||
"""Route to create a new use"""
|
||||
session = Session()
|
||||
reqfields = ['upc', 'quantity', 'date', 'location']
|
||||
for field in reqfields:
|
||||
if field not in request.form:
|
||||
return jsonify({'error': f'NO_{field.upper()}_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
||||
|
||||
newuse = Use(product_upc=request.form['upc'], quantity=request.form['quantity'],
|
||||
date=request.form['date'], location=request.form['location'])
|
||||
session.add(newuse)
|
||||
print("added newuse")
|
||||
session.commit()
|
||||
print("committed")
|
||||
|
||||
return jsonify({'api_endpoints': [f'/api/v1/use/id/{newuse.id}']})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Run with `flask` or a WSGI server!")
|
||||
|
Loading…
Reference in New Issue
Block a user