diff --git a/.gitignore b/.gitignore index 7631bcf..bf37456 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ inserts/ venv/ +data.db \ No newline at end of file diff --git a/app.py b/app.py index 072d316..4495aad 100644 --- a/app.py +++ b/app.py @@ -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 * @@ -31,21 +31,18 @@ # jwt = JWT(app, authenticate, identity) # /auth # Sets up API endpoints -api.add_resource(RawMaterial, '/raw_material/') -api.add_resource(RawMaterialList, '/raw_materials') +api.add_resource(RawMaterial, '/raw_materials/') +api.add_resource(RawMaterials, '/raw_materials') -api.add_resource(Recipe, '/recipe/') -api.add_resource(RecipePost, '/recipe') -api.add_resource(RecipeList, '/recipes') +api.add_resource(Recipe, '/recipes/') +api.add_resource(Recipes, '/recipes') api.add_resource(MaterialList, '/recipe//materials') -api.add_resource(Customer, '/customer/') -api.add_resource(CustomerPost, '/customer') -api.add_resource(CustomerList, '/customers') +api.add_resource(Customer, '/customers/') +api.add_resource(Customers, '/customers') -api.add_resource(Order, '/order/') -api.add_resource(OrderPost, '/order') -api.add_resource(OrderList, '/orders') +api.add_resource(Order, '/orders/') +api.add_resource(Orders, '/orders') # api.add_resource(UserRegister, '/register') diff --git a/data.db b/data.db index c928efc..af459a2 100644 Binary files a/data.db and b/data.db differ diff --git a/models/orders.py b/models/orders.py index 443fa0c..7339610 100644 --- a/models/orders.py +++ b/models/orders.py @@ -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"; diff --git a/models/raw_material.py b/models/raw_material.py index 5e1f144..6ec4a41 100644 --- a/models/raw_material.py +++ b/models/raw_material.py @@ -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 diff --git a/resources/customers.py b/resources/customers.py index 4b5c466..596344f 100644 --- a/resources/customers.py +++ b/resources/customers.py @@ -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 @@ -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 @@ -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']) @@ -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()]} diff --git a/resources/orders.py b/resources/orders.py index 3905812..7f50292 100644 --- a/resources/orders.py +++ b/resources/orders.py @@ -8,7 +8,7 @@ from models.orders import OrderModel class Order(Resource): - # to handle HTTP GET /order/ + # to handle HTTP GET /orders/ def get(self, id): order = OrderModel.find_by_id(id) if order: @@ -16,6 +16,7 @@ def get(self, id): else: return {'Message': constants['ID_NOT_FOUND']} + # to handle HTTP DEL /orders/ def delete(self, id): # checks if material exists in database order = OrderModel.find_by_id(id) @@ -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) @@ -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 \ No newline at end of file diff --git a/resources/raw_material.py b/resources/raw_material.py index f4f5184..8cbed32 100644 --- a/resources/raw_material.py +++ b/resources/raw_material.py @@ -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 @@ -8,7 +8,7 @@ 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) @@ -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=? - 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/ + 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/ + 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/ + 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: @@ -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 \ No newline at end of file diff --git a/resources/recipe.py b/resources/recipe.py index 514346f..a76b2e9 100644 --- a/resources/recipe.py +++ b/resources/recipe.py @@ -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']) @@ -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//materials @@ -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']} \ No newline at end of file