daimaou-project-5zk58s/endpoints/countries.post.py

19 lines
663 B
Python

from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session
from core.database import get_db
from schemas.country import CountryCreate, CountrySchema
from helpers.country_helpers import get_country_by_name, create_country
router = APIRouter()
@router.post("/countries", status_code=status.HTTP_201_CREATED, response_model=CountrySchema)
async def create_new_country(
country_data: CountryCreate,
db: Session = Depends(get_db)
):
existing_country = get_country_by_name(db, name=country_data.name)
if existing_country:
return existing_country
new_country = create_country(db, country_data)
return new_country