Skip to content
Open
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
92 changes: 84 additions & 8 deletions backend/routers/v1/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

All routes are prefixed with /api/v1/inventory
"""
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from models.snack import (
Snack,
SnackCreateSchema,
Expand All @@ -24,26 +24,102 @@
update_snack
)

def raise_internal_error():
raise HTTPException(status_code=500, detail=
{
"error": {
"code": "INTERNAL_SERVER_ERROR",
"message": "An internal error occured",
}
})
def raise_not_found_error():
raise HTTPException(status_code=404, detail=
{
"error": {
"code": "NOT_FOUND",
"message": "Resource not found",
}
})
def raise_bad_request_error():
raise HTTPException(status_code=400, detail=
{
"error": {
"code": "INVALID INPUT",
"message": "The Response Model or SKU is invalid",
}
})
router = APIRouter()

@router.get("/", response_model=InventoryResponse)
async def get_inventory_route():
snacks = get_inventory()
return { "snacks": snacks }
try:
snacks = get_inventory()
if not snacks:
raise_not_found_error()
return { "snacks": snacks }
except Exception as e:
Comment thread
MatthewTran22 marked this conversation as resolved.
raise_internal_error()


@router.get("/snacks/{sku}", response_model=Snack)
async def get_snack_route(sku: str):
snack = get_snack(sku)
return snack
#check duplicate entry (i dont think this even needs to be here LOL)
try:
#check input validation (UPDATE THIS WHEN WE HAVE VALIDATION)
if not sku:
raise_bad_request_error()
snack = get_snack(sku)
#check if snack exists
if not snack:
raise_not_found_error()
return snack
#check for internal server error
except Exception as e:
raise_internal_error()
Comment on lines +77 to +78
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?)


@router.post("/snacks", response_model=Snack)
async def create_snack_route(snack: SnackCreateSchema):
return create_snack(snack)
#check input validation (UPDATE THIS WHEN WE HAVE VALIDATION)
#check duplicate entry (no method yet)
try:
snack = create_snack(snack)
#check if snack exists
if not snack:
raise_bad_request_error()
return snack
#check for internal server error
except Exception as e:
raise_internal_error()
Comment on lines +91 to +92
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?)


@router.put("/snacks/{sku}", response_model=Snack)
async def update_snack_route(sku: str, updates: SnackUpdateSchema):
return update_snack(sku, updates)
#check duplicate entry (no method yet)
try:
#check input validation (UPDATE THIS WHEN WE HAVE VALIDATION)
if not sku:
raise_bad_request_error()

updated = update_snack(sku, updates)
#check if snack exists
if not updated:
raise_not_found_error()
return updated
#check for internal server error
except Exception as e:
raise_internal_error()
Comment on lines +108 to +109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?)


@router.delete("/snacks/{sku}", response_model=Snack)
async def delete_snack_route(sku: str):
return delete_snack(sku)
#check duplicate entry (no method yet)
try:
#check input validation (UPDATE THIS WHEN WE HAVE VALIDATION)
if not sku:
raise_bad_request_error()
snack = delete_snack(sku)
#check if snack exists
if not snack:
raise_not_found_error()
return snack
#check for internal server error
except Exception as e:
raise_internal_error()
Comment on lines +124 to +125
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?)