Update code in endpoints/human.get.py
This commit is contained in:
parent
edf9df9799
commit
16f7e5ab3b
@ -1,39 +1,25 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from typing import List, Dict
|
||||
|
||||
people = [
|
||||
{"name": "Alice", "age": 55, "country": "UK"},
|
||||
{"name": "Bob", "age": 45, "country": "US"},
|
||||
{"name": "Bob", "age": 45, "country": "USA"},
|
||||
{"name": "Charlie", "age": 60, "country": "ESP"},
|
||||
{"name": "David", "age": 35, "country": "UK"},
|
||||
{"name": "Eve", "age": 52, "country": "US"}
|
||||
{"name": "Eve", "age": 52, "country": "USA"}
|
||||
]
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/human", response_model=List[Dict[str, str | int]])
|
||||
def get_people_over_50():
|
||||
@router.get("/human")
|
||||
async def get_older_people():
|
||||
"""Fetch list of people over 50 years of age"""
|
||||
if request.method != "GET":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
over_50 = [person for person in people if person["age"] > 50]
|
||||
|
||||
older_people = [p for p in people if p["age"] > 50]
|
||||
|
||||
return {
|
||||
"method": "GET",
|
||||
"_verb": "get",
|
||||
"data": over_50
|
||||
"data": older_people
|
||||
}
|
||||
```
|
||||
|
||||
This endpoint follows the provided rules and examples:
|
||||
|
||||
- Uses `@router.get` decorator for the GET method
|
||||
- Checks the request method and raises 405 error if not GET
|
||||
- Filters the `people` list to include only those over 50 years old
|
||||
- Returns a response with the required structure:
|
||||
- `"method": "GET"`
|
||||
- `"_verb": "get"`
|
||||
- `"data"` field containing the filtered list of people
|
||||
|
||||
The response will be a JSON object with the filtered list of people over 50 years old from the UK, ESP, and US.
|
||||
This endpoint defines a list of people with their name, age, and country. The `get_older_people` function filters this list to only include people whose age is over 50 years old. The response includes the filtered list of older people, along with the method metadata as per the requirements.
|
Loading…
x
Reference in New Issue
Block a user