- 添加 .env.example 环境变量配置示例 - 添加 .gitignore 忽略文件配置 - 添加 core/config.py 配置管理模块 - 添加 deployments/k8s/configmap.yaml Kubernetes 配置 - 添加 core/database.py 数据库连接管理模块 - 添加 core/dependencies.py 全局依赖模块 - 添加 DEPENDENCIES_UPDATED.md 依赖更新记录 - 添加 deployments/k8s/deployment.yaml Kubernetes 部署配置- 添加 deployments/swarm/docker-compose.swarm.yml Docker Swarm 部署配置 - 添加 deployments/docker/docker-compose.yml Docker 部署配置 - 添加 deployments/docker/Dockerfile 应用镜像构建文件 - 添加 middleware/error_handler.py 全局异常处理中间件
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
"""
|
|
App B Router - API endpoints for product management.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from core.database import get_session
|
|
from core.responses import ApiResponse, success, paginated, ERROR_RESPONSES
|
|
from core.dependencies import get_trace_id
|
|
from apps.app_b.schemas import ProductCreate, ProductResponse
|
|
from apps.app_b.services import ProductService
|
|
|
|
router = APIRouter(
|
|
prefix="/app-b",
|
|
tags=["App B - Products"]
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/products",
|
|
response_model=ApiResponse[ProductResponse],
|
|
status_code=201,
|
|
summary="Create a new product",
|
|
description="Create a new product with SKU, name, price, and stock",
|
|
responses={
|
|
201: {"description": "Product created successfully"},
|
|
400: ERROR_RESPONSES[400],
|
|
409: ERROR_RESPONSES[409],
|
|
422: ERROR_RESPONSES[422],
|
|
500: ERROR_RESPONSES[500],
|
|
}
|
|
)
|
|
async def create_product(
|
|
product_data: ProductCreate,
|
|
session: AsyncSession = Depends(get_session),
|
|
trace_id: str = Depends(get_trace_id)
|
|
) -> ApiResponse[ProductResponse]:
|
|
"""Create a new product."""
|
|
product = await ProductService.create_product(session, product_data)
|
|
product_response = ProductResponse.model_validate(product)
|
|
return success(data=product_response, message="Product created successfully", trace_id=trace_id)
|
|
|
|
|
|
@router.get(
|
|
"/products/{product_id}",
|
|
response_model=ApiResponse[ProductResponse],
|
|
summary="Get product by ID",
|
|
description="Retrieve a product by its ID",
|
|
responses={
|
|
200: {"description": "Product retrieved successfully"},
|
|
404: ERROR_RESPONSES[404],
|
|
500: ERROR_RESPONSES[500],
|
|
}
|
|
)
|
|
async def get_product(
|
|
product_id: int,
|
|
session: AsyncSession = Depends(get_session),
|
|
trace_id: str = Depends(get_trace_id)
|
|
) -> ApiResponse[ProductResponse]:
|
|
"""Get product by ID."""
|
|
product = await ProductService.get_product(session, product_id)
|
|
product_response = ProductResponse.model_validate(product)
|
|
return success(data=product_response, trace_id=trace_id)
|
|
|
|
|
|
@router.get(
|
|
"/products",
|
|
response_model=ApiResponse,
|
|
summary="List all products",
|
|
description="List all products with pagination support",
|
|
responses={
|
|
200: {"description": "Products retrieved successfully"},
|
|
400: ERROR_RESPONSES[400],
|
|
500: ERROR_RESPONSES[500],
|
|
}
|
|
)
|
|
async def list_products(
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
session: AsyncSession = Depends(get_session),
|
|
trace_id: str = Depends(get_trace_id)
|
|
) -> ApiResponse:
|
|
"""List all products with pagination."""
|
|
skip = (page - 1) * page_size
|
|
products, total = await ProductService.list_products(session, skip, page_size)
|
|
product_responses = [ProductResponse.model_validate(product) for product in products]
|
|
return paginated(
|
|
items=product_responses,
|
|
total=total,
|
|
page=page,
|
|
page_size=page_size,
|
|
trace_id=trace_id
|
|
)
|