Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasbpro committed May 18, 2021
2 parents 4d6ebc7 + 5a0396a commit a6740ae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
Binary file modified data.db
Binary file not shown.
15 changes: 7 additions & 8 deletions models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ class ProductModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))
stock_amt = db.Column(db.Integer)
last_update = db.Column(db.String(constants['MEDIUM_LENGTH']))
fabrication_date = db.Column(db.String(constants['MEDIUM_LENGTH']))

# define relationships with other tables
recipe = db.relationship('RecipeModel')

def __init__(self, recipe_id, stock_amt):
def __init__(self, recipe_id):
self.recipe_id = recipe_id
self.stock_amt = stock_amt or 0
self.last_update = datetime.now().strftime("%d/%m/%Y %H:%M")
self.fabrication_date = datetime.now().strftime("%d/%m/%Y %H:%M")

def json(self):
return {
'id' : self.id,
'recipe_id' : self.recipe_id,
'stock_amt' : self.stock_amt,
'last_update' : self.last_update
'fabrication_date' : self.fabrication_date
}

def save_to_db(self):
Expand All @@ -50,5 +48,6 @@ def find_by_recipe_id(cls, id_):
def get_recipe(self):
return [self.recipe.json()]

def get_stock_amount(self):
return self.json().stock_amt
@classmethod
def get_stock_amount(cls, id_):
return len(cls.query.filter_by(recipe_id=id_))
5 changes: 4 additions & 1 deletion models/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ def get_materials(self):
return [material.json() for material in self.materials]

def get_materials_amount(self):
return [item.json() for item in RecipeMaterialAmountModel.query.all()]
return [item.json() for item in RecipeMaterialAmountModel.query.all()]

def get_products_from_recipe(self):
return [product.json() for product in self.products]
22 changes: 7 additions & 15 deletions resources/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ def put(self, id):
# in case it exists, updates it
if product:
for key in data.keys():
if key=='stock_amt' and data['stock_amt']:
product.stock_amt = data['stock_amt']
product.last_update = datetime.now().strftime("%d/%m/%Y %H:%M")
if key=='recipe_id' and data['recipe_id']:
recipe = RecipeModel.find_by_id(data['recipe_id'])
if recipe:
product.recipe_id = data['recipe_id']
product.recipe = recipe
product.last_update = datetime.now().strftime("%d/%m/%Y %H:%M")
product.fabrication_date = 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
Expand All @@ -72,7 +69,6 @@ class Products(Resource):
# adds a parser to handle POST HTTP requests
parser = reqparse.RequestParser()
parser.add_argument('recipe_id',type=int,required=True)
parser.add_argument('stock_amt',type=int,required=False)

# handles HTTP request GET /products
def get(self):
Expand All @@ -83,16 +79,12 @@ def post(self):
# gets parameter from parser
data = Products.parser.parse_args()

# checks if material exists in database
product = ProductModel.find_by_recipe_id(data['recipe_id'])

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

# in case it does not exist, creates a new material using data passed
# along with the HTTP request
product = ProductModel(data['recipe_id'], data['stock_amt'])
recipe = RecipeModel.find_by_id(data['recipe_id'])
if recipe:
product = ProductModel(data['recipe_id'])
# in case provided recipe does not exist, throw error message
else:
return {"message": "A recipe with id '{}' does not exist.".format(data['recipe_id'])}, 400

# tries to insert in database
# returns 500 (internal server error) in case of database failure
Expand Down

0 comments on commit a6740ae

Please sign in to comment.