second-project-wahm3i/endpoints/create-country.post.py

27 lines
869 B
Python

from fastapi import APIRouter, HTTPException, status
from typing import Dict, Any
from pydantic import BaseModel
from database.country import create_country
from utils.formatters import format_country_response
router = APIRouter()
class CountryCreate(BaseModel):
name: str
landmass_size: float
@router.post("/create-country", status_code=status.HTTP_201_CREATED, response_model=Dict[str, Any])
async def create_new_country(country_data: CountryCreate):
"""Create a new country"""
created_country = create_country(
name=country_data.name,
landmass_size=country_data.landmass_size
)
if not created_country:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid country data or country already exists"
)
return format_country_response(created_country)