2026-05-09 18:25:04 +08:00
|
|
|
import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
|
|
|
|
|
import { KnowledgeBaseRepository } from './knowledge-base.repository';
|
|
|
|
|
import { MAX_KNOWLEDGE_BASE_COUNT } from './constants/knowledge-base.constants';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class KnowledgeBaseService {
|
|
|
|
|
constructor(private readonly repository: KnowledgeBaseRepository) {}
|
|
|
|
|
|
|
|
|
|
async create(userId: string, dto: any) {
|
|
|
|
|
const count = await this.repository.countByUserId(userId);
|
|
|
|
|
if (count >= MAX_KNOWLEDGE_BASE_COUNT) {
|
|
|
|
|
throw new BadRequestException('知识库数量已达到上限');
|
|
|
|
|
}
|
|
|
|
|
return this.repository.create(userId, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAll(userId: string, query: any) {
|
|
|
|
|
return this.repository.findAllByUserId(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(userId: string, id: string) {
|
|
|
|
|
const kb = await this.repository.findById(id);
|
2026-05-09 18:57:33 +08:00
|
|
|
if (!kb || String(kb.userId) !== userId) {
|
2026-05-09 18:25:04 +08:00
|
|
|
throw new NotFoundException('知识库不存在');
|
|
|
|
|
}
|
|
|
|
|
return kb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(userId: string, id: string, dto: any) {
|
|
|
|
|
const kb = await this.repository.findById(id);
|
2026-05-09 18:57:33 +08:00
|
|
|
if (!kb || String(kb.userId) !== userId) {
|
2026-05-09 18:25:04 +08:00
|
|
|
throw new NotFoundException('知识库不存在');
|
|
|
|
|
}
|
|
|
|
|
return this.repository.update(id, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async remove(userId: string, id: string) {
|
|
|
|
|
const kb = await this.repository.findById(id);
|
2026-05-09 18:57:33 +08:00
|
|
|
if (!kb || String(kb.userId) !== userId) {
|
2026-05-09 18:25:04 +08:00
|
|
|
throw new NotFoundException('知识库不存在');
|
|
|
|
|
}
|
|
|
|
|
return this.repository.softDelete(id);
|
|
|
|
|
}
|
|
|
|
|
}
|