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 LearningSessionRepository {
|
2026-05-17 00:39:46 +08:00
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
2026-05-09 18:25:04 +08:00
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async create(userId: string, dto: {
|
|
|
|
|
knowledgeItemId?: string;
|
|
|
|
|
knowledgeBaseId?: string;
|
|
|
|
|
mode?: string;
|
|
|
|
|
}) {
|
|
|
|
|
return this.prisma.learningSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId,
|
|
|
|
|
knowledgeItemId: dto.knowledgeItemId ?? null,
|
|
|
|
|
knowledgeBaseId: dto.knowledgeBaseId ?? null,
|
|
|
|
|
mode: dto.mode ?? 'reading',
|
|
|
|
|
status: 'active',
|
|
|
|
|
startedAt: new Date(),
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async end(id: string) {
|
|
|
|
|
const session = await this.prisma.learningSession.findUnique({ where: { id } });
|
2026-05-09 18:25:04 +08:00
|
|
|
if (!session) return undefined;
|
2026-05-17 00:39:46 +08:00
|
|
|
|
|
|
|
|
return this.prisma.learningSession.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: {
|
|
|
|
|
status: 'completed',
|
|
|
|
|
endedAt: new Date(),
|
|
|
|
|
durationSeconds: Math.floor(
|
|
|
|
|
(Date.now() - session.startedAt.getTime()) / 1000,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 19:08:07 +08:00
|
|
|
async findByUserId(userId: string, pagination?: { page?: number; limit?: number }) {
|
|
|
|
|
const page = pagination?.page ?? 1;
|
|
|
|
|
const limit = pagination?.limit ?? 20;
|
2026-05-17 00:39:46 +08:00
|
|
|
return this.prisma.learningSession.findMany({
|
|
|
|
|
where: { userId },
|
|
|
|
|
orderBy: { startedAt: 'desc' },
|
2026-05-17 19:08:07 +08:00
|
|
|
skip: (page - 1) * limit,
|
|
|
|
|
take: limit,
|
2026-05-17 00:39:46 +08:00
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
}
|