diff --git a/app.py b/app.py index 5c5c7dc..31b25ff 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,7 @@ from resources.recipe import * from resources.customers import * from resources.orders import * +from resources.recipe_material_amt import * # creates Flask application app = Flask(__name__) @@ -35,6 +36,7 @@ api.add_resource(Recipe, '/recipes/') api.add_resource(Recipes, '/recipes') api.add_resource(MaterialList, '/recipe//materials') +api.add_resource(RecipeMaterialAmount,'/recipe//material/') api.add_resource(Customer, '/customers/') api.add_resource(Customers, '/customers') diff --git a/models/recipe.py b/models/recipe.py index a38a4d7..a083292 100644 --- a/models/recipe.py +++ b/models/recipe.py @@ -2,6 +2,7 @@ from db import db from constants import constants from datetime import datetime +from models.recipe_material_amt import RecipeMaterialAmountModel # defines the model for 'recipes' table in db class RecipeModel(db.Model): @@ -52,3 +53,10 @@ def find_by_id(cls, id_): def get_materials(self): return [material.json() for material in self.materials] + + def get_materials_amount(self): + return [ { + "material_id": material.id, + "amount": RecipeMaterialAmountModel.find_by_material_id(material.id).amount + } + for material in self.materials] \ No newline at end of file diff --git a/models/recipe_material_amt.py b/models/recipe_material_amt.py new file mode 100644 index 0000000..b074d00 --- /dev/null +++ b/models/recipe_material_amt.py @@ -0,0 +1,54 @@ + +from db import db +from constants import constants # constants dictionary +from datetime import datetime + +# defines the model for 'recipe_material_amt' table in db +class RecipeMaterialAmountModel(db.Model): + + # name of the table in database + __tablename__ = 'recipe_material_amt' + + # define columns in table + id = db.Column(db.Integer, primary_key=True) + recipe_id = db.Column(db.Integer) + material_id = db.Column(db.Integer) + amount = db.Column(db.Float) + creation_date = db.Column(db.String(constants['MEDIUM_LENGTH'])) + last_update = db.Column(db.String(constants['MEDIUM_LENGTH'])) + + def __init__(self, recipe_id, material_id, amount): + self.recipe_id = recipe_id + self.material_id = material_id + self.amount = amount + self.creation_date = datetime.now().strftime("%d/%m/%Y %H:%M") + self.last_update = self.creation_date + + def json(self): + return {'id' : self.id, + 'recipe_id' : self.recipe_id, + 'material_id' : self.material_id, + 'amount' : self.amount, + 'creation_date': self.creation_date, + 'last_update' : self.last_update, + } + + def save_to_db(self): + db.session.add(self) + db.session.commit() + + def delete_from_db(self): + db.session.delete(self) + db.session.commit() + + @classmethod + def find_by_map(cls, recipe_id, material_id): + return cls.query.filter_by(recipe_id=recipe_id).filter_by(material_id=material_id).first() + + @classmethod + def find_by_recipe_id(cls, id): + return cls.query.filter_by(recipe_id=id).first() + + @classmethod + def find_by_material_id(cls, id): + return cls.query.filter_by(material_id=id).first() diff --git a/resources/recipe.py b/resources/recipe.py index 5cf878c..eb872c3 100644 --- a/resources/recipe.py +++ b/resources/recipe.py @@ -132,6 +132,6 @@ class MaterialList(Resource): def get(self, id): recipe = RecipeModel.find_by_id(id) if recipe: - return recipe.get_materials() + return recipe.get_materials_amount() else: return {'message' : constants['ID_NOT_FOUND']} \ No newline at end of file diff --git a/resources/recipe_material_amt.py b/resources/recipe_material_amt.py new file mode 100644 index 0000000..3df765e --- /dev/null +++ b/resources/recipe_material_amt.py @@ -0,0 +1,71 @@ +# import flask libs +from flask_restful import Resource, reqparse +from constants import constants +from datetime import datetime +#from flask_jwt import jwt_required + +# import model +from models.recipe_material_amt import RecipeMaterialAmountModel + +class RecipeMaterialAmount(Resource): + + # adds a parser to handle PUT HTTP requests + parser = reqparse.RequestParser() + parser.add_argument('amount',type=float,required=True) + + # handles HTTP PUT /recipe//material/ + def put(self, recipe_id, material_id): + # gets parameter from parser + data = RecipeMaterialAmount.parser.parse_args() + + # checks if material exists in database + recipe_material = RecipeMaterialAmountModel.find_by_map(recipe_id, material_id) + + # in case it exists, updates it + if recipe_material: + for key in data.keys(): + if key=='amount': + recipe_material.amount = data['amount'] + + recipe_material.last_update = datetime.now().strftime("%d/%m/%Y %H:%M") + + # in case it does not exist, creates a new material using data passed + # along with the HTTP request + else: + recipe_material = RecipeMaterialAmountModel(recipe_id, material_id, data['amount']) + + # tries to insert in database + # returns 500 (internal server error) in case of database failure + try: + recipe_material.save_to_db() + except: + return {"message": "An error occurred with the database."}, 500 + + # returns + return recipe_material.json() + + # handles HTTP POST /recipe//material/ + def post(self, recipe_id, material_id): + # gets parameter from parser + data = RecipeMaterialAmount.parser.parse_args() + + # checks if material exists in database + recipe_material = RecipeMaterialAmountModel.find_by_map(recipe_id, material_id) + + # in case it exists, returns a message and HTTP 400 code (BAD REQUEST) + if recipe_material: + return {'message': "The related recipe-material map already exists."}, 400 + + # in case it does not exist, creates a new material using data passed + # along with the HTTP request + recipe_material = RecipeMaterialAmountModel(recipe_id, material_id, data['amount']) + + # tries to insert in database + # returns 500 (internal server error) in case of database failure + try: + recipe_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 recipe_material.json(), 201