2026-05-09 18:25:04 +08:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2026-05-17 00:39:46 +08:00
|
|
|
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
2026-05-09 18:25:04 +08:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class KnowledgeBaseRepository {
|
2026-05-17 00:39:46 +08:00
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
async create(userId: string, dto: { title: string; description?: string }) {
|
|
|
|
|
return this.prisma.knowledgeBase.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId,
|
|
|
|
|
title: dto.title,
|
|
|
|
|
description: dto.description ?? '',
|
|
|
|
|
status: 'active',
|
|
|
|
|
itemCount: 0,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async findById(id: string) {
|
|
|
|
|
return this.prisma.knowledgeBase.findUnique({ where: { id } });
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async findAllByUserId(userId: string) {
|
|
|
|
|
return this.prisma.knowledgeBase.findMany({
|
|
|
|
|
where: { userId, deletedAt: null },
|
|
|
|
|
orderBy: { updatedAt: 'desc' },
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async countByUserId(userId: string) {
|
|
|
|
|
return this.prisma.knowledgeBase.count({
|
|
|
|
|
where: { userId, deletedAt: null },
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async update(id: string, dto: { title?: string; description?: string }) {
|
|
|
|
|
return this.prisma.knowledgeBase.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: dto,
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async softDelete(id: string) {
|
|
|
|
|
await this.prisma.knowledgeBase.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: { deletedAt: new Date() },
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|