feat: Updated endpoint endpoints/get-grocery2.get.py via AI and updated requirements.txt with auto lint fixes

This commit is contained in:
Backend IM Bot 2025-04-14 02:04:53 +00:00
parent 475e1e4b79
commit 9dd8289a9c
5 changed files with 36 additions and 8 deletions

View File

@ -0,0 +1,19 @@
"""add shape field to groceries table
Revision ID: b8f5d3a9
Revises: 9a4c2e7d
Create Date: 2024-01-30 10:45:23.123456
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'b8f5d3a9'
down_revision = '9a4c2e7d'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('groceries', sa.Column('shape', sa.String(), nullable=True))
def downgrade():
op.drop_column('groceries', 'shape')

View File

@ -1,15 +1,19 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from core.database import get_db from core.database import get_db
from schemas.grocery import GrocerySchema from schemas.grocery import GrocerySchema
from helpers.grocery_helpers import get_grocery_by_name from crud.grocery import get_grocery_by_name
from typing import Optional
router = APIRouter() router = APIRouter()
@router.get("/get-grocery2", response_model=GrocerySchema) @router.get("/get-grocery2", response_model=Optional[GrocerySchema])
async def get_grocery2(name: str, db: Session = Depends(get_db)): async def get_grocery_shape(name: str, db: Session = Depends(get_db)):
"""Get grocery item with color information by name""" """Get grocery item with shape property"""
grocery = get_grocery_by_name(db=db, name=name) grocery = get_grocery_by_name(db=db, name=name)
if not grocery: if not grocery:
raise HTTPException(status_code=404, detail="Grocery not found") raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Grocery not found"
)
return grocery return grocery

View File

@ -20,7 +20,8 @@ def create_grocery(db: Session, grocery_data: GroceryCreate) -> Optional[Grocery
normalized_name = normalize_grocery_name(grocery_data.name) normalized_name = normalize_grocery_name(grocery_data.name)
db_grocery = Grocery( db_grocery = Grocery(
name=normalized_name, name=normalized_name,
color=grocery_data.color color=grocery_data.color,
shape=grocery_data.shape
) )
db.add(db_grocery) db.add(db_grocery)
db.commit() db.commit()
@ -33,7 +34,7 @@ def create_grocery(db: Session, grocery_data: GroceryCreate) -> Optional[Grocery
def get_grocery_by_name(db: Session, name: str) -> Optional[Grocery]: def get_grocery_by_name(db: Session, name: str) -> Optional[Grocery]:
""" """
Retrieves a grocery item by its name using the indexed name column. Retrieves a grocery item by its name using the indexed name column.
Returns the complete grocery object including the color field. Returns the complete grocery object including the color and shape fields.
Args: Args:
db (Session): The database session db (Session): The database session

View File

@ -10,6 +10,7 @@ class Grocery(Base):
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, nullable=False) name = Column(String, nullable=False)
color = Column(String, nullable=True) color = Column(String, nullable=True)
shape = Column(String, nullable=True)
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())

View File

@ -6,6 +6,7 @@ from uuid import UUID
class GroceryBase(BaseModel): class GroceryBase(BaseModel):
name: str = Field(..., min_length=1, description="Name of the grocery item") name: str = Field(..., min_length=1, description="Name of the grocery item")
color: Optional[str] = Field(None, description="Color of the grocery item") color: Optional[str] = Field(None, description="Color of the grocery item")
shape: Optional[str] = Field(None, description="Shape of the grocery item")
class GroceryCreate(GroceryBase): class GroceryCreate(GroceryBase):
pass pass
@ -13,6 +14,7 @@ class GroceryCreate(GroceryBase):
class GroceryUpdate(BaseModel): class GroceryUpdate(BaseModel):
name: Optional[str] = Field(None, min_length=1, description="Name of the grocery item") name: Optional[str] = Field(None, min_length=1, description="Name of the grocery item")
color: Optional[str] = Field(None, description="Color of the grocery item") color: Optional[str] = Field(None, description="Color of the grocery item")
shape: Optional[str] = Field(None, description="Shape of the grocery item")
class GrocerySchema(GroceryBase): class GrocerySchema(GroceryBase):
id: UUID id: UUID
@ -26,6 +28,7 @@ class GrocerySchema(GroceryBase):
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"name": "Milk", "name": "Milk",
"color": "White", "color": "White",
"shape": "Rectangular",
"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"
} }