Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Refactor inventory processing and add restock endpoint #26

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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'
Loading