feat: Updated endpoint endpoints/get-grocery2.get.py via AI
This commit is contained in:
parent
4c5152bbdb
commit
fe37aefdf8
19
alembic/versions/20250414_010203_5350732a_update_grocery.py
Normal file
19
alembic/versions/20250414_010203_5350732a_update_grocery.py
Normal file
@ -0,0 +1,19 @@
|
||||
"""add color field to groceries table
|
||||
Revision ID: 9a4c2e7d
|
||||
Revises: 8f2b3d5c
|
||||
Create Date: 2024-01-30 10:00:00.000000
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '9a4c2e7d'
|
||||
down_revision = '8f2b3d5c'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.add_column('groceries', sa.Column('color', sa.String(), nullable=True))
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('groceries', 'color')
|
@ -1,28 +1,15 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from core.database import get_db
|
||||
from schemas.grocery import GrocerySchema
|
||||
from helpers.grocery_helpers import get_grocery_by_name, normalize_grocery_name
|
||||
from helpers.grocery_helpers import get_grocery_by_name
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/get-grocery2", response_model=List[GrocerySchema])
|
||||
@router.get("/get-grocery2", response_model=GrocerySchema)
|
||||
async def get_grocery2(name: str, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Get grocery items by name after fixing the faulty migration
|
||||
"""
|
||||
try:
|
||||
normalized_name = normalize_grocery_name(name)
|
||||
grocery = get_grocery_by_name(db=db, name=normalized_name)
|
||||
if not grocery:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Grocery not found"
|
||||
)
|
||||
return [grocery] # Return as list for consistency with response_model
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e)
|
||||
)
|
||||
"""Get grocery item with color information by name"""
|
||||
grocery = get_grocery_by_name(db=db, name=name)
|
||||
if not grocery:
|
||||
raise HTTPException(status_code=404, detail="Grocery not found")
|
||||
return grocery
|
@ -18,7 +18,10 @@ def create_grocery(db: Session, grocery_data: GroceryCreate) -> Optional[Grocery
|
||||
"""
|
||||
try:
|
||||
normalized_name = normalize_grocery_name(grocery_data.name)
|
||||
db_grocery = Grocery(name=normalized_name)
|
||||
db_grocery = Grocery(
|
||||
name=normalized_name,
|
||||
color=grocery_data.color
|
||||
)
|
||||
db.add(db_grocery)
|
||||
db.commit()
|
||||
db.refresh(db_grocery)
|
||||
@ -30,6 +33,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.
|
||||
|
||||
Args:
|
||||
db (Session): The database session
|
||||
|
@ -9,6 +9,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)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
|
@ -5,12 +5,14 @@ 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")
|
||||
|
||||
class GroceryCreate(GroceryBase):
|
||||
pass
|
||||
|
||||
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")
|
||||
|
||||
class GrocerySchema(GroceryBase):
|
||||
id: UUID
|
||||
@ -23,6 +25,7 @@ class GrocerySchema(GroceryBase):
|
||||
"example": {
|
||||
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||
"name": "Milk",
|
||||
"color": "White",
|
||||
"created_at": "2023-01-01T12:00:00",
|
||||
"updated_at": "2023-01-01T12:00:00"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user