Skip to content

Commit

Permalink
fix: Added GET request to recipe-material map, added save_to_db in re…
Browse files Browse the repository at this point in the history
…cipe resource
  • Loading branch information
lucasbpro committed Apr 4, 2021
1 parent 4cabea1 commit b3697d0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# --- Strings
'HELP_PARSER' : "This field can not be left blank!",
'ITEM_EXISTS' : "An item with descripton '{}' already exists.",
'MATERIAL_NOT_EXIST' : "Material id '{}' provided was not found. Recipe not created/updated.",
'INSERT_FAIL' : "An error occurred upon inserting the item into the database.",
'DELETED' : "Item was deleted.",
'ID_NOT_FOUND' : "The provided id does not exist in the database."
Expand Down
Binary file modified data.db
Binary file not shown.
1 change: 1 addition & 0 deletions resources/raw_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ 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('supplier_name',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)
Expand Down
36 changes: 28 additions & 8 deletions resources/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class Recipe(Resource):
parser.add_argument('supply_cost',type=float,required=False)
parser.add_argument('materials', type=dict, action="append",required=False)

# to handle HTTP GET /recipe?id=<int:id>
# to handle HTTP GET /recipes/<int:id>
def get(self, id):
#id_ = request.args.get('id')
recipe = RecipeModel.find_by_id(id)
return recipe.json(), 200
if recipe:
return recipe.json(), 200
else:
return {'message': constants['ID_NOT_FOUND']}

def delete(self, id):
# checks if material exists in database
Expand Down Expand Up @@ -73,6 +75,9 @@ def put(self, id):
else:
materialRecipeItem = RecipeMaterialAmountModel(id, materialId, materialAmount)
materialRecipeItem.save_to_db()
# in case any materialId provided does not exist
else:
return {"message": constants['MATERIAL_NOT_EXIST'].format(materialId)}, 400

recipe.last_update = datetime.now().strftime("%d/%m/%Y %H:%M")

Expand All @@ -90,9 +95,11 @@ def put(self, id):
# in case recipe-material map already exists, then just updates it
if materialRecipeItem:
materialRecipeItem.amount = materialAmount
materialRecipeItem.save_to_db()
# otherwise, create a new recipe-material map
else:
materialRecipeItem = RecipeMaterialAmountModel(id, materialId, materialAmount)
materialRecipeItem.save_to_db()

# tries to insert in database
# returns 500 (internal server error) in case of database failure
Expand Down Expand Up @@ -132,13 +139,26 @@ def post(self):
# in case it does not exist, creates a new recipe using data passed
# along with the HTTP request
recipe = RecipeModel(data['description'],data['labor_cost'],data['supply_cost'])

# links the recipe to all related materials
for id in data['materials']:
material = RawMaterialModel.find_by_id(id)
materialsDict = data['materials'][0]
for key in materialsDict.keys():
materialId = int(key)
materialAmount = materialsDict[key]
material = RawMaterialModel.find_by_id(materialId)
if material:
recipe.materials.append(material)

materialRecipeItem = RecipeMaterialAmountModel.find_by_map(recipe.id, materialId)
# in case recipe-material map already exists, then just updates it
if materialRecipeItem:
materialRecipeItem.amount = materialAmount
materialRecipeItem.save_to_db()
# otherwise, create a new recipe-material map
else:
materialRecipeItem = RecipeMaterialAmountModel(recipe.id, materialId, materialAmount)
materialRecipeItem.save_to_db()
# in case any materialId provided does not exist
else:
return {"message": constants['MATERIAL_NOT_EXIST'].format(materialId)}, 200

# tries to insert in database
# returns 500 (internal server error) in case of database failure
try:
Expand Down
9 changes: 9 additions & 0 deletions resources/recipe_material_amt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class RecipeMaterialAmount(Resource):
parser = reqparse.RequestParser()
parser.add_argument('amount',type=float,required=True)

# handles HTTP GET /recipe/<int:recipe_id>/material/<int:material_id>
def get(self, recipe_id, material_id):
recipe_material = RecipeMaterialAmountModel.find_by_map(recipe_id, material_id)
if recipe_material:
return {"amount" : recipe_material.amount}, 200
else:
return {'Message': constants['ID_NOT_FOUND']}


# handles HTTP PUT /recipe/<int:recipe_id>/material/<int:material_id>
def put(self, recipe_id, material_id):
# gets parameter from parser
Expand Down

0 comments on commit b3697d0

Please sign in to comment.