Update code in endpoints/man.get.py

This commit is contained in:
Backend IM Bot 2025-03-23 00:21:56 +01:00
parent daa708c269
commit b415d9990d

View File

@ -3,23 +3,33 @@ from fastapi import APIRouter, HTTPException
people = [ people = [
{"name": "Alice", "age": 55, "country": "UK"}, {"name": "Alice", "age": 55, "country": "UK"},
{"name": "Bob", "age": 45, "country": "ESP"}, {"name": "Bob", "age": 45, "country": "ESP"},
{"name": "Charlie", "age": 60, "country": "Nigeria"}, {"name": "Charlie", "age": 60, "country": "Togo"},
{"name": "David", "age": 52, "country": "UK"}, {"name": "David", "age": 35, "country": "UK"},
{"name": "Eve", "age": 48, "country": "ESP"} {"name": "Eve", "age": 52, "country": "ESP"}
] ]
router = APIRouter() router = APIRouter()
@router.get("/man") @router.get("/man")
async def get_people_over_50(): async def get_people_over_50():
"""Fetches list of people over 50 years of age""" """Endpoint to fetch list of people over 50 years of age"""
over_50 = [p for p in people if p["age"] > 50] if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
over_50 = [person for person in people if person["age"] > 50]
return { return {
"method": "GET", "method": "GET",
"_verb": "get", "_verb": "get",
"data": over_50 "data": over_50
} }
``` ```
This endpoint defines a list of people with their name, age, and country. The `get_people_over_50` function filters the list to only include people whose age is greater than 50, and returns a dictionary with the method metadata and the filtered list of people. This endpoint follows the provided rules and examples:
1. It uses the `@router.get` decorator for the GET method.
2. It validates the request method and raises a 405 error if not GET.
3. It filters the `people` list to include only those over 50 years old.
4. The response includes the required `"method"` and `"_verb"` fields, along with the filtered list of people in the `"data"` field.
Note that the `people` list is initialized with some sample data for demonstration purposes. In a real application, this data would likely come from a database or other external source.