Skip to content

Commit

Permalink
Complete all TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadabdulnasir committed Jun 16, 2022
1 parent 7aa8cac commit 143115f
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 116 deletions.
176 changes: 116 additions & 60 deletions backend/src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def drink_list():
'drinks': [d.short() for d in drinks]
}), 200



@app.route('/drinks-detail', methods=['GET'])
@requires_auth('get:drinks-detail')
def drink_list_detail(payload):
Expand All @@ -58,8 +56,6 @@ def drink_list_detail(payload):
'drinks': [d.long() for d in drinks]
}), 200



@app.route('/drinks', methods=['POST'])
@requires_auth('post:drinks')
def drink_create(payload):
Expand All @@ -73,80 +69,140 @@ def drink_create(payload):
or appropriate status code indicating reason for failure
'''
# Get the body
req = request.get_json()
body = request.get_json()
try:
# create new Drink
drink = Drink()
drink.title = req['title']
# convert recipe to String
drink.recipe = json.dumps(req['recipe'])
# insert the new Drink
# Prepare Drink for DB
drink = Drink(
title=body.get("title"),
recipe=json.dumps(body.get("recipe"))
)
# Commit to DB
drink.insert()

except Exception:
abort(400)

return jsonify({'success': True, 'drinks': [drink.long()]})

@app.route('/drinks/<int:id>', methods=['PATCH'])
@requires_auth('patch:drinks')
def drink_update(payload, id):
'''
@TODO implement endpoint
PATCH /drinks/<id>
where <id> is the existing model id
it should respond with a 404 error if <id> is not found
it should update the corresponding row for <id>
it should require the 'patch:drinks' permission
it should contain the drink.long() data representation
returns status code 200 and json {"success": True, "drinks": drink} where drink an array containing only the updated drink
or appropriate status code indicating reason for failure
'''
# Get the body
body = request.get_json()

'''
@TODO implement endpoint
PATCH /drinks/<id>
where <id> is the existing model id
it should respond with a 404 error if <id> is not found
it should update the corresponding row for <id>
it should require the 'patch:drinks' permission
it should contain the drink.long() data representation
returns status code 200 and json {"success": True, "drinks": drink} where drink an array containing only the updated drink
or appropriate status code indicating reason for failure
'''
# Get the Drink with requested Id
drink = Drink.query.filter(Drink.id == id).one_or_none()

# abort, drink not found
if not drink:
abort(404)

'''
@TODO implement endpoint
DELETE /drinks/<id>
where <id> is the existing model id
it should respond with a 404 error if <id> is not found
it should delete the corresponding row for <id>
it should require the 'delete:drinks' permission
returns status code 200 and json {"success": True, "delete": id} where id is the id of the deleted record
or appropriate status code indicating reason for failure
'''
try:

title = body.get('title')
recipe = body.get('recipe')

# Error Handling
'''
Example error handling for unprocessable entity
'''
# check if we should update title
if title:
drink.title = title

# check if we should update recipe
if recipe:
drink.recipe = json.dumps(body.get("recipe"))

@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422
# update the drink
drink.update()
except Exception:
abort(400)

return jsonify({'success': True, 'drinks': [drink.long()]}), 200

'''
@TODO implement error handlers using the @app.errorhandler(error) decorator
each error handler should return (with approprate messages):
jsonify({
"success": False,
"error": 404,
"message": "resource not found"
}), 404
@app.route('/drinks/<int:id>', methods=['DELETE'])
@requires_auth('delete:drinks')
def drink_delete(payload, id):
'''
@TODO implement endpoint
DELETE /drinks/<id>
where <id> is the existing model id
it should respond with a 404 error if <id> is not found
it should delete the corresponding row for <id>
it should require the 'delete:drinks' permission
returns status code 200 and json {"success": True, "delete": id} where id is the id of the deleted record
or appropriate status code indicating reason for failure
'''
# Get the Drink with requested Id
drink = Drink.query.filter(Drink.id == id).one_or_none()

'''
# abort when drink not found
if not drink:
abort(404)

'''
@TODO implement error handler for 404
error handler should conform to general task above
'''
try:
# delete the drink
drink.delete()
except Exception:
abort(400)

return jsonify({'success': True, 'delete': id}), 200

'''
@TODO implement error handler for AuthError
error handler should conform to general task above
'''
# Error Handling
@app.errorhandler(404)
def not_found_error(error):
return jsonify({"success": False, "error": 404, "message": "Not Found!!!"}), 404

@app.errorhandler(406)
def not_found_error(error):
return (
jsonify({"success": False, "error": 406,
"message": "Not Acceptable!!!"}),
406,
)

@app.errorhandler(422)
def unprocessable_error(error):
return (
jsonify(
{"success": False, "error": 422,
"message": "Request Unprocessable!!!"}
),
422,
)

@app.errorhandler(400)
def bad_request_error(error):
return (
jsonify({"success": False, "error": 400,
"message": "Bad request!!!"}),
400,
)

@app.errorhandler(500)
def server_error(error):
return (
jsonify({"success": False, "error": 500,
"message": "Server Error!!!"}),
500,
)

@app.errorhandler(AuthError)
def auth_error(error):
'''
@DONE implement error handler for AuthError
error handler should conform to general task above
'''
return jsonify({
"success": False,
"error": error.status_code,
"message": error.error.get("description")
}), error.status_code
Loading

0 comments on commit 143115f

Please sign in to comment.