Skip to content

Commit

Permalink
feat: changed strucuture of API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasbpro committed Oct 5, 2020
1 parent 13e2356 commit 67b2422
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__/
inserts/
venv/
data.db
21 changes: 9 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#from security import authenticate, identity

# import resorces
from resources.raw_material import RawMaterial, RawMaterialList
from resources.raw_material import *
from resources.recipe import *
from resources.customers import *
from resources.orders import *
Expand All @@ -31,21 +31,18 @@
# jwt = JWT(app, authenticate, identity) # /auth

# Sets up API endpoints
api.add_resource(RawMaterial, '/raw_material/<int:id>')
api.add_resource(RawMaterialList, '/raw_materials')
api.add_resource(RawMaterial, '/raw_materials/<int:id>')
api.add_resource(RawMaterials, '/raw_materials')

api.add_resource(Recipe, '/recipe/<int:id>')
api.add_resource(RecipePost, '/recipe')
api.add_resource(RecipeList, '/recipes')
api.add_resource(Recipe, '/recipes/<int:id>')
api.add_resource(Recipes, '/recipes')
api.add_resource(MaterialList, '/recipe/<int:id>/materials')

api.add_resource(Customer, '/customer/<int:id>')
api.add_resource(CustomerPost, '/customer')
api.add_resource(CustomerList, '/customers')
api.add_resource(Customer, '/customers/<int:id>')
api.add_resource(Customers, '/customers')

api.add_resource(Order, '/order/<int:id>')
api.add_resource(OrderPost, '/order')
api.add_resource(OrderList, '/orders')
api.add_resource(Order, '/orders/<int:id>')
api.add_resource(Orders, '/orders')

# api.add_resource(UserRegister, '/register')

Expand Down
Binary file modified data.db
Binary file not shown.
2 changes: 1 addition & 1 deletion models/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OrderModel(db.Model):
def __init__(self, product_id, customer_id, notes):
self.product_id = product_id;
self.customer_id = customer_id;
self.notes = notes
self.notes = notes;
self.order_total = 0;
self.status_fabrication = "Fabricação a iniciar";
self.status_payment = "Pagamento pendente";
Expand Down
2 changes: 1 addition & 1 deletion models/raw_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def delete_from_db(self):
db.session.commit()

@classmethod
def find_by_name(cls, name):
def find_by_description(cls, name):
return cls.query.filter_by(description=name).first()

@classmethod
Expand Down
18 changes: 9 additions & 9 deletions resources/customers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# import flask libs
from flask_restful import Resource, reqparse
from flask import request
from constants import constants
#from flask_jwt import jwt_required

# import model
Expand Down Expand Up @@ -30,7 +30,7 @@ def delete(self, id):
customer.delete_from_db()

# return message and default HTTP status (200 - OK)
return {'Message': 'Customer has been deleted'}
return {'Message': constants['DELETED']}

def put(self, id):
# gets parameter from parser
Expand Down Expand Up @@ -64,16 +64,21 @@ def put(self, id):
# returns
return customer.json()

class CustomerPost(Resource):
class Customers(Resource):
# adds a parser to handle POST HTTP requests
parser = reqparse.RequestParser()
parser.add_argument('name',type=str,required=True)
parser.add_argument('email',type=str,required=False)
parser.add_argument('birth_date',type=str,required=False)

# handles HTTP request GET /customers
def get(self):
return {'Customers': [x.json() for x in CustomerModel.query.all()]}

# handles HTTP request POST /customers
def post(self):
# gets parameter from parser
data = CustomerPost.parser.parse_args()
data = Customers.parser.parse_args()

# checks if material exists in database
customer = CustomerModel.find_by_name(data['name'])
Expand All @@ -96,8 +101,3 @@ def post(self):
# returns JSON with the created Material and returns CREATED status (201)
return customer.json(), 201

# class used to get the whole list of materials in the database
# route: /customers
class CustomerList(Resource):
def get(self):
return {'Customers': [x.json() for x in CustomerModel.query.all()]}
20 changes: 10 additions & 10 deletions resources/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from models.orders import OrderModel

class Order(Resource):
# to handle HTTP GET /order/<int:id>
# to handle HTTP GET /orders/<int:id>
def get(self, id):
order = OrderModel.find_by_id(id)
if order:
return order.json(), 200
else:
return {'Message': constants['ID_NOT_FOUND']}

# to handle HTTP DEL /orders/<int:id>
def delete(self, id):
# checks if material exists in database
order = OrderModel.find_by_id(id)
Expand All @@ -27,16 +28,21 @@ def delete(self, id):
# return message and default HTTP status (200 - OK)
return {'Message': constants['DELETED']}

class OrderPost(Resource):
class Orders(Resource):
# parser to handle POST request for /order route
parser = reqparse.RequestParser();
parser.add_argument('product_id',type=int,required=True);
parser.add_argument('customer_id',type=int,required=True);
parser.add_argument('notes',type=str,required=False);

# handles HTTP request GET /orders
def get(self):
return {'orders': [x.json() for x in OrderModel.query.all()]}

# handles HTTP request POST /orders
def post(self):
# gets parameter from parser
data = OrderPost.parser.parse_args()
data = Orders.parser.parse_args()

# creates a new order
order = OrderModel(**data)
Expand All @@ -48,10 +54,4 @@ def post(self):
return {"message": "An error occurred upon inserting the into the database."}, 500

# returns JSON with the created Material and returns CREATED status (201)
return order.json(), 201

# class used to get the whole list of materials in the database
# request GET /orders
class OrderList(Resource):
def get(self):
return {'orders': [x.json() for x in OrderModel.query.all()]}
return order.json(), 201
92 changes: 52 additions & 40 deletions resources/raw_material.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# import flask libs
from flask_restful import Resource, reqparse
from flask import request
from constants import constants
#from flask_jwt import jwt_required

# import model
from models.raw_material import RawMaterialModel

class RawMaterial(Resource):

# adds a parser to handle PUT an POST HTTP requests
# adds a parser to handle PUT HTTP requests
parser = reqparse.RequestParser()
parser.add_argument('description',type=str,required=False)
parser.add_argument('package_price',type=float,required=False)
Expand All @@ -17,57 +17,33 @@ class RawMaterial(Resource):
parser.add_argument('stock_amt',type=int,required=False)
parser.add_argument('sell_by_date',type=str,required=False)

# TODO: to handle HTTP GET /raw_material?id=?<int:id>
def get(self):
id_ = request.args.get('id')
raw_material = RawMaterialModel.find_by_id(id_)
return raw_material.json(), 200

def post(self):
# gets parameter from parser
data = RawMaterial.parser.parse_args()

# checks if material exists in database
id_ = request.args.get('id')
raw_material = RawMaterialModel.find_by_id(id_)

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
# handles HTTP GET /raw_materials/<int:id>
def get(self, id):
raw_material = RawMaterialModel.find_by_id(id)
if raw_material:
return {'message': "A raw material with descripton '{}' already exists.".format(description)}, 400

# in case it does not exist, creates a new material using data passed
# along with the HTTP request
raw_material = RawMaterialModel(**data)

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
raw_material.save_to_db()
except:
return {"message": "An error occurred upon inserting the into the database."}, 500

# returns JSON with the created Material and returns CREATED status (201)
return raw_material.json(), 201
return raw_material.json(), 200
else:
return {'Message': constants['ID_NOT_FOUND']}

def delete(self):
# handles HTTP DEL /raw_materials/<int:id>
def delete(self, id):
# checks if material exists in database
id_ = request.args.get('id')
raw_material = RawMaterialModel.find_by_id(id_)
raw_material = RawMaterialModel.find_by_id(id)

# in case it exists, delete it
if raw_material:
raw_material.delete_from_db()

# return message and default HTTP status (200 - OK)
return {'message': 'Item deleted'}
return {'message': constants['DELETED']}

def put(self):
# handles HTTP PUT /raw_materials/<int:id>
def put(self, id):
# gets parameter from parser
data = RawMaterial.parser.parse_args()

# checks if material exists in database
id_ = request.args.get('id')
raw_material = RawMaterialModel.find_by_id(id_)
raw_material = RawMaterialModel.find_by_id(id)

# in case it exists, updates it
if raw_material:
Expand Down Expand Up @@ -100,6 +76,42 @@ def put(self):

# class used to get the whole list of materials in the database
# route: /raw_materials
class RawMaterialList(Resource):
class RawMaterials(Resource):
# adds a parser to handle POST HTTP requests
parser = reqparse.RequestParser()
parser.add_argument('description',type=str,required=False)
parser.add_argument('package_price',type=float,required=False)
parser.add_argument('package_amt',type=int,required=False)
parser.add_argument('unit_material',type=str,required=False)
parser.add_argument('stock_amt',type=int,required=False)
parser.add_argument('sell_by_date',type=str,required=False)

# handles HTTTP request GET /raw_materials
def get(self):
return {'Raw Materials': [x.json() for x in RawMaterialModel.query.all()]}

# handles HTTTP request POST /raw_materials
def post(self):
# gets parameter from parser
data = RawMaterials.parser.parse_args()

# checks if material exists in database
raw_material = RawMaterialModel.find_by_description(data['description'])

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
if raw_material:
return {'message': "A raw material with descripton '{}' already exists.".format(description)}, 400

# in case it does not exist, creates a new material using data passed
# along with the HTTP request
raw_material = RawMaterialModel(**data)

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
raw_material.save_to_db()
except:
return {"message": "An error occurred upon inserting the into the database."}, 500

# returns JSON with the created Material and returns CREATED status (201)
return raw_material.json(), 201
16 changes: 8 additions & 8 deletions resources/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ def put(self, id):
return recipe.json()


class RecipePost(Resource):
class Recipes(Resource):
# adds a parser to handle POST HTTP requests
parser = reqparse.RequestParser()
parser.add_argument('description',type=str,required=True)
parser.add_argument('labor_cost',type=float,required=True)
parser.add_argument('supply_cost',type=float,required=True)

# handles HTTP request GET /recipes
def get(self):
return {'Recipes': [x.json() for x in RecipeModel.query.all()]}

# handles HTTP request POST /recipes
def post(self):
# gets parameter from parser
data = RecipePost.parser.parse_args()
data = Recipes.parser.parse_args()

# checks if material exists in database
recipe = RecipeModel.find_by_description(data['description'])
Expand All @@ -103,11 +108,6 @@ def post(self):
# returns JSON with the created Material and returns CREATED status (201)
return recipe.json(), 201

# class used to get the whole list of recipes from the database
class RecipeList(Resource):
def get(self):
return {'Recipes': [x.json() for x in RecipeModel.query.all()]}

# class used to get the whole list of materials in a recipe
class MaterialList(Resource):
# route: recipe/<int:id>/materials
Expand All @@ -118,4 +118,4 @@ def get(self, id):
if recipe:
return {'Materials': [x.json() for x in recipe.get_all_materials()]}
else:
return {'message' : constants['ID_NOT_FOUND']}
return {'message' : constants['ID_NOT_FOUND']}

0 comments on commit 67b2422

Please sign in to comment.