feat: Updated endpoint endpoints/fruits.get.py via AI
This commit is contained in:
parent
d831b0922f
commit
c8f49faef2
@ -8,11 +8,15 @@ from helpers.fruit_helpers import get_all_fruits
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/fruits", response_model=List[FruitSchema])
|
||||
async def get_fruits(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
fruits = get_all_fruits(db, skip=skip, limit=limit, active_only=active_only)
|
||||
return fruits
|
||||
async def get_fruits(db: Session = Depends(get_db)):
|
||||
fruits = get_all_fruits(db)
|
||||
enhanced_fruits = []
|
||||
fancy_prefixes = ["Magnificent", "Splendid", "Extraordinary", "Marvelous", "Spectacular"]
|
||||
from random import choice
|
||||
|
||||
for fruit in fruits:
|
||||
fruit_dict = fruit.__dict__
|
||||
fruit_dict["name"] = f"{choice(fancy_prefixes)} {fruit_dict['name']}"
|
||||
enhanced_fruits.append(fruit_dict)
|
||||
|
||||
return enhanced_fruits
|
@ -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]:
|
||||
"""
|
||||
Retrieves all fruits from the database with optional pagination and active filtering.
|
||||
Returns fruits with their decorative names included.
|
||||
|
||||
Args:
|
||||
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
|
||||
|
||||
Returns:
|
||||
List[Fruit]: List of fruit objects
|
||||
List[Fruit]: List of fruit objects with decorative names
|
||||
"""
|
||||
query = db.query(Fruit)
|
||||
if active_only:
|
||||
|
@ -5,6 +5,7 @@ from uuid import UUID
|
||||
|
||||
class FruitBase(BaseModel):
|
||||
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")
|
||||
is_active: bool = Field(True, description="Whether the fruit is active")
|
||||
|
||||
@ -13,6 +14,7 @@ class FruitCreate(FruitBase):
|
||||
|
||||
class FruitUpdate(BaseModel):
|
||||
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")
|
||||
is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
|
||||
|
||||
@ -27,6 +29,7 @@ class FruitSchema(FruitBase):
|
||||
"example": {
|
||||
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||
"name": "Apple",
|
||||
"decorative_name": "Sweet Apple",
|
||||
"description": "A sweet, edible fruit produced by an apple tree",
|
||||
"is_active": True,
|
||||
"created_at": "2023-01-01T12:00:00",
|
||||
|
Loading…
x
Reference in New Issue
Block a user