diff --git a/endpoints/names.post.py b/endpoints/names.post.py index b475e1c..f540876 100644 --- a/endpoints/names.post.py +++ b/endpoints/names.post.py @@ -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 \ No newline at end of file +@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} \ No newline at end of file diff --git a/schemas/fruit.py b/schemas/fruit.py index d6520e9..755b637 100644 --- a/schemas/fruit.py +++ b/schemas/fruit.py @@ -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" + } + ] + } } \ No newline at end of file