api-server/src/modules/knowledge-items/knowledge-items.repository.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

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,
});
}
}