feat: Updated endpoint endpoints/names.post.py via AI
This commit is contained in:
parent
4e528f862a
commit
cdff0ac12d
@ -1,12 +1,14 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from typing import List
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
from schemas.fruit import FruitNamesResponse
|
from schemas.fruit import FruitNamesResponse
|
||||||
from helpers.fruit_helpers import get_all_fruit_names
|
from helpers.fruit_helpers import get_all_fruit_names
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/names", status_code=200, response_model=List[FruitNamesResponse])
|
@router.post("/names", response_model=FruitNamesResponse, status_code=status.HTTP_200_OK)
|
||||||
async def get_fruit_names():
|
async def get_names(db: Session = Depends(get_db)):
|
||||||
"""Get all fruit names"""
|
names = get_all_fruit_names(db)
|
||||||
fruit_names = get_all_fruit_names()
|
if not names:
|
||||||
return fruit_names
|
raise HTTPException(status_code=404, detail="No fruit names found")
|
||||||
|
return {"names": names}
|
@ -3,17 +3,6 @@ from typing import List, Optional
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
class FruitNamesResponse(BaseModel):
|
|
||||||
names: List[str] = Field(..., description="List of fruit names")
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
schema_extra = {
|
|
||||||
"example": {
|
|
||||||
"names": ["Apple", "Banana", "Orange"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Note: Keeping the original schemas in case they're still needed elsewhere in the application
|
|
||||||
class FruitBase(BaseModel):
|
class FruitBase(BaseModel):
|
||||||
name: str = Field(..., min_length=1, description="Fruit name")
|
name: str = Field(..., min_length=1, description="Fruit name")
|
||||||
|
|
||||||
@ -37,4 +26,21 @@ class FruitSchema(FruitBase):
|
|||||||
"created_at": "2023-01-01T12:00:00",
|
"created_at": "2023-01-01T12:00:00",
|
||||||
"updated_at": "2023-01-01T12:00:00"
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FruitsResponse(BaseModel):
|
||||||
|
fruits: List[FruitSchema] = Field(..., description="List of fruit objects")
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
schema_extra = {
|
||||||
|
"example": {
|
||||||
|
"fruits": [
|
||||||
|
{
|
||||||
|
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||||
|
"name": "Apple",
|
||||||
|
"created_at": "2023-01-01T12:00:00",
|
||||||
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user