69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
|
|
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<string, KnowledgeBase> = new Map();
|
||
|
|
|
||
|
|
async create(userId: string, dto: any): Promise<KnowledgeBase> {
|
||
|
|
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<KnowledgeBase | undefined> {
|
||
|
|
return this.items.get(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
async findAllByUserId(userId: string): Promise<KnowledgeBase[]> {
|
||
|
|
return Array.from(this.items.values()).filter(
|
||
|
|
(kb) => kb.userId === userId && kb.status !== 'deleted',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
async countByUserId(userId: string): Promise<number> {
|
||
|
|
return Array.from(this.items.values()).filter(
|
||
|
|
(kb) => kb.userId === userId && kb.status !== 'deleted',
|
||
|
|
).length;
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, dto: any): Promise<KnowledgeBase | undefined> {
|
||
|
|
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<boolean> {
|
||
|
|
const kb = this.items.get(id);
|
||
|
|
if (!kb) return false;
|
||
|
|
kb.status = 'deleted';
|
||
|
|
kb.updatedAt = new Date();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|