feat: Updated endpoint endpoints/countries.post.py via AI
This commit is contained in:
parent
c9af878945
commit
6917e2a56e
@ -1,15 +1,21 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.country import CountryCreate
|
||||
from helpers.country_helpers import create_country
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from typing import List
|
||||
|
||||
from schemas.country import CountryCreate, CountrySchema
|
||||
from helpers.country_helpers import get_all_countries, create_country
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/countries", status_code=201)
|
||||
async def create_new_country(
|
||||
country_data: CountryCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
new_country = create_country(db, country_data)
|
||||
return new_country
|
||||
@router.post("/countries", status_code=status.HTTP_201_CREATED, response_model=CountrySchema)
|
||||
async def create_new_country(country: CountryCreate = None):
|
||||
"""Create a new country"""
|
||||
new_country = create_country(country)
|
||||
if not new_country:
|
||||
raise HTTPException(status_code=400, detail="Country could not be created")
|
||||
return new_country
|
||||
|
||||
@router.get("/countries", status_code=200, response_model=List[CountrySchema])
|
||||
async def get_countries():
|
||||
"""Get all countries"""
|
||||
countries = get_all_countries()
|
||||
return countries
|
@ -1,8 +1,8 @@
|
||||
import enum
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
import enum
|
||||
|
||||
class Continent(enum.Enum):
|
||||
AFRICA = "Africa"
|
||||
@ -17,7 +17,7 @@ class CountryBase(BaseModel):
|
||||
name: str = Field(..., description="Country name")
|
||||
latitude: float = Field(..., description="Country latitude")
|
||||
longitude: float = Field(..., description="Country longitude")
|
||||
continent: Continent = Field(..., description="Country continent")
|
||||
continent: Optional[Continent] = Field(None, description="Country continent")
|
||||
|
||||
class CountryCreate(CountryBase):
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user