Skip to content
Permalink
c5ecbb80ca
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
35 lines (31 sloc) 1.11 KB
import fastapi
app = fastapi.FastAPI()
inventory = {}
@app.get("/store")
async def index(itemName: str | None = None, quantity: int | None = None):
if ((quantity == None) and (itemName != None) and (itemName in inventory)):
return inventory[itemName]
elif ((quantity != None) and (itemName not in inventory)):
inventory.update({itemName: quantity})
return inventory
@app.get("/update")
async def modify(itemName: str, action: str):
match action:
case "inc":
inventory.update({itemName: inventory[itemName]+1})
case "dec":
inventory.update({itemName: inventory[itemName]-1})
case _:
return {"Error": "Invalid action"}
return inventory
@app.get("/rename")
async def rename(oldName: str, newName: str):
if ((oldName in inventory) and (newName not in inventory)):
quantity = inventory[oldName]
inventory.pop(oldName)
inventory.update({newName: quantity})
elif (oldName not in inventory):
return {"Error": "Invalid item name"}
else:
return {"Error": "Name already in use"}
return inventory