mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 12:33:17 +00:00
61 lines
1.2 KiB
TypeScript
61 lines
1.2 KiB
TypeScript
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
|
|
|
import { PrismaService } from '../../../prisma.service'
|
|
|
|
@Injectable()
|
|
export class GradesRepository {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
private readonly logger = new Logger(GradesRepository.name)
|
|
|
|
async createGrade({
|
|
cardId,
|
|
userId,
|
|
deckId,
|
|
grade,
|
|
}: {
|
|
cardId: string
|
|
userId: string
|
|
deckId: string
|
|
grade: number
|
|
}) {
|
|
try {
|
|
return await this.prisma.grade.upsert({
|
|
where: {
|
|
userId,
|
|
cardId,
|
|
deckId,
|
|
},
|
|
update: {
|
|
grade,
|
|
shots: {
|
|
increment: 1,
|
|
},
|
|
},
|
|
create: {
|
|
grade,
|
|
shots: 1,
|
|
user: {
|
|
connect: {
|
|
id: userId,
|
|
},
|
|
},
|
|
card: {
|
|
connect: {
|
|
id: cardId,
|
|
},
|
|
},
|
|
deck: {
|
|
connect: {
|
|
id: deckId,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
} catch (e) {
|
|
this.logger.error(e?.message)
|
|
throw new InternalServerErrorException(e?.message)
|
|
}
|
|
}
|
|
}
|