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

This commit is contained in:
Backend IM Bot 2025-04-12 01:26:38 +00:00
parent d831b0922f
commit c8f49faef2
3 changed files with 17 additions and 9 deletions

View File

@ -8,11 +8,15 @@ from helpers.fruit_helpers import get_all_fruits
router = APIRouter() router = APIRouter()
@router.get("/fruits", response_model=List[FruitSchema]) @router.get("/fruits", response_model=List[FruitSchema])
async def get_fruits( async def get_fruits(db: Session = Depends(get_db)):
skip: int = 0, fruits = get_all_fruits(db)
limit: int = 100, enhanced_fruits = []
active_only: bool = True, fancy_prefixes = ["Magnificent", "Splendid", "Extraordinary", "Marvelous", "Spectacular"]
db: Session = Depends(get_db) from random import choice
):
fruits = get_all_fruits(db, skip=skip, limit=limit, active_only=active_only) for fruit in fruits:
return fruits fruit_dict = fruit.__dict__
fruit_dict["name"] = f"{choice(fancy_prefixes)} {fruit_dict['name']}"
enhanced_fruits.append(fruit_dict)
return enhanced_fruits

View File

@ -8,6 +8,7 @@ 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[Fruit]:
""" """
Retrieves all fruits from the database with optional pagination and active filtering. Retrieves all fruits from the database with optional pagination and active filtering.
Returns fruits with their decorative names included.
Args: Args:
db (Session): The database session db (Session): The database session
@ -16,7 +17,7 @@ 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 List[Fruit]: List of fruit objects with decorative names
""" """
query = db.query(Fruit) query = db.query(Fruit)
if active_only: if active_only:

View File

@ -5,6 +5,7 @@ from uuid import UUID
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")
description: Optional[str] = Field(None, description="Fruit description") description: Optional[str] = Field(None, description="Fruit description")
is_active: bool = Field(True, description="Whether the fruit is active") is_active: bool = Field(True, description="Whether the fruit is active")
@ -13,6 +14,7 @@ class FruitCreate(FruitBase):
class FruitUpdate(BaseModel): class FruitUpdate(BaseModel):
name: Optional[str] = Field(None, min_length=1, description="Fruit name") name: Optional[str] = Field(None, min_length=1, description="Fruit name")
decorative_name: Optional[str] = Field(None, description="Decorative prefix for the fruit name")
description: Optional[str] = Field(None, description="Fruit description") description: Optional[str] = Field(None, description="Fruit description")
is_active: Optional[bool] = Field(None, description="Whether the fruit is active") is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
@ -27,6 +29,7 @@ class FruitSchema(FruitBase):
"example": { "example": {
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"name": "Apple", "name": "Apple",
"decorative_name": "Sweet Apple",
"description": "A sweet, edible fruit produced by an apple tree", "description": "A sweet, edible fruit produced by an apple tree",
"is_active": True, "is_active": True,
"created_at": "2023-01-01T12:00:00", "created_at": "2023-01-01T12:00:00",