Skip to content

Commit

Permalink
added demo.py
Browse files Browse the repository at this point in the history
  • Loading branch information
IshanRattan committed Mar 4, 2024
1 parent dd269d0 commit d5b35f2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions programming/fastapi-fundamentals/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

from typing import Optional, List
from datetime import datetime
from fastapi import FastAPI, HTTPException



app = FastAPI()

db = [{'id': 1, 'size': 's', 'fuel': 'gasoline', 'doors': 4, 'transmission': 'auto'},
{'id': 2, 'size': 's', 'fuel': 'gasoline', 'doors': 3, 'transmission': 'auto'},
{'id': 3, 'size': 's', 'fuel': 'gasoline', 'doors': 2, 'transmission': 'auto'}]


@app.get('/')
def welcome():
"""Return a welcome message."""
return {'message': 'Welcome to the car sharing service!'}

@app.get('/date')
def date():
return {'date': datetime.now()}

@app.get('/api/cars')
def get_cars(size: Optional[str]=None, doors: Optional[int]=None) -> List:
result = db
if size:
result = [car for car in result if car['size'] == size]
if doors and doors <=5:
result = [car for car in result if car['doors'] >= doors]
else:
raise HTTPException(status_code=404, detail='No car with doors greater than 5!')

return result

0 comments on commit d5b35f2

Please sign in to comment.