83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from backend.database import get_db
|
|
from backend import crud
|
|
from backend.schemas import Channel, ChannelCreate, ChannelUpdate, ChannelList
|
|
|
|
router = APIRouter(prefix="/api/channels", tags=["channels"])
|
|
|
|
@router.get("", response_model=ChannelList)
|
|
async def list_channels(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""获取所有通道列表"""
|
|
channels = await crud.get_channels(db, skip=skip, limit=limit)
|
|
total = len(channels)
|
|
return {"channels": channels, "total": total}
|
|
|
|
@router.get("/{channel_id}", response_model=Channel)
|
|
async def get_channel(channel_id: int, db: AsyncSession = Depends(get_db)):
|
|
"""获取单个通道详情"""
|
|
channel = await crud.get_channel(db, channel_id)
|
|
if not channel:
|
|
raise HTTPException(status_code=404, detail="Channel not found")
|
|
return channel
|
|
|
|
@router.post("", response_model=Channel, status_code=201)
|
|
async def create_channel(
|
|
channel: ChannelCreate,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""创建新通道"""
|
|
existing = await crud.get_channel_by_name(db, channel.name)
|
|
if existing:
|
|
raise HTTPException(status_code=400, detail="Channel name already exists")
|
|
|
|
return await crud.create_channel(db, channel.model_dump())
|
|
|
|
@router.put("/{channel_id}", response_model=Channel)
|
|
async def update_channel(
|
|
channel_id: int,
|
|
channel: ChannelUpdate,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""更新通道"""
|
|
if channel.name:
|
|
existing = await crud.get_channel_by_name(db, channel.name)
|
|
if existing and existing.id != channel_id:
|
|
raise HTTPException(status_code=400, detail="Channel name already exists")
|
|
|
|
updated = await crud.update_channel(db, channel_id, channel.model_dump(exclude_unset=True))
|
|
if not updated:
|
|
raise HTTPException(status_code=404, detail="Channel not found")
|
|
return updated
|
|
|
|
@router.delete("/{channel_id}")
|
|
async def delete_channel(channel_id: int, db: AsyncSession = Depends(get_db)):
|
|
"""删除通道"""
|
|
success = await crud.delete_channel(db, channel_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Channel not found")
|
|
return {"message": "Channel deleted successfully"}
|
|
|
|
@router.post("/{channel_id}/test")
|
|
async def test_channel(channel_id: int, db: AsyncSession = Depends(get_db)):
|
|
"""测试通道配置"""
|
|
from backend.notify_service import NotifyService
|
|
|
|
service = NotifyService()
|
|
result = await service.send_notification(
|
|
db, channel_id, "测试通知", "这是一条测试消息", "normal"
|
|
)
|
|
|
|
if result["status"] == "sent":
|
|
return {"success": True, "message": "Test notification sent successfully"}
|
|
else:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Test failed: {result.get('error_msg', 'Unknown error')}"
|
|
)
|