diff --git a/endpoints/fruits.get.py b/endpoints/fruits.get.py index 595b118..098bda1 100644 --- a/endpoints/fruits.get.py +++ b/endpoints/fruits.get.py @@ -1,14 +1,12 @@ -from fastapi import APIRouter, Depends -from sqlalchemy.orm import Session +from fastapi import APIRouter from typing import List -from core.database import get_db from schemas.fruit import FruitSchema from helpers.fruit_helpers import get_all_fruits router = APIRouter() -@router.get("/fruits", response_model=List[FruitSchema]) -async def get_fruits(db: Session = Depends(get_db)): - """Get all fruits""" - fruits = get_all_fruits(db) +@router.get("/fruits", status_code=200, response_model=List[FruitSchema]) +async def get_fruits(): + """Get all fruit names""" + fruits = get_all_fruits() return fruits \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py index a8483a3..aff6355 100644 --- a/helpers/fruit_helpers.py +++ b/helpers/fruit_helpers.py @@ -5,10 +5,10 @@ from sqlalchemy import and_ from models.fruit import Fruit from schemas.fruit import FruitCreate, FruitUpdate -def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]: +def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[str]: """ - Retrieves all fruits from the database with optional pagination and active filtering. - Returns fruits with their decorative names included. + Retrieves all fruit names from the database with optional pagination and active filtering. + Returns only the names of the fruits. Args: db (Session): The database session @@ -17,12 +17,12 @@ def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bo active_only (bool): If True, returns only active fruits Returns: - List[Fruit]: List of fruit objects with decorative names + List[str]: List of fruit names """ - query = db.query(Fruit) + query = db.query(Fruit.name) if active_only: query = query.filter(Fruit.is_active) - return query.offset(skip).limit(limit).all() + return [fruit[0] for fruit in query.offset(skip).limit(limit).all()] def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]: """ diff --git a/schemas/fruit.py b/schemas/fruit.py index 5d59be3..c5b0c95 100644 --- a/schemas/fruit.py +++ b/schemas/fruit.py @@ -1,8 +1,11 @@ from pydantic import BaseModel, Field -from typing import Optional +from typing import Optional, List from datetime import datetime from uuid import UUID +class FruitName(BaseModel): + name: str = Field(..., min_length=1, description="Fruit name") + class FruitBase(BaseModel): name: str = Field(..., min_length=1, description="Fruit name") decorative_name: str = Field(..., description="Decorative prefix for the fruit name") @@ -35,4 +38,14 @@ class FruitSchema(FruitBase): "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00" } + } + +class FruitNameList(BaseModel): + fruits: List[str] + + class Config: + schema_extra = { + "example": { + "fruits": ["Apple", "Banana", "Orange"] + } } \ No newline at end of file