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: { items: 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 }; } }