feat: Updated endpoint endpoints/fruits.get.py via AI
This commit is contained in:
parent
4084efc386
commit
7a3bb1c17e
19
alembic/versions/20250412_024841_9b20efd2_update_fruit.py
Normal file
19
alembic/versions/20250412_024841_9b20efd2_update_fruit.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
"""Remove is_ripe field from Fruit model
|
||||||
|
Revision ID: b8d5e2c6a4f9
|
||||||
|
Revises: a7c4e9f1d3b5
|
||||||
|
Create Date: 2024-01-19 10:23:45.123456
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'b8d5e2c6a4f9'
|
||||||
|
down_revision = 'a7c4e9f1d3b5'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.drop_column('fruits', 'is_ripe')
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.add_column('fruits', sa.Column('is_ripe', sa.Boolean(), server_default='false', nullable=False))
|
@ -1,4 +1,4 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, status
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from typing import List
|
from typing import List
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
@ -7,10 +7,10 @@ from helpers.fruit_helpers import get_all_fruits
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/fruits", response_model=List[FruitSchema], status_code=200)
|
@router.get("/fruits", response_model=List[FruitSchema], status_code=status.HTTP_200_OK)
|
||||||
async def get_fruits(db: Session = Depends(get_db)):
|
async def get_fruits(db: Session = Depends(get_db)):
|
||||||
"""Get all fruits"""
|
|
||||||
fruits = get_all_fruits(db)
|
fruits = get_all_fruits(db)
|
||||||
if not fruits:
|
for fruit in fruits:
|
||||||
raise HTTPException(status_code=404, detail="No fruits found")
|
if hasattr(fruit, 'is_ripe'):
|
||||||
|
delattr(fruit, 'is_ripe')
|
||||||
return fruits
|
return fruits
|
@ -8,7 +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 full fruit objects including the is_ripe status.
|
Returns full fruit objects.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
@ -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 and is_ripe status
|
fruit_data (FruitCreate): The data for the fruit to create, including optional color
|
||||||
|
|
||||||
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 and is_ripe status
|
fruit_data (FruitUpdate): The update data for the fruit, including optional color
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Optional[Fruit]: The updated fruit object if found, otherwise None
|
Optional[Fruit]: The updated fruit object if found, otherwise None
|
||||||
|
@ -12,6 +12,5 @@ class Fruit(Base):
|
|||||||
description = Column(Text, nullable=True)
|
description = Column(Text, nullable=True)
|
||||||
color = Column(String(50), nullable=True)
|
color = Column(String(50), nullable=True)
|
||||||
is_active = Column(Boolean, server_default='true', nullable=False)
|
is_active = Column(Boolean, server_default='true', nullable=False)
|
||||||
is_ripe = Column(Boolean, server_default='false', nullable=False)
|
|
||||||
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||||
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
|
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
|
@ -11,7 +11,6 @@ class FruitBase(BaseModel):
|
|||||||
description: Optional[str] = Field(None, description="Fruit description")
|
description: Optional[str] = Field(None, description="Fruit description")
|
||||||
color: Optional[str] = Field(None, max_length=50, description="Fruit color")
|
color: Optional[str] = Field(None, max_length=50, 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
|
||||||
@ -21,7 +20,6 @@ class FruitUpdate(BaseModel):
|
|||||||
description: Optional[str] = Field(None, description="Fruit description")
|
description: Optional[str] = Field(None, description="Fruit description")
|
||||||
color: Optional[str] = Field(None, max_length=50, description="Fruit color")
|
color: Optional[str] = Field(None, max_length=50, 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
|
||||||
@ -37,7 +35,6 @@ 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