import { Injectable } from '@nestjs/common'; import { generateShortId } from '../../common/utils/id.util'; import { KnowledgeBaseStatus } from './types/knowledge-base.types'; export interface KnowledgeBase { id: string; userId: string; title: string; description: string; status: KnowledgeBaseStatus; itemCount: number; lastStudiedAt: Date | null; createdAt: Date; updatedAt: Date; } @Injectable() export class KnowledgeBaseRepository { private items: Map = new Map(); async create(userId: string, dto: any): Promise { const kb: KnowledgeBase = { id: generateShortId(), userId, title: dto.title, description: dto.description || '', status: 'active', itemCount: 0, lastStudiedAt: null, createdAt: new Date(), updatedAt: new Date(), }; this.items.set(kb.id, kb); return kb; } async findById(id: string): Promise { return this.items.get(id); } async findAllByUserId(userId: string): Promise { return Array.from(this.items.values()).filter( (kb) => kb.userId === userId && kb.status !== 'deleted', ); } async countByUserId(userId: string): Promise { return Array.from(this.items.values()).filter( (kb) => kb.userId === userId && kb.status !== 'deleted', ).length; } async update(id: string, dto: any): Promise { const kb = this.items.get(id); if (!kb) return undefined; Object.assign(kb, { ...dto, updatedAt: new Date() }); this.items.set(id, kb); return kb; } async softDelete(id: string): Promise { const kb = this.items.get(id); if (!kb) return false; kb.status = 'deleted'; kb.updatedAt = new Date(); return true; } }