
- Set up project structure with FastAPI and SQLite - Create models for products and cart items - Implement schemas for API request/response validation - Add database migrations with Alembic - Create service layer for business logic - Implement API endpoints for cart operations - Add health endpoint for monitoring - Update documentation - Fix linting issues
77 lines
1.7 KiB
Python
77 lines
1.7 KiB
Python
from typing import List
|
|
from pydantic import BaseModel, Field, validator
|
|
|
|
|
|
class CartItemBase(BaseModel):
|
|
"""Base schema for cart item data."""
|
|
product_id: int = Field(..., title="Product ID")
|
|
quantity: int = Field(..., title="Quantity", ge=1)
|
|
|
|
@validator('quantity')
|
|
def validate_quantity(cls, v):
|
|
if v < 1:
|
|
raise ValueError('Quantity must be at least 1')
|
|
return v
|
|
|
|
|
|
class CartItemCreate(CartItemBase):
|
|
"""Schema for adding an item to a cart."""
|
|
pass
|
|
|
|
|
|
class CartItemUpdate(BaseModel):
|
|
"""Schema for updating a cart item."""
|
|
quantity: int = Field(..., title="New quantity", ge=1)
|
|
|
|
@validator('quantity')
|
|
def validate_quantity(cls, v):
|
|
if v < 1:
|
|
raise ValueError('Quantity must be at least 1')
|
|
return v
|
|
|
|
|
|
class CartItemResponse(CartItemBase):
|
|
"""Schema for cart item data in API responses."""
|
|
id: int
|
|
unit_price: float
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class CartItemDetail(CartItemResponse):
|
|
"""Schema for detailed cart item data including product details."""
|
|
product_name: str
|
|
subtotal: float
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class CartBase(BaseModel):
|
|
"""Base schema for cart data."""
|
|
user_id: str = Field(..., title="User ID")
|
|
is_active: int = Field(1, title="Cart status")
|
|
|
|
|
|
class CartCreate(CartBase):
|
|
"""Schema for creating a new cart."""
|
|
pass
|
|
|
|
|
|
class CartResponse(CartBase):
|
|
"""Schema for cart data in API responses."""
|
|
id: int
|
|
created_at: str
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class CartDetail(CartResponse):
|
|
"""Schema for detailed cart data including items."""
|
|
items: List[CartItemDetail] = []
|
|
total: float
|
|
|
|
class Config:
|
|
orm_mode = True |