mirror of
https://git.oceanpay.cc/danial/kami_apple_exchage.git
synced 2025-12-18 21:23:49 +00:00
- 新增 CODEBUDDY.md、GEMINI.md、GEMINI_CN.md 等项目文档 - 更新 Dockerfile 和其他配置文件 - 优化部分代码结构,如 orders.py、tasks.py 等 - 新增 .dockerignore 文件
103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
"""
|
|
链接相关的API接口
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_async_db
|
|
from app.services.link_service import LinksService
|
|
from app.schemas.link import (
|
|
LinkCreate,
|
|
LinkListResponse,
|
|
LinkResponse,
|
|
)
|
|
from app.core.log import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
router = APIRouter()
|
|
|
|
|
|
def get_link_service(db: AsyncSession = Depends(get_async_db)) -> LinksService:
|
|
"""获取链接服务实例"""
|
|
return LinksService(db)
|
|
|
|
|
|
@router.post("/", response_model=LinkResponse)
|
|
async def create_link(
|
|
link_data: LinkCreate, link_service: LinksService = Depends(get_link_service)
|
|
):
|
|
"""创建新链接"""
|
|
try:
|
|
return await link_service.create_link(link_data)
|
|
except ValueError as e:
|
|
logger.warning(
|
|
f"创建链接失败 - 参数验证错误: {str(e)}", link_data=link_data.model_dump()
|
|
)
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
logger.error(
|
|
f"创建链接失败: {str(e)}", link_data=link_data.model_dump(), exc_info=True
|
|
)
|
|
raise HTTPException(status_code=500, detail="创建链接失败")
|
|
|
|
|
|
@router.get("/", response_model=LinkListResponse)
|
|
async def get_links(
|
|
page: int = Query(1, ge=1, description="页码"),
|
|
size: int = Query(20, ge=1, le=1000, description="每页大小"),
|
|
min_amount: float | None = Query(None, description="最小金额"),
|
|
max_amount: float | None = Query(None, description="最大金额"),
|
|
url_pattern: str | None = Query(None, description="URL模式"),
|
|
link_service: LinksService = Depends(get_link_service),
|
|
):
|
|
"""获取链接列表"""
|
|
try:
|
|
return await link_service.get_links(
|
|
page=page,
|
|
size=size,
|
|
min_amount=min_amount,
|
|
max_amount=max_amount,
|
|
url_pattern=url_pattern,
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"获取链接列表失败: {str(e)}", exc_info=True)
|
|
raise HTTPException(status_code=500, detail="获取链接列表失败")
|
|
|
|
|
|
@router.get("/{link_id}", response_model=LinkResponse)
|
|
async def get_link(
|
|
link_id: int, link_service: LinksService = Depends(get_link_service)
|
|
):
|
|
"""获取单个链接详情"""
|
|
try:
|
|
link = await link_service.get_link(str(link_id))
|
|
if not link:
|
|
logger.warning(f"获取链接详情失败 - 链接不存在: {link_id}")
|
|
raise HTTPException(status_code=404, detail="链接不存在")
|
|
return link
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"获取链接详情失败: {str(e)}", link_id=link_id, exc_info=True)
|
|
raise HTTPException(status_code=500, detail="获取链接详情失败")
|
|
|
|
|
|
@router.delete("/{link_id}")
|
|
async def delete_link(
|
|
link_id: str, link_service: LinksService = Depends(get_link_service)
|
|
):
|
|
"""删除链接"""
|
|
try:
|
|
success = await link_service.delete_link(link_id)
|
|
if not success:
|
|
logger.warning(f"删除链接失败 - 链接不存在: {link_id}")
|
|
raise HTTPException(status_code=404, detail="链接不存在")
|
|
logger.info(f"链接删除成功: {link_id}")
|
|
return {"message": "链接删除成功"}
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"删除链接失败: {str(e)}", link_id=link_id, exc_info=True)
|
|
raise HTTPException(status_code=500, detail="删除链接失败")
|