实现 Web API 路由
- 添加 LLM 配置 API (llm-configs.ts) - 添加数据源管理 API (sources.ts) - 添加推送渠道管理 API (channels.ts) - 添加主题管理 API (topics.ts) - 添加报告查询 API (reports.ts) - 添加资讯浏览 API (articles.ts) - 添加采集日志 API (collection-logs.ts) - 更新 app.ts 注册所有路由和调度器 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
881bbb64a3
commit
51eb7d4292
8 files changed
+741
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import prisma from '../../config/database';
|
||||
import { successResponse, paginatedResponse } from '../../utils/response';
|
||||
import { NotFoundError, BadRequestError } from '../../utils/errors';
|
||||
|
||||
export async function channelRoutes(app: FastifyInstance): Promise<void> {
|
||||
// 获取推送渠道列表
|
||||
app.get('/', async (request, reply) => {
|
||||
const { page = 1, limit = 20, type } = request.query as any;
|
||||
|
||||
const where: any = {};
|
||||
if (type) where.type = type;
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
prisma.channel.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip: (Number(page) - 1) * Number(limit),
|
||||
take: Number(limit),
|
||||
}),
|
||||
prisma.channel.count({ where }),
|
||||
]);
|
||||
|
||||
paginatedResponse(reply, items, total, Number(page), Number(limit));
|
||||
});
|
||||
|
||||
// 获取单个推送渠道
|
||||
app.get('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
|
||||
const channel = await prisma.channel.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
include: {
|
||||
topicChannels: { include: { topic: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!channel) {
|
||||
throw new NotFoundError('推送渠道不存在');
|
||||
}
|
||||
|
||||
successResponse(reply, channel);
|
||||
});
|
||||
|
||||
// 创建推送渠道
|
||||
app.post('/', async (request, reply) => {
|
||||
const { name, type, config, enabled } = request.body as any;
|
||||
|
||||
if (!name || !type || !config) {
|
||||
throw new BadRequestError('缺少必填字段');
|
||||
}
|
||||
|
||||
const channel = await prisma.channel.create({
|
||||
data: { name, type, config, enabled: enabled ?? true },
|
||||
});
|
||||
|
||||
successResponse(reply, channel, '推送渠道创建成功', 201);
|
||||
});
|
||||
|
||||
// 更新推送渠道
|
||||
app.put('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
const { name, type, config, enabled } = request.body as any;
|
||||
|
||||
const existing = await prisma.channel.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw new NotFoundError('推送渠道不存在');
|
||||
}
|
||||
|
||||
const channel = await prisma.channel.update({
|
||||
where: { id: parseInt(id) },
|
||||
data: { name, type, config, enabled },
|
||||
});
|
||||
|
||||
successResponse(reply, channel, '推送渠道更新成功');
|
||||
});
|
||||
|
||||
// 删除推送渠道
|
||||
app.delete('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
|
||||
const existing = await prisma.channel.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw new NotFoundError('推送渠道不存在');
|
||||
}
|
||||
|
||||
await prisma.channel.delete({ where: { id: parseInt(id) } });
|
||||
|
||||
successResponse(reply, null, '推送渠道删除成功');
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user