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

This commit is contained in:
Backend IM Bot 2025-04-12 02:16:15 +00:00
parent af1c3bb37a
commit ba10c64955
5 changed files with 32 additions and 4 deletions

View File

@ -0,0 +1,19 @@
"""add color field to fruits table
Revision ID: 8a4f2c91
Revises: 237fe07a
Create Date: 2024-01-30 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '8a4f2c91'
down_revision = '237fe07a'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('fruits', sa.Column('color', sa.String(), nullable=True))
def downgrade():
op.drop_column('fruits', 'color')

View File

@ -7,7 +7,12 @@ from helpers.fruit_helpers import get_all_fruits
router = APIRouter()
@router.get("/fruits", response_model=List[FruitSchema], status_code=200)
async def get_fruits(db: Session = Depends(get_db)):
@router.get("/fruits", response_model=List[FruitSchema])
async def get_fruits(
color: str | None = None,
db: Session = Depends(get_db)
):
fruits = get_all_fruits(db)
if color:
fruits = [fruit for fruit in fruits if fruit.color == color]
return fruits

View File

@ -56,7 +56,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit:
Args:
db (Session): The database session
fruit_data (FruitCreate): The data for the fruit to create
fruit_data (FruitCreate): The data for the fruit to create, including optional color
Returns:
Fruit: The newly created fruit object
@ -74,7 +74,7 @@ def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Option
Args:
db (Session): The database session
fruit_id (UUID): The ID of the fruit to update
fruit_data (FruitUpdate): The update data for the fruit
fruit_data (FruitUpdate): The update data for the fruit, including optional color
Returns:
Optional[Fruit]: The updated fruit object if found, otherwise None

View File

@ -10,6 +10,7 @@ class Fruit(Base):
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, unique=True, nullable=False, index=True)
description = Column(String, nullable=True)
color = Column(String, nullable=True)
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

View File

@ -9,6 +9,7 @@ class FruitName(BaseModel):
class FruitBase(BaseModel):
name: str = Field(..., min_length=1, description="Fruit name")
description: Optional[str] = Field(None, description="Fruit description")
color: Optional[str] = Field(None, description="Fruit color")
is_active: bool = Field(True, description="Whether the fruit is active")
class FruitCreate(FruitBase):
@ -17,6 +18,7 @@ class FruitCreate(FruitBase):
class FruitUpdate(BaseModel):
name: Optional[str] = Field(None, min_length=1, description="Fruit name")
description: Optional[str] = Field(None, description="Fruit description")
color: Optional[str] = Field(None, description="Fruit color")
is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
class FruitSchema(FruitBase):
@ -31,6 +33,7 @@ class FruitSchema(FruitBase):
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"name": "Apple",
"description": "A sweet, edible fruit produced by an apple tree",
"color": "Red",
"is_active": True,
"created_at": "2023-01-01T12:00:00",
"updated_at": "2023-01-01T12:00:00"