实现 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,197 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import prisma from '../../config/database';
|
||||
import { successResponse, paginatedResponse } from '../../utils/response';
|
||||
import { NotFoundError, BadRequestError } from '../../utils/errors';
|
||||
|
||||
export async function topicRoutes(app: FastifyInstance): Promise<void> {
|
||||
// 获取主题列表
|
||||
app.get('/', async (request, reply) => {
|
||||
const { page = 1, limit = 20 } = request.query as any;
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
prisma.topic.findMany({
|
||||
include: {
|
||||
llmConfig: true,
|
||||
topicSources: { include: { source: true } },
|
||||
topicChannels: { include: { channel: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip: (Number(page) - 1) * Number(limit),
|
||||
take: Number(limit),
|
||||
}),
|
||||
prisma.topic.count(),
|
||||
]);
|
||||
|
||||
paginatedResponse(reply, items, total, Number(page), Number(limit));
|
||||
});
|
||||
|
||||
// 获取单个主题
|
||||
app.get('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
|
||||
const topic = await prisma.topic.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
include: {
|
||||
llmConfig: true,
|
||||
topicSources: { include: { source: true } },
|
||||
topicChannels: { include: { channel: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!topic) {
|
||||
throw new NotFoundError('主题不存在');
|
||||
}
|
||||
|
||||
successResponse(reply, topic);
|
||||
});
|
||||
|
||||
// 创建主题
|
||||
app.post('/', async (request, reply) => {
|
||||
const {
|
||||
name,
|
||||
description,
|
||||
processMode,
|
||||
llmConfigId,
|
||||
scheduleConfig,
|
||||
sourceIds,
|
||||
channelIds,
|
||||
enabled,
|
||||
} = request.body as any;
|
||||
|
||||
if (!name || !processMode || !scheduleConfig) {
|
||||
throw new BadRequestError('缺少必填字段');
|
||||
}
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
processMode,
|
||||
llmConfigId,
|
||||
scheduleConfig,
|
||||
enabled: enabled ?? true,
|
||||
},
|
||||
});
|
||||
|
||||
// 关联数据源
|
||||
if (sourceIds?.length > 0) {
|
||||
await prisma.topicSource.createMany({
|
||||
data: sourceIds.map((sourceId: number) => ({
|
||||
topicId: topic.id,
|
||||
sourceId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// 关联推送渠道
|
||||
if (channelIds?.length > 0) {
|
||||
await prisma.topicChannel.createMany({
|
||||
data: channelIds.map((channelId: number) => ({
|
||||
topicId: topic.id,
|
||||
channelId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
successResponse(reply, topic, '主题创建成功', 201);
|
||||
});
|
||||
|
||||
// 更新主题
|
||||
app.put('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
const {
|
||||
name,
|
||||
description,
|
||||
processMode,
|
||||
llmConfigId,
|
||||
scheduleConfig,
|
||||
sourceIds,
|
||||
channelIds,
|
||||
enabled,
|
||||
} = request.body as any;
|
||||
|
||||
const existing = await prisma.topic.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw new NotFoundError('主题不存在');
|
||||
}
|
||||
|
||||
const topic = await prisma.topic.update({
|
||||
where: { id: parseInt(id) },
|
||||
data: { name, description, processMode, llmConfigId, scheduleConfig, enabled },
|
||||
});
|
||||
|
||||
// 更新数据源关联
|
||||
if (sourceIds !== undefined) {
|
||||
await prisma.topicSource.deleteMany({ where: { topicId: topic.id } });
|
||||
if (sourceIds.length > 0) {
|
||||
await prisma.topicSource.createMany({
|
||||
data: sourceIds.map((sourceId: number) => ({
|
||||
topicId: topic.id,
|
||||
sourceId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 更新推送渠道关联
|
||||
if (channelIds !== undefined) {
|
||||
await prisma.topicChannel.deleteMany({ where: { topicId: topic.id } });
|
||||
if (channelIds.length > 0) {
|
||||
await prisma.topicChannel.createMany({
|
||||
data: channelIds.map((channelId: number) => ({
|
||||
topicId: topic.id,
|
||||
channelId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
successResponse(reply, topic, '主题更新成功');
|
||||
});
|
||||
|
||||
// 删除主题
|
||||
app.delete('/:id', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
|
||||
const existing = await prisma.topic.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw new NotFoundError('主题不存在');
|
||||
}
|
||||
|
||||
await prisma.topic.delete({ where: { id: parseInt(id) } });
|
||||
|
||||
successResponse(reply, null, '主题删除成功');
|
||||
});
|
||||
|
||||
// 手动生成报告
|
||||
app.post('/:id/generate-report', async (request, reply) => {
|
||||
const { id } = request.params as any;
|
||||
const { ReportService } = await import('../../services/report.service');
|
||||
const { DistributorService } = await import('../../services/distributor.service');
|
||||
|
||||
const topic = await prisma.topic.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
|
||||
if (!topic) {
|
||||
throw new NotFoundError('主题不存在');
|
||||
}
|
||||
|
||||
const reportService = new ReportService();
|
||||
const distributorService = new DistributorService();
|
||||
|
||||
reportService.generateReportForTopic(topic.id).then(async (reportId) => {
|
||||
await distributorService.distributeReportToAllChannels(reportId);
|
||||
}).catch((error) => {
|
||||
console.error('报告生成失败:', error);
|
||||
});
|
||||
|
||||
successResponse(reply, null, '报告生成任务已触发');
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user