feat: Updated endpoint endpoints/fruits.get.py via AI
This commit is contained in:
parent
ba10c64955
commit
ccd764291d
19
alembic/versions/20250412_021951_28878fb8_update_fruit.py
Normal file
19
alembic/versions/20250412_021951_28878fb8_update_fruit.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
"""add is_ripe field to fruits table
|
||||||
|
Revision ID: 9b5e3d82
|
||||||
|
Revises: 8a4f2c91
|
||||||
|
Create Date: 2024-01-22 10:23:45.123456
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '9b5e3d82'
|
||||||
|
down_revision = '8a4f2c91'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.add_column('fruits', sa.Column('is_ripe', sa.Boolean(), server_default='false', nullable=False))
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_column('fruits', 'is_ripe')
|
@ -8,11 +8,8 @@ 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_with_ripeness(db: Session = Depends(get_db)):
|
||||||
color: str | None = None,
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
|
||||||
fruits = get_all_fruits(db)
|
fruits = get_all_fruits(db)
|
||||||
if color:
|
for fruit in fruits:
|
||||||
fruits = [fruit for fruit in fruits if fruit.color == color]
|
fruit.is_ripe = fruit.ripeness > 0.7
|
||||||
return fruits
|
return fruits
|
@ -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[str]:
|
def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]:
|
||||||
"""
|
"""
|
||||||
Retrieves all fruit names from the database with optional pagination and active filtering.
|
Retrieves all fruits from the database with optional pagination and active filtering.
|
||||||
Returns only the names of the fruits.
|
Returns full fruit objects including the is_ripe status.
|
||||||
|
|
||||||
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[str]: List of fruit names
|
List[Fruit]: List of fruit objects
|
||||||
"""
|
"""
|
||||||
query = db.query(Fruit.name)
|
query = db.query(Fruit)
|
||||||
if active_only:
|
if active_only:
|
||||||
query = query.filter(Fruit.is_active)
|
query = query.filter(Fruit.is_active)
|
||||||
return [fruit[0] for fruit in query.offset(skip).limit(limit).all()]
|
return 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]:
|
||||||
"""
|
"""
|
||||||
@ -56,7 +56,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
fruit_data (FruitCreate): The data for the fruit to create, including optional color
|
fruit_data (FruitCreate): The data for the fruit to create, including optional color and is_ripe status
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Fruit: The newly created fruit object
|
Fruit: The newly created fruit object
|
||||||
@ -74,7 +74,7 @@ def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Option
|
|||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
fruit_id (UUID): The ID of the fruit to update
|
fruit_id (UUID): The ID of the fruit to update
|
||||||
fruit_data (FruitUpdate): The update data for the fruit, including optional color
|
fruit_data (FruitUpdate): The update data for the fruit, including optional color and is_ripe status
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Optional[Fruit]: The updated fruit object if found, otherwise None
|
Optional[Fruit]: The updated fruit object if found, otherwise None
|
||||||
|
@ -12,5 +12,6 @@ class Fruit(Base):
|
|||||||
description = Column(String, nullable=True)
|
description = Column(String, nullable=True)
|
||||||
color = Column(String, nullable=True)
|
color = Column(String, nullable=True)
|
||||||
is_active = Column(Boolean, default=True)
|
is_active = Column(Boolean, default=True)
|
||||||
|
is_ripe = Column(Boolean, default=False)
|
||||||
created_at = Column(DateTime, default=func.now())
|
created_at = Column(DateTime, default=func.now())
|
||||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
@ -11,6 +11,7 @@ class FruitBase(BaseModel):
|
|||||||
description: Optional[str] = Field(None, description="Fruit description")
|
description: Optional[str] = Field(None, description="Fruit description")
|
||||||
color: Optional[str] = Field(None, description="Fruit color")
|
color: Optional[str] = Field(None, description="Fruit color")
|
||||||
is_active: bool = Field(True, description="Whether the fruit is active")
|
is_active: bool = Field(True, description="Whether the fruit is active")
|
||||||
|
is_ripe: bool = Field(False, description="Whether the fruit is ripe")
|
||||||
|
|
||||||
class FruitCreate(FruitBase):
|
class FruitCreate(FruitBase):
|
||||||
pass
|
pass
|
||||||
@ -20,6 +21,7 @@ class FruitUpdate(BaseModel):
|
|||||||
description: Optional[str] = Field(None, description="Fruit description")
|
description: Optional[str] = Field(None, description="Fruit description")
|
||||||
color: Optional[str] = Field(None, description="Fruit color")
|
color: Optional[str] = Field(None, description="Fruit color")
|
||||||
is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
|
is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
|
||||||
|
is_ripe: Optional[bool] = Field(None, description="Whether the fruit is ripe")
|
||||||
|
|
||||||
class FruitSchema(FruitBase):
|
class FruitSchema(FruitBase):
|
||||||
id: UUID
|
id: UUID
|
||||||
@ -35,6 +37,7 @@ class FruitSchema(FruitBase):
|
|||||||
"description": "A sweet, edible fruit produced by an apple tree",
|
"description": "A sweet, edible fruit produced by an apple tree",
|
||||||
"color": "Red",
|
"color": "Red",
|
||||||
"is_active": True,
|
"is_active": True,
|
||||||
|
"is_ripe": False,
|
||||||
"created_at": "2023-01-01T12:00:00",
|
"created_at": "2023-01-01T12:00:00",
|
||||||
"updated_at": "2023-01-01T12:00:00"
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user