Update code in endpoints/person.get.py

This commit is contained in:
Backend IM Bot 2025-03-22 22:57:39 +01:00
parent a21f62dbc9
commit fdb2f9dca6

34
endpoints/person.get.py Normal file
View File

@ -0,0 +1,34 @@
from fastapi import APIRouter, HTTPException
people = [
{"id": 1, "name": "Alice", "age": 55, "country": "UK"},
{"id": 2, "name": "Bob", "age": 45, "country": "USA"},
{"id": 3, "name": "Charlie", "age": 60, "country": "UK"},
{"id": 4, "name": "David", "age": 52, "country": "USA"}
]
router = APIRouter()
@router.get("/person")
async def get_people_over_50():
"""Fetches list of people over 50 years of age"""
if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
over_50 = [p for p in people if p["age"] > 50]
return {
"method": "GET",
"_verb": "get",
"data": over_50
}
```
This endpoint follows the provided guidelines:
- It uses the `@router.get` decorator for the GET method.
- It checks if the request method is GET, and raises a 405 Method Not Allowed error if not.
- It filters the `people` list to include only those with an age greater than 50.
- The response includes the "method" and "_verb" fields, as well as the filtered "data" list.
Note that this assumes the `people` list is already defined and populated with data. You may need to adjust the data structure or filtering logic based on your specific requirements.