feat: Updated endpoint endpoints/names.post.py via AI

This commit is contained in:
Backend IM Bot 2025-04-12 21:42:46 +00:00
parent 4e528f862a
commit cdff0ac12d
2 changed files with 26 additions and 18 deletions

View File

@ -1,12 +1,14 @@
from fastapi import APIRouter
from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from core.database import get_db
from schemas.fruit import FruitNamesResponse
from helpers.fruit_helpers import get_all_fruit_names
router = APIRouter()
@router.post("/names", status_code=200, response_model=List[FruitNamesResponse])
async def get_fruit_names():
"""Get all fruit names"""
fruit_names = get_all_fruit_names()
return fruit_names
@router.post("/names", response_model=FruitNamesResponse, status_code=status.HTTP_200_OK)
async def get_names(db: Session = Depends(get_db)):
names = get_all_fruit_names(db)
if not names:
raise HTTPException(status_code=404, detail="No fruit names found")
return {"names": names}

View File

@ -3,17 +3,6 @@ from typing import List, Optional
from uuid import UUID
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):
name: str = Field(..., min_length=1, description="Fruit name")
@ -37,4 +26,21 @@ class FruitSchema(FruitBase):
"created_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"
}
]
}
}