Skip to content

Commit

Permalink
feat: changed general route strucuture
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasbpro committed Oct 4, 2020
1 parent 22d7f02 commit 13e2356
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 105 deletions.
16 changes: 11 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
# jwt = JWT(app, authenticate, identity) # /auth

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

api.add_resource(Recipe, '/recipe/<int:id>')
api.add_resource(RecipePost, '/recipe')
api.add_resource(RecipeList, '/recipes')
api.add_resource(MaterialList, '/recipe/<int:recipe_id>')
api.add_resource(Customer, '/customer')
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(Order, '/order')

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

# api.add_resource(UserRegister, '/register')
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'HELP_PARSER' : "This field can not be left blank!",
'ITEM_EXISTS' : "An item with descripton '{}' already exists.",
'INSERT_FAIL' : "An error occurred upon inserting the item into the database.",
'DELETED' : "Item with description '{}' was deleted.",
'DELETED' : "Item was deleted.",
'ID_NOT_FOUND' : "The provided id does not exist in the database."
}
Binary file modified data.db
Binary file not shown.
85 changes: 45 additions & 40 deletions resources/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,23 @@
from models.customers import CustomerModel

class Customer(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('name',type=str,required=False)
parser.add_argument('email',type=str,required=False)
parser.add_argument('birth_date',type=str,required=False)

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

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

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

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
def get(self, id):
customer = CustomerModel.find_by_id(id)
if customer:
return {'message': "A customer with name '{}' already exists.".format(data['name'])}, 400

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

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
customer.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 customer.json(), 201
return customer.json(), 200
else:
return {'Message': constants['ID_NOT_EXIST']}

def delete(self):
def delete(self, id):
# checks if material exists in database
id_ = request.args.get('id')
customer = CustomerModel.find_by_id(id_)
customer = CustomerModel.find_by_id(id)

# in case it exists, delete it
if customer:
Expand All @@ -58,22 +32,21 @@ def delete(self):
# return message and default HTTP status (200 - OK)
return {'Message': 'Customer has been deleted'}

def put(self):
def put(self, id):
# gets parameter from parser
data = Customer.parser.parse_args()

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

# in case it exists, updates it
if customer:
for key in data.keys():
if key=='name':
if key=='name' and data['name']:
customer.name = data['name']
if key=='email':
if key=='email' and data['email']:
customer.email = data['email']
if key=='birth_date':
if key=='birth_date' and data['birth_date']:
customer.birth_date = data['birth_date']

# in case it does not exist, creates a new material using data passed
Expand All @@ -91,6 +64,38 @@ def put(self):
# returns
return customer.json()

class CustomerPost(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)

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

# checks if material exists in database
customer = CustomerModel.find_by_name(data['name'])

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
if customer:
return {'message': "A customer with name '{}' already exists.".format(data['name'])}, 400

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

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
customer.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 customer.json(), 201

# class used to get the whole list of materials in the database
# route: /customers
class CustomerList(Resource):
Expand Down
42 changes: 22 additions & 20 deletions resources/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@
from flask_restful import Resource, reqparse
from flask import request
from datetime import datetime
from constants import constants
#from flask_jwt import jwt_required

from models.orders import OrderModel

class Order(Resource):
# to handle HTTP GET /order/<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']}

def delete(self, id):
# checks if material exists in database
order = OrderModel.find_by_id(id)

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

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

# adds a parser to handle POST HTTP requests
class OrderPost(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);

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

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

# creates a new order
order = OrderModel(**data)
Expand All @@ -36,18 +50,6 @@ def post(self):
# returns JSON with the created Material and returns CREATED status (201)
return order.json(), 201

def delete(self):
# checks if material exists in database
id_ = request.args.get('id')
order = OrderModel.find_by_id(id_)

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

# return message and default HTTP status (200 - OK)
return {'Message': 'Order has been deleted'}

# class used to get the whole list of materials in the database
# request GET /orders
class OrderList(Resource):
Expand Down
82 changes: 43 additions & 39 deletions resources/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,28 @@ class Recipe(Resource):
parser.add_argument('supply_cost',type=float,required=False)

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

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

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

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
if recipe:
return {'message': constants['ITEM_EXISTS'].format(description)}, 400

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

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
recipe.save_to_db()
except:
return {"message": constants['INSERT_FAIL']}, 500

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

def delete(self):
def delete(self, id):
# checks if material exists in database
id_ = request.args.get('id')
recipe = RecipeModel.find_by_id(id_)
recipe = RecipeModel.find_by_id(id)

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

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

def put(self):
def put(self, id):
# gets parameter from parser
data = Recipe.parser.parse_args()

# checks if item exists in database
id_ = request.args.get('id')
recipe = RecipeModel.find_by_id(id_)
recipe = RecipeModel.find_by_id(id)

# in case it exists, updates it
if recipe:
Expand Down Expand Up @@ -99,17 +71,49 @@ def put(self):
return recipe.json()


class RecipePost(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)

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

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

# in case it exists, returns a message and HTTP 400 code (BAD REQUEST)
if recipe:
return {'message': constants['ITEM_EXISTS'].format(description)}, 400

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

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
recipe.save_to_db()
except:
return {"message": constants['INSERT_FAIL']}, 500

# 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:recipe_id>
def get(self, recipe_id):
# route: recipe/<int:id>/materials
def get(self, id):

recipe = RecipeModel.find_by_id(recipe_id)
recipe = RecipeModel.find_by_id(id)

if recipe:
return {'Materials': [x.json() for x in recipe.get_all_materials()]}
Expand Down

0 comments on commit 13e2356

Please sign in to comment.