From 0e85231712df0d7ea16767d01b39744141e8d416 Mon Sep 17 00:00:00 2001 From: WangDL Date: Fri, 22 May 2026 15:54:06 +0800 Subject: [PATCH] feat: admin knowledge base list with Prisma --- src/app.module.ts | 2 + .../admin-knowledge.controller.ts | 50 +++++++++++++++++++ .../admin-knowledge/admin-knowledge.module.ts | 11 ++++ 3 files changed, 63 insertions(+) create mode 100644 src/modules/admin-knowledge/admin-knowledge.controller.ts create mode 100644 src/modules/admin-knowledge/admin-knowledge.module.ts diff --git a/src/app.module.ts b/src/app.module.ts index 69f840b..8b5a0c3 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -15,6 +15,7 @@ import { AuthModule } from './modules/auth/auth.module'; import { AdminAuthModule } from './modules/admin-auth/admin-auth.module'; import { AdminDashboardModule } from './modules/admin-dashboard/admin-dashboard.module'; import { AdminUsersModule } from './modules/admin-users/admin-users.module'; +import { AdminKnowledgeModule } from './modules/admin-knowledge/admin-knowledge.module'; import { AdminCostsModule } from './modules/admin-costs/admin-costs.module'; import { AdminBillingModule } from './modules/admin-billing/admin-billing.module'; import { AdminServersModule } from './modules/admin-servers/admin-servers.module'; @@ -92,6 +93,7 @@ import appleConfig from './config/apple.config'; AdminAuthModule, AdminDashboardModule, AdminUsersModule, + AdminKnowledgeModule, AdminCostsModule, AdminBillingModule, AdminServersModule, diff --git a/src/modules/admin-knowledge/admin-knowledge.controller.ts b/src/modules/admin-knowledge/admin-knowledge.controller.ts new file mode 100644 index 0000000..caff0c2 --- /dev/null +++ b/src/modules/admin-knowledge/admin-knowledge.controller.ts @@ -0,0 +1,50 @@ +import { Controller, Get, Param, Delete, Query, UseGuards } from '@nestjs/common'; +import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; +import { PrismaService } from '../../infrastructure/database/prisma.service'; +import { AdminAuthGuard } from '../../common/guards/admin-auth.guard'; +import { AdminRolesGuard } from '../../common/guards/admin-roles.guard'; + +@ApiTags('admin-knowledge') +@Controller('admin-api/knowledge-bases') +@UseGuards(AdminAuthGuard, AdminRolesGuard) +@ApiBearerAuth() +export class AdminKnowledgeController { + constructor(private readonly prisma: PrismaService) {} + + @Get() + @ApiOperation({ summary: '知识库列表(管理员)' }) + async list(@Query('page') page = '1', @Query('limit') limit = '20') { + const p = parseInt(page), l = parseInt(limit); + const [items, total] = await Promise.all([ + this.prisma.knowledgeBase.findMany({ + where: { deletedAt: null }, + orderBy: { updatedAt: 'desc' }, + skip: (p - 1) * l, take: l, + include: { user: { select: { nickname: true, email: true } } }, + }), + this.prisma.knowledgeBase.count({ where: { deletedAt: null } }), + ]); + return { items, total, page: p, limit: l, totalPages: Math.ceil(total / l) }; + } + + @Get(':id') + @ApiOperation({ summary: '知识库详情' }) + async detail(@Param('id') id: string) { + const kb = await this.prisma.knowledgeBase.findUnique({ + where: { id }, + include: { + user: { select: { nickname: true, email: true } }, + sources: { where: { deletedAt: null }, take: 20 }, + _count: { select: { knowledgeItems: true } }, + }, + }); + return kb; + } + + @Delete(':id') + @ApiOperation({ summary: '删除知识库' }) + async remove(@Param('id') id: string) { + await this.prisma.knowledgeBase.update({ where: { id }, data: { deletedAt: new Date() } }); + return { success: true }; + } +} diff --git a/src/modules/admin-knowledge/admin-knowledge.module.ts b/src/modules/admin-knowledge/admin-knowledge.module.ts new file mode 100644 index 0000000..18de15e --- /dev/null +++ b/src/modules/admin-knowledge/admin-knowledge.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { AdminKnowledgeController } from './admin-knowledge.controller'; +import { PrismaService } from '../../infrastructure/database/prisma.service'; +import { AdminAuthGuard } from '../../common/guards/admin-auth.guard'; +import { AdminRolesGuard } from '../../common/guards/admin-roles.guard'; + +@Module({ + controllers: [AdminKnowledgeController], + providers: [PrismaService, AdminAuthGuard, AdminRolesGuard], +}) +export class AdminKnowledgeModule {}