c6206787da
项目结构: - backend/ Python FastAPI 后端 - uni-app/ uni-app跨端前端 - docs/ 设计文档 - docker-compose.yml Docker编排 - nginx/scripts/systemd 运维配置 已完成功能: - 用户认证 (JWT) - 智能翻译 + 回复建议 - 营销素材生成 - 客户管理 + 沉默检测 - 报价单管理 - 产品库管理 - 汇率换算 - 推送通知 (uni-push) - WhatsApp Webhook框架 - Celery定时任务
101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from typing import Annotated, Optional
|
|
from app.database import get_db
|
|
from app.services.product import ProductService
|
|
from app.api.v1.deps import get_current_user_id
|
|
from pydantic import BaseModel
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class ProductCreate(BaseModel):
|
|
name: str
|
|
name_en: Optional[str] = None
|
|
description: Optional[str] = None
|
|
description_en: Optional[str] = None
|
|
category: Optional[str] = None
|
|
price: Optional[str] = None
|
|
price_unit: Optional[str] = "USD"
|
|
moq: Optional[str] = None
|
|
keywords: Optional[list] = []
|
|
specifications: Optional[dict] = {}
|
|
images: Optional[list] = []
|
|
|
|
|
|
class ProductUpdate(BaseModel):
|
|
name: Optional[str] = None
|
|
name_en: Optional[str] = None
|
|
description: Optional[str] = None
|
|
description_en: Optional[str] = None
|
|
category: Optional[str] = None
|
|
price: Optional[str] = None
|
|
price_unit: Optional[str] = None
|
|
moq: Optional[str] = None
|
|
keywords: Optional[list] = None
|
|
specifications: Optional[dict] = None
|
|
images: Optional[list] = None
|
|
is_active: Optional[bool] = None
|
|
|
|
|
|
@router.get("")
|
|
async def list_products(
|
|
category: Optional[str] = None,
|
|
page: int = Query(1, ge=1),
|
|
size: int = Query(20, ge=1, le=100),
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: Annotated[AsyncSession, Depends(get_db)] = None,
|
|
):
|
|
service = ProductService(db)
|
|
return await service.list_products(user_id, category, page, size)
|
|
|
|
|
|
@router.get("/{product_id}")
|
|
async def get_product(
|
|
product_id: str,
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: Annotated[AsyncSession, Depends(get_db)] = None,
|
|
):
|
|
service = ProductService(db)
|
|
product = await service.get_product(user_id, product_id)
|
|
if not product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return product
|
|
|
|
|
|
@router.post("")
|
|
async def create_product(
|
|
data: ProductCreate,
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: Annotated[AsyncSession, Depends(get_db)] = None,
|
|
):
|
|
service = ProductService(db)
|
|
product = await service.create_product(user_id, data.dict())
|
|
return product
|
|
|
|
|
|
@router.patch("/{product_id}")
|
|
async def update_product(
|
|
product_id: str,
|
|
data: ProductUpdate,
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: Annotated[AsyncSession, Depends(get_db)] = None,
|
|
):
|
|
service = ProductService(db)
|
|
product = await service.update_product(user_id, product_id, data.dict(exclude_unset=True))
|
|
if not product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return product
|
|
|
|
|
|
@router.delete("/{product_id}")
|
|
async def delete_product(
|
|
product_id: str,
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: Annotated[AsyncSession, Depends(get_db)] = None,
|
|
):
|
|
service = ProductService(db)
|
|
deleted = await service.delete_product(user_id, product_id)
|
|
if not deleted:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return {"message": "Product deleted"} |