Skip to content

Commit

Permalink
added function to fetch car by id
Browse files Browse the repository at this point in the history
  • Loading branch information
IshanRattan committed Mar 6, 2024
1 parent d5b35f2 commit 7317b0f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion programming/fastapi-fundamentals/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fastapi import FastAPI, HTTPException



app = FastAPI()

db = [{'id': 1, 'size': 's', 'fuel': 'gasoline', 'doors': 4, 'transmission': 'auto'},
Expand Down Expand Up @@ -32,3 +31,11 @@ def get_cars(size: Optional[str]=None, doors: Optional[int]=None) -> List:
raise HTTPException(status_code=404, detail='No car with doors greater than 5!')

return result

@app.get('/api/cars/{id}')
def car_by_id(id: int) -> dict:
result = [car for car in db if car['id'] == id]
if result:
return result[0]
else:
raise HTTPException(status_code=404, detail=f'No car with id= {id}.')

0 comments on commit 7317b0f

Please sign in to comment.