Update code in endpoints/people.get.py
This commit is contained in:
parent
92568b9360
commit
4f64878316
@ -1,22 +1,22 @@
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
people = [
|
people = [
|
||||||
{"name": "Alice", "age": 55},
|
{"name": "John Doe", "age": 55, "country": "UK"},
|
||||||
{"name": "Bob", "age": 45},
|
{"name": "Jane Smith", "age": 45, "country": "USA"},
|
||||||
{"name": "Charlie", "age": 60},
|
{"name": "Bob Johnson", "age": 62, "country": "UK"},
|
||||||
{"name": "David", "age": 35},
|
{"name": "Alice Williams", "age": 38, "country": "USA"},
|
||||||
{"name": "Eve", "age": 52}
|
{"name": "Charlie Brown", "age": 51, "country": "UK"}
|
||||||
]
|
]
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/people")
|
@router.get("/people")
|
||||||
async def get_people_over_50():
|
async def get_people_over_50():
|
||||||
"""Fetch list of people over 50 years of age"""
|
"""Fetches list of people over 50 years of age"""
|
||||||
if request.method != "GET":
|
if request.method != "GET":
|
||||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
over_50 = [p for p in people if p["age"] > 50]
|
over_50 = [person for person in people if person["age"] > 50]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
@ -25,4 +25,11 @@ async def get_people_over_50():
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This endpoint filters the `people` list to only include those whose age is over 50, and returns that filtered list in the response data. It also includes the required method metadata fields.
|
This endpoint follows the provided rules and examples:
|
||||||
|
|
||||||
|
- It uses the `@router.get` decorator for the GET method.
|
||||||
|
- It checks if the request method is GET, and raises a 405 error if not.
|
||||||
|
- It filters the `people` list to include only those over 50 years of age.
|
||||||
|
- The response includes the "method", "_verb", and "data" (filtered list) fields.
|
||||||
|
|
||||||
|
Note: The `people` list is pre-populated with sample data for demonstration purposes. In a real application, this data would typically come from a database or other data source.
|
Loading…
x
Reference in New Issue
Block a user