diff --git a/app.py b/app.py index 75b5cd2..67d8e05 100644 --- a/app.py +++ b/app.py @@ -30,21 +30,26 @@ def unhandled_exception(): obj['keyDoesntExist'] Inventory = { - 'wrench': 1, - 'nails': 1, - 'hammer': 1 + 'wrench': 100, + 'nails': 100, + 'hammer': 100 } def process_order(cart): global Inventory - tempInventory = Inventory + item_counts = {} for item in cart: - if Inventory[item['id']] <= 0: - raise Exception("Not enough inventory for " + item['id']) - else: - tempInventory[item['id']] -= 1 - print 'Success: ' + item['id'] + ' was purchased, remaining stock is ' + str(tempInventory[item['id']]) - Inventory = tempInventory + item_id = item['id'] + item_counts[item_id] = item_counts.get(item_id, 0) + 1 + + for item_id, count in item_counts.items(): + if Inventory.get(item_id, 0) < count: + return {"success": False, "message": f"Not enough inventory for {item_id}"} + + for item_id, count in item_counts.items(): + Inventory[item_id] -= count + + return {"success": True} @app.before_request def sentry_event_context(): @@ -70,6 +75,19 @@ def checkout(): print "Processing order for: " + order["email"] cart = order["cart"] - process_order(cart) + result = process_order(cart) + + if not result["success"]: + return result["message"], 400 return 'Success' + +@app.route('/restock', methods=['POST']) +def restock(): + global Inventory + Inventory = { + 'wrench': 100, + 'nails': 100, + 'hammer': 100 + } + return 'Inventory restocked'