fix grades

This commit is contained in:
2023-08-07 16:00:31 +02:00
parent 8c5b09889a
commit 17b5c05685
6 changed files with 43 additions and 32 deletions

View File

@@ -126,7 +126,7 @@ model deck {
model grade { model grade {
id String @id @default(cuid()) id String @id @default(cuid())
deckId String deckId String
cardId String @unique cardId String
userId String userId String
grade Int grade Int
shots Int shots Int
@@ -136,6 +136,7 @@ model grade {
card card @relation(fields: [cardId], references: [id], onDelete: Cascade) card card @relation(fields: [cardId], references: [id], onDelete: Cascade)
deck deck @relation(fields: [deckId], references: [id], onDelete: Cascade) deck deck @relation(fields: [deckId], references: [id], onDelete: Cascade)
@@unique([cardId, userId])
@@index([userId]) @@index([userId])
@@index([deckId]) @@index([deckId])
@@index([cardId]) @@index([cardId])

View File

@@ -17,7 +17,7 @@ export class Card {
} }
export class PaginatedCards { export class PaginatedCards {
items: Card[] items: Omit<Card, 'userId' | 'rating'>[]
pagination: Pagination pagination: Pagination
} }

View File

@@ -111,7 +111,6 @@ export class CardsRepository {
grades: { grades: {
where: { where: {
userId, userId,
deckId,
}, },
}, },
}, },

View File

@@ -22,7 +22,10 @@ export class GradesRepository {
try { try {
return await this.prisma.grade.upsert({ return await this.prisma.grade.upsert({
where: { where: {
cardId_userId: {
cardId, cardId,
userId,
},
}, },
update: { update: {
grade, grade,
@@ -45,7 +48,6 @@ export class GradesRepository {
card: { card: {
connect: { connect: {
id: cardId, id: cardId,
shots: 1,
}, },
}, },
deck: { deck: {

View File

@@ -1,6 +1,6 @@
import { ForbiddenException, NotFoundException } from '@nestjs/common' import { ForbiddenException, NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { omit } from 'remeda' import { pick } from 'remeda'
import { GetAllCardsInDeckDto } from '../../cards/dto' import { GetAllCardsInDeckDto } from '../../cards/dto'
import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity' import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity'
@@ -24,19 +24,27 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
private transformGrade(cards: PaginatedCardsWithGrade): PaginatedCards { private transformGrade(cards: PaginatedCardsWithGrade): PaginatedCards {
return { return {
...cards, ...cards,
items: cards.items.map(card => { items: cards.items.map(card =>
if (card.grades.length === 0) return omit({ ...card, grade: 0 }, ['grades']) pick(
const grade = card.grades.reduce((acc, grade) => acc + grade.grade, 0) / card.grades.length
return omit(
{ {
...card, ...card,
grade, grade: card.grades[0]?.grade || 0,
}, },
['grades'] [
'id',
'question',
'answer',
'deckId',
'questionImg',
'answerImg',
'questionVideo',
'answerVideo',
'created',
'updated',
'shots',
]
) )
}), ),
} }
} }

View File

@@ -3,7 +3,6 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { Prisma } from '@prisma/client' import { Prisma } from '@prisma/client'
import { pick } from 'remeda' import { pick } from 'remeda'
import { Card } from '../../cards/entities/cards.entity'
import { CardsRepository } from '../../cards/infrastructure/cards.repository' import { CardsRepository } from '../../cards/infrastructure/cards.repository'
import { DecksRepository } from '../infrastructure/decks.repository' import { DecksRepository } from '../infrastructure/decks.repository'
@@ -24,7 +23,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
private readonly decksRepository: DecksRepository private readonly decksRepository: DecksRepository
) {} ) {}
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<Card> { private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<CardWithGrade> {
const selectionPool: Array<CardWithGrade> = [] const selectionPool: Array<CardWithGrade> = []
console.log(cards.length) console.log(cards.length)
@@ -49,7 +48,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
private async getNotDuplicateRandomCard( private async getNotDuplicateRandomCard(
cards: Array<CardWithGrade>, cards: Array<CardWithGrade>,
previousCardId: string previousCardId: string
): Promise<Card> { ): Promise<CardWithGrade> {
const randomCard = await this.getSmartRandomCard(cards) const randomCard = await this.getSmartRandomCard(cards)
if (randomCard.id === previousCardId && cards.length !== 1) { if (randomCard.id === previousCardId && cards.length !== 1) {
@@ -67,14 +66,14 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
if (deck.userId !== command.userId && deck.isPrivate) { if (deck.userId !== command.userId && deck.isPrivate) {
throw new ForbiddenException(`You can't get a private deck that you don't own`) throw new ForbiddenException(`You can't get a private deck that you don't own`)
} }
const cards = await this.cardsRepository.findCardsByDeckIdWithGrade( const cards = await this.cardsRepository.findCardsByDeckIdWithGrade(
command.userId, command.userId,
command.deckId command.deckId
) )
const smartRandomCard = await this.getNotDuplicateRandomCard(cards, command.previousCardId) const smartRandomCard = await this.getNotDuplicateRandomCard(cards, command.previousCardId)
return pick(smartRandomCard, [ return {
...pick(smartRandomCard, [
'id', 'id',
'question', 'question',
'answer', 'answer',
@@ -86,6 +85,8 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
'created', 'created',
'updated', 'updated',
'shots', 'shots',
]) ]),
grade: smartRandomCard.grades[0]?.grade || 0,
}
} }
} }