All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 41s
- DELETE /knowledge-items/:id 软删除(设 deletedAt) - POST /knowledge-items/batch-delete 批量软删除 - softDelete 校验归属权 + 自动更新父KB的itemCount Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
|
|
|
@Injectable()
|
|
export class KnowledgeItemsRepository {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async create(userId: string, knowledgeBaseId: string, dto: {
|
|
title?: string;
|
|
content?: string;
|
|
parentId?: string;
|
|
itemType?: string;
|
|
orderIndex?: number;
|
|
}) {
|
|
const item = await this.prisma.knowledgeItem.create({
|
|
data: {
|
|
userId,
|
|
knowledgeBaseId,
|
|
title: dto.title ?? '',
|
|
content: dto.content ?? '',
|
|
parentId: dto.parentId ?? null,
|
|
itemType: dto.itemType ?? 'lesson',
|
|
orderIndex: dto.orderIndex ?? 0,
|
|
},
|
|
});
|
|
|
|
// 更新知识库 itemCount
|
|
await this.prisma.knowledgeBase.update({
|
|
where: { id: knowledgeBaseId },
|
|
data: { itemCount: { increment: 1 } },
|
|
});
|
|
|
|
return item;
|
|
}
|
|
|
|
async findById(id: string) {
|
|
return this.prisma.knowledgeItem.findUnique({ where: { id } });
|
|
}
|
|
|
|
async findByKnowledgeBaseId(knowledgeBaseId: string) {
|
|
return this.prisma.knowledgeItem.findMany({
|
|
where: { knowledgeBaseId, deletedAt: null },
|
|
orderBy: { orderIndex: 'asc' },
|
|
});
|
|
}
|
|
|
|
async update(id: string, dto: Record<string, any>) {
|
|
return this.prisma.knowledgeItem.update({
|
|
where: { id },
|
|
data: dto,
|
|
});
|
|
}
|
|
|
|
async softDelete(id: string) {
|
|
const item = await this.prisma.knowledgeItem.update({
|
|
where: { id },
|
|
data: { deletedAt: new Date() },
|
|
});
|
|
|
|
// 更新知识库 itemCount
|
|
await this.prisma.knowledgeBase.update({
|
|
where: { id: item.knowledgeBaseId },
|
|
data: { itemCount: { decrement: 1 } },
|
|
});
|
|
|
|
return item;
|
|
}
|
|
}
|