
- 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
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from typing import Optional
|
|
from pydantic import BaseModel, Field, validator
|
|
|
|
|
|
class ProductBase(BaseModel):
|
|
"""Base schema for product data."""
|
|
name: str = Field(..., title="Product name", min_length=1, max_length=255)
|
|
description: Optional[str] = Field(None, title="Product description")
|
|
price: float = Field(..., title="Product price", ge=0.01)
|
|
stock: int = Field(..., title="Available quantity", ge=0)
|
|
image_url: Optional[str] = Field(None, title="Product image URL")
|
|
is_active: bool = Field(True, title="Product availability status")
|
|
|
|
@validator('price')
|
|
def validate_price(cls, v):
|
|
if v < 0:
|
|
raise ValueError('Price must be positive')
|
|
return round(v, 2)
|
|
|
|
|
|
class ProductCreate(ProductBase):
|
|
"""Schema for creating a new product."""
|
|
pass
|
|
|
|
|
|
class ProductUpdate(BaseModel):
|
|
"""Schema for updating an existing product."""
|
|
name: Optional[str] = Field(None, title="Product name", min_length=1, max_length=255)
|
|
description: Optional[str] = Field(None, title="Product description")
|
|
price: Optional[float] = Field(None, title="Product price", ge=0.01)
|
|
stock: Optional[int] = Field(None, title="Available quantity", ge=0)
|
|
image_url: Optional[str] = Field(None, title="Product image URL")
|
|
is_active: Optional[bool] = Field(None, title="Product availability status")
|
|
|
|
@validator('price')
|
|
def validate_price(cls, v):
|
|
if v is not None and v < 0:
|
|
raise ValueError('Price must be positive')
|
|
if v is not None:
|
|
return round(v, 2)
|
|
return v
|
|
|
|
|
|
class ProductInDB(ProductBase):
|
|
"""Schema for product data retrieved from the database."""
|
|
id: int
|
|
created_at: str
|
|
updated_at: str
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class Product(ProductInDB):
|
|
"""Schema for product data returned in API responses."""
|
|
pass |