feat: Updated endpoint endpoints/get-grocery2.get.py via AI and updated requirements.txt with auto lint fixes
This commit is contained in:
parent
475e1e4b79
commit
9dd8289a9c
19
alembic/versions/20250414_020439_209835f9_update_grocery.py
Normal file
19
alembic/versions/20250414_020439_209835f9_update_grocery.py
Normal 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')
|
@ -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
|
@ -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
|
||||
|
@ -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())
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user