feat: Updated endpoint endpoints/fruits.get.py via AI

This commit is contained in:
Backend IM Bot 2025-04-12 01:55:03 +00:00
parent 0cefccc0c9
commit 5f501c080a
3 changed files with 25 additions and 14 deletions

View File

@ -1,14 +1,12 @@
from fastapi import APIRouter, Depends from fastapi import APIRouter
from sqlalchemy.orm import Session
from typing import List from typing import List
from core.database import get_db
from schemas.fruit import FruitSchema from schemas.fruit import FruitSchema
from helpers.fruit_helpers import get_all_fruits from helpers.fruit_helpers import get_all_fruits
router = APIRouter() router = APIRouter()
@router.get("/fruits", response_model=List[FruitSchema]) @router.get("/fruits", status_code=200, response_model=List[FruitSchema])
async def get_fruits(db: Session = Depends(get_db)): async def get_fruits():
"""Get all fruits""" """Get all fruit names"""
fruits = get_all_fruits(db) fruits = get_all_fruits()
return fruits return fruits

View File

@ -5,10 +5,10 @@ from sqlalchemy import and_
from models.fruit import Fruit from models.fruit import Fruit
from schemas.fruit import FruitCreate, FruitUpdate 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. Retrieves all fruit names from the database with optional pagination and active filtering.
Returns fruits with their decorative names included. Returns only the names of the fruits.
Args: Args:
db (Session): The database session 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 active_only (bool): If True, returns only active fruits
Returns: 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: if active_only:
query = query.filter(Fruit.is_active) 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]: def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
""" """

View File

@ -1,8 +1,11 @@
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from typing import Optional from typing import Optional, List
from datetime import datetime from datetime import datetime
from uuid import UUID from uuid import UUID
class FruitName(BaseModel):
name: str = Field(..., min_length=1, description="Fruit name")
class FruitBase(BaseModel): class FruitBase(BaseModel):
name: str = Field(..., min_length=1, description="Fruit name") name: str = Field(..., min_length=1, description="Fruit name")
decorative_name: str = Field(..., description="Decorative prefix for the fruit name") decorative_name: str = Field(..., description="Decorative prefix for the fruit name")
@ -36,3 +39,13 @@ class FruitSchema(FruitBase):
"updated_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"]
}
}