Update code in endpoints/tenants.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 14:01:13 +01:00
parent 14c1dbd462
commit 924b3a75a2

View File

@ -2,9 +2,43 @@ from fastapi import APIRouter, HTTPException
router = APIRouter() router = APIRouter()
towns = [ towns_in_tennessee = [
"Memphis", "Nashville", "Memphis", "Knoxville", "Chattanooga", "Clarksville",
"Murfreesboro", "Franklin", "Jackson", "Johnson City", "Bartlett"
]
@router.post("/tenants", response_model=dict)
async def get_towns_in_tennessee():
"""Returns list of towns in Tennessee"""
if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return {
"method": "POST",
"_verb": "post",
"towns": towns_in_tennessee
}
```
This endpoint adheres to the provided rules and examples:
- Uses `@router.post` decorator for the `/tenants` path
- Checks `request.method` and raises 405 error for non-POST requests
- Returns a dictionary response with required fields:
- `"method": "POST"`
- `"_verb": "post"`
- `"towns"` key containing list of town names in Tennessee
- Follows code structure from examples (imports, docstring, etc.)
The response will be:
```json
{
"method": "POST",
"_verb": "post",
"towns": [
"Nashville", "Nashville",
"Memphis",
"Knoxville", "Knoxville",
"Chattanooga", "Chattanooga",
"Clarksville", "Clarksville",
@ -14,25 +48,4 @@ towns = [
"Johnson City", "Johnson City",
"Bartlett" "Bartlett"
] ]
@router.post("/tenants", response_model=dict)
async def list_towns():
"""Returns list of towns in Tennessee"""
if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return {
"method": "POST",
"_verb": "post",
"towns": towns
} }
```
This endpoint follows the provided rules and examples:
- Uses the `@router.post` decorator for the `/tenants` path
- Validates the request method is POST, raising 405 error if not
- Returns a dictionary response containing the list of towns
- Includes the "method" and "_verb" metadata in the response
The `towns` list is pre-populated with some major cities in Tennessee for this example. In a real application, this data would likely come from a database or external API.