diff --git a/alembic/versions/20250414_020439_209835f9_update_grocery.py b/alembic/versions/20250414_020439_209835f9_update_grocery.py new file mode 100644 index 0000000..168d8a6 --- /dev/null +++ b/alembic/versions/20250414_020439_209835f9_update_grocery.py @@ -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') \ No newline at end of file diff --git a/endpoints/get-grocery2.get.py b/endpoints/get-grocery2.get.py index a3a23d9..5ff30cf 100644 --- a/endpoints/get-grocery2.get.py +++ b/endpoints/get-grocery2.get.py @@ -1,15 +1,19 @@ -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from core.database import get_db 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.get("/get-grocery2", response_model=GrocerySchema) -async def get_grocery2(name: str, db: Session = Depends(get_db)): - """Get grocery item with color information by name""" +@router.get("/get-grocery2", response_model=Optional[GrocerySchema]) +async def get_grocery_shape(name: str, db: Session = Depends(get_db)): + """Get grocery item with shape property""" grocery = get_grocery_by_name(db=db, name=name) 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 \ No newline at end of file diff --git a/helpers/grocery_helpers.py b/helpers/grocery_helpers.py index d061945..022d6b8 100644 --- a/helpers/grocery_helpers.py +++ b/helpers/grocery_helpers.py @@ -20,7 +20,8 @@ def create_grocery(db: Session, grocery_data: GroceryCreate) -> Optional[Grocery normalized_name = normalize_grocery_name(grocery_data.name) db_grocery = Grocery( name=normalized_name, - color=grocery_data.color + color=grocery_data.color, + shape=grocery_data.shape ) db.add(db_grocery) 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]: """ 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: db (Session): The database session diff --git a/models/grocery.py b/models/grocery.py index 08c6bcd..d44f838 100644 --- a/models/grocery.py +++ b/models/grocery.py @@ -10,6 +10,7 @@ class Grocery(Base): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = Column(String, nullable=False) color = Column(String, nullable=True) + shape = Column(String, nullable=True) created_at = Column(DateTime, default=func.now()) updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) diff --git a/schemas/grocery.py b/schemas/grocery.py index fec5b64..72eeee7 100644 --- a/schemas/grocery.py +++ b/schemas/grocery.py @@ -6,6 +6,7 @@ from uuid import UUID class GroceryBase(BaseModel): name: str = Field(..., min_length=1, description="Name 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): pass @@ -13,6 +14,7 @@ class GroceryCreate(GroceryBase): class GroceryUpdate(BaseModel): 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") + shape: Optional[str] = Field(None, description="Shape of the grocery item") class GrocerySchema(GroceryBase): id: UUID @@ -26,6 +28,7 @@ class GrocerySchema(GroceryBase): "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "name": "Milk", "color": "White", + "shape": "Rectangular", "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00" }