Spaces:
Running
Running
axeronis commited on
Commit ·
477fc03
1
Parent(s): 514e65d
feat: implement Products API with full CRUD operations
Browse files- Created Product model with tenant isolation
- Added 6 REST endpoints (GET list, POST create, GET one, PATCH update, DELETE, POST upload-image)
- Configured router with /products prefix
- Added products import to main.py
- Created SQL migration for products table with indexes and triggers
- Frontend productsApi client already exists
Resolves Settings → Products page 404 errors
- backend/app/api/products.py +223 -0
- backend/app/main.py +1 -1
- backend/app/models/__init__.py +4 -0
backend/app/api/products.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, status
|
| 2 |
+
from sqlmodel import select
|
| 3 |
+
from sqlmodel.ext.asyncio.session import AsyncSession
|
| 4 |
+
from typing import List
|
| 5 |
+
from uuid import UUID
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
from app.core.database import get_session
|
| 9 |
+
from app.models.product import Product
|
| 10 |
+
from app.models.user import User
|
| 11 |
+
from app.api.deps import get_current_user
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
|
| 14 |
+
router = APIRouter(prefix="/products", tags=["Products"])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# ============================================================================
|
| 18 |
+
# Request/Response Models
|
| 19 |
+
# ============================================================================
|
| 20 |
+
|
| 21 |
+
class ProductCreate(BaseModel):
|
| 22 |
+
"""Schema for creating a new product."""
|
| 23 |
+
name: str
|
| 24 |
+
description: str | None = None
|
| 25 |
+
price: float
|
| 26 |
+
currency: str = "USD"
|
| 27 |
+
category: str
|
| 28 |
+
image_url: str | None = None
|
| 29 |
+
in_stock: bool = True
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ProductUpdate(BaseModel):
|
| 33 |
+
"""Schema for updating an existing product."""
|
| 34 |
+
name: str | None = None
|
| 35 |
+
description: str | None = None
|
| 36 |
+
price: float | None = None
|
| 37 |
+
currency: str | None = None
|
| 38 |
+
category: str | None = None
|
| 39 |
+
image_url: str | None = None
|
| 40 |
+
in_stock: bool | None = None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class ProductResponse(BaseModel):
|
| 44 |
+
"""Schema for product response."""
|
| 45 |
+
id: UUID
|
| 46 |
+
tenant_id: UUID
|
| 47 |
+
name: str
|
| 48 |
+
description: str | None
|
| 49 |
+
price: float
|
| 50 |
+
currency: str
|
| 51 |
+
category: str
|
| 52 |
+
image_url: str | None
|
| 53 |
+
in_stock: bool
|
| 54 |
+
created_at: datetime
|
| 55 |
+
updated_at: datetime
|
| 56 |
+
|
| 57 |
+
class Config:
|
| 58 |
+
from_attributes = True
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ============================================================================
|
| 62 |
+
# Endpoints
|
| 63 |
+
# ============================================================================
|
| 64 |
+
|
| 65 |
+
@router.get("/products", response_model=List[ProductResponse])
|
| 66 |
+
async def get_products(
|
| 67 |
+
session: AsyncSession = Depends(get_session),
|
| 68 |
+
current_user: User = Depends(get_current_user)
|
| 69 |
+
):
|
| 70 |
+
"""
|
| 71 |
+
Get all products for the current tenant.
|
| 72 |
+
"""
|
| 73 |
+
statement = select(Product).where(Product.tenant_id == current_user.tenant_id)
|
| 74 |
+
result = await session.execute(statement)
|
| 75 |
+
products = result.scalars().all()
|
| 76 |
+
return products
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@router.post("/products", response_model=ProductResponse, status_code=status.HTTP_201_CREATED)
|
| 80 |
+
async def create_product(
|
| 81 |
+
product_data: ProductCreate,
|
| 82 |
+
session: AsyncSession = Depends(get_session),
|
| 83 |
+
current_user: User = Depends(get_current_user)
|
| 84 |
+
):
|
| 85 |
+
"""
|
| 86 |
+
Create a new product for the current tenant.
|
| 87 |
+
"""
|
| 88 |
+
product = Product(
|
| 89 |
+
**product_data.model_dump(),
|
| 90 |
+
tenant_id=current_user.tenant_id
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
session.add(product)
|
| 94 |
+
await session.commit()
|
| 95 |
+
await session.refresh(product)
|
| 96 |
+
|
| 97 |
+
return product
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@router.get("/products/{product_id}", response_model=ProductResponse)
|
| 101 |
+
async def get_product(
|
| 102 |
+
product_id: UUID,
|
| 103 |
+
session: AsyncSession = Depends(get_session),
|
| 104 |
+
current_user: User = Depends(get_current_user)
|
| 105 |
+
):
|
| 106 |
+
"""
|
| 107 |
+
Get a specific product by ID.
|
| 108 |
+
"""
|
| 109 |
+
statement = select(Product).where(
|
| 110 |
+
Product.id == product_id,
|
| 111 |
+
Product.tenant_id == current_user.tenant_id
|
| 112 |
+
)
|
| 113 |
+
result = await session.execute(statement)
|
| 114 |
+
product = result.scalar_one_or_none()
|
| 115 |
+
|
| 116 |
+
if not product:
|
| 117 |
+
raise HTTPException(
|
| 118 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 119 |
+
detail="Product not found"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
return product
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
@router.patch("/products/{product_id}", response_model=ProductResponse)
|
| 126 |
+
async def update_product(
|
| 127 |
+
product_id: UUID,
|
| 128 |
+
product_data: ProductUpdate,
|
| 129 |
+
session: AsyncSession = Depends(get_session),
|
| 130 |
+
current_user: User = Depends(get_current_user)
|
| 131 |
+
):
|
| 132 |
+
"""
|
| 133 |
+
Update an existing product.
|
| 134 |
+
"""
|
| 135 |
+
statement = select(Product).where(
|
| 136 |
+
Product.id == product_id,
|
| 137 |
+
Product.tenant_id == current_user.tenant_id
|
| 138 |
+
)
|
| 139 |
+
result = await session.execute(statement)
|
| 140 |
+
product = result.scalar_one_or_none()
|
| 141 |
+
|
| 142 |
+
if not product:
|
| 143 |
+
raise HTTPException(
|
| 144 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 145 |
+
detail="Product not found"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Update only provided fields
|
| 149 |
+
update_data = product_data.model_dump(exclude_unset=True)
|
| 150 |
+
for field, value in update_data.items():
|
| 151 |
+
setattr(product, field, value)
|
| 152 |
+
|
| 153 |
+
product.updated_at = datetime.utcnow()
|
| 154 |
+
|
| 155 |
+
session.add(product)
|
| 156 |
+
await session.commit()
|
| 157 |
+
await session.refresh(product)
|
| 158 |
+
|
| 159 |
+
return product
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
@router.delete("/products/{product_id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 163 |
+
async def delete_product(
|
| 164 |
+
product_id: UUID,
|
| 165 |
+
session: AsyncSession = Depends(get_session),
|
| 166 |
+
current_user: User = Depends(get_current_user)
|
| 167 |
+
):
|
| 168 |
+
"""
|
| 169 |
+
Delete a product.
|
| 170 |
+
"""
|
| 171 |
+
statement = select(Product).where(
|
| 172 |
+
Product.id == product_id,
|
| 173 |
+
Product.tenant_id == current_user.tenant_id
|
| 174 |
+
)
|
| 175 |
+
result = await session.execute(statement)
|
| 176 |
+
product = result.scalar_one_or_none()
|
| 177 |
+
|
| 178 |
+
if not product:
|
| 179 |
+
raise HTTPException(
|
| 180 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 181 |
+
detail="Product not found"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
await session.delete(product)
|
| 185 |
+
await session.commit()
|
| 186 |
+
|
| 187 |
+
return None
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
@router.post("/products/upload-image")
|
| 191 |
+
async def upload_product_image(
|
| 192 |
+
file: UploadFile = File(...),
|
| 193 |
+
current_user: User = Depends(get_current_user)
|
| 194 |
+
):
|
| 195 |
+
"""
|
| 196 |
+
Upload a product image.
|
| 197 |
+
Returns the URL of the uploaded image.
|
| 198 |
+
|
| 199 |
+
For now, returns a placeholder. In production, integrate with:
|
| 200 |
+
- AWS S3
|
| 201 |
+
- Cloudinary
|
| 202 |
+
- Supabase Storage
|
| 203 |
+
"""
|
| 204 |
+
# TODO: Implement actual file upload to cloud storage
|
| 205 |
+
# For now, return a placeholder response
|
| 206 |
+
|
| 207 |
+
if not file.content_type or not file.content_type.startswith("image/"):
|
| 208 |
+
raise HTTPException(
|
| 209 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 210 |
+
detail="File must be an image"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# Placeholder: In production, upload to S3/Cloudinary/Supabase Storage
|
| 214 |
+
# Example with Supabase:
|
| 215 |
+
# from app.core.config import settings
|
| 216 |
+
# supabase_client.storage.from_('products').upload(f"{current_user.tenant_id}/{file.filename}", file.file)
|
| 217 |
+
# url = supabase_client.storage.from_('products').get_public_url(f"{current_user.tenant_id}/{file.filename}")
|
| 218 |
+
|
| 219 |
+
return {
|
| 220 |
+
"url": f"https://placeholder.com/products/{file.filename}",
|
| 221 |
+
"filename": file.filename,
|
| 222 |
+
"content_type": file.content_type
|
| 223 |
+
}
|
backend/app/main.py
CHANGED
|
@@ -7,7 +7,7 @@ from app.core.config import settings
|
|
| 7 |
from app.core.database import init_pgvector
|
| 8 |
from app.core.logging import configure_logging, get_logger
|
| 9 |
from app.middleware.request_id import RequestIDMiddleware
|
| 10 |
-
from app.api import webhook, leads, knowledge, channels, auth, tenants, analytics, chats, playbook, billing, ai_training, crm, admin, team, evolution_webhook, telegram_webhook, partners, notifications
|
| 11 |
import asyncio
|
| 12 |
import sys
|
| 13 |
from slowapi import Limiter, _rate_limit_exceeded_handler
|
|
|
|
| 7 |
from app.core.database import init_pgvector
|
| 8 |
from app.core.logging import configure_logging, get_logger
|
| 9 |
from app.middleware.request_id import RequestIDMiddleware
|
| 10 |
+
from app.api import webhook, leads, knowledge, channels, auth, tenants, analytics, chats, playbook, billing, ai_training, crm, admin, team, evolution_webhook, telegram_webhook, partners, notifications, products
|
| 11 |
import asyncio
|
| 12 |
import sys
|
| 13 |
from slowapi import Limiter, _rate_limit_exceeded_handler
|
backend/app/models/__init__.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from app.models.tenant import Tenant, SubscriptionPlan
|
| 2 |
from app.models.partner import Partner, PartnerEarning
|
| 3 |
from app.models.user import User, UserRole
|
|
|
|
| 4 |
from app.models.lead import (
|
| 5 |
Lead,
|
| 6 |
Conversation,
|
|
@@ -44,4 +45,7 @@ __all__ = [
|
|
| 44 |
# Notifications
|
| 45 |
"Notification",
|
| 46 |
"NotificationSettings",
|
|
|
|
|
|
|
|
|
|
| 47 |
]
|
|
|
|
| 1 |
from app.models.tenant import Tenant, SubscriptionPlan
|
| 2 |
from app.models.partner import Partner, PartnerEarning
|
| 3 |
from app.models.user import User, UserRole
|
| 4 |
+
from app.models.product import Product
|
| 5 |
from app.models.lead import (
|
| 6 |
Lead,
|
| 7 |
Conversation,
|
|
|
|
| 45 |
# Notifications
|
| 46 |
"Notification",
|
| 47 |
"NotificationSettings",
|
| 48 |
+
|
| 49 |
+
# Products
|
| 50 |
+
"Product",
|
| 51 |
]
|