mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09:26 +00:00
fix grades
This commit is contained in:
@@ -126,7 +126,7 @@ model deck {
|
||||
model grade {
|
||||
id String @id @default(cuid())
|
||||
deckId String
|
||||
cardId String @unique
|
||||
cardId String
|
||||
userId String
|
||||
grade Int
|
||||
shots Int
|
||||
@@ -136,6 +136,7 @@ model grade {
|
||||
card card @relation(fields: [cardId], references: [id], onDelete: Cascade)
|
||||
deck deck @relation(fields: [deckId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([cardId, userId])
|
||||
@@index([userId])
|
||||
@@index([deckId])
|
||||
@@index([cardId])
|
||||
|
||||
@@ -17,7 +17,7 @@ export class Card {
|
||||
}
|
||||
|
||||
export class PaginatedCards {
|
||||
items: Card[]
|
||||
items: Omit<Card, 'userId' | 'rating'>[]
|
||||
pagination: Pagination
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,6 @@ export class CardsRepository {
|
||||
grades: {
|
||||
where: {
|
||||
userId,
|
||||
deckId,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -22,7 +22,10 @@ export class GradesRepository {
|
||||
try {
|
||||
return await this.prisma.grade.upsert({
|
||||
where: {
|
||||
cardId,
|
||||
cardId_userId: {
|
||||
cardId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
grade,
|
||||
@@ -45,7 +48,6 @@ export class GradesRepository {
|
||||
card: {
|
||||
connect: {
|
||||
id: cardId,
|
||||
shots: 1,
|
||||
},
|
||||
},
|
||||
deck: {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { omit } from 'remeda'
|
||||
import { pick } from 'remeda'
|
||||
|
||||
import { GetAllCardsInDeckDto } from '../../cards/dto'
|
||||
import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity'
|
||||
@@ -24,19 +24,27 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
|
||||
private transformGrade(cards: PaginatedCardsWithGrade): PaginatedCards {
|
||||
return {
|
||||
...cards,
|
||||
items: cards.items.map(card => {
|
||||
if (card.grades.length === 0) return omit({ ...card, grade: 0 }, ['grades'])
|
||||
|
||||
const grade = card.grades.reduce((acc, grade) => acc + grade.grade, 0) / card.grades.length
|
||||
|
||||
return omit(
|
||||
items: cards.items.map(card =>
|
||||
pick(
|
||||
{
|
||||
...card,
|
||||
grade,
|
||||
grade: card.grades[0]?.grade || 0,
|
||||
},
|
||||
['grades']
|
||||
[
|
||||
'id',
|
||||
'question',
|
||||
'answer',
|
||||
'deckId',
|
||||
'questionImg',
|
||||
'answerImg',
|
||||
'questionVideo',
|
||||
'answerVideo',
|
||||
'created',
|
||||
'updated',
|
||||
'shots',
|
||||
]
|
||||
)
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { pick } from 'remeda'
|
||||
|
||||
import { Card } from '../../cards/entities/cards.entity'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
import { DecksRepository } from '../infrastructure/decks.repository'
|
||||
|
||||
@@ -24,7 +23,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
||||
private readonly decksRepository: DecksRepository
|
||||
) {}
|
||||
|
||||
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<Card> {
|
||||
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<CardWithGrade> {
|
||||
const selectionPool: Array<CardWithGrade> = []
|
||||
|
||||
console.log(cards.length)
|
||||
@@ -49,7 +48,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
||||
private async getNotDuplicateRandomCard(
|
||||
cards: Array<CardWithGrade>,
|
||||
previousCardId: string
|
||||
): Promise<Card> {
|
||||
): Promise<CardWithGrade> {
|
||||
const randomCard = await this.getSmartRandomCard(cards)
|
||||
|
||||
if (randomCard.id === previousCardId && cards.length !== 1) {
|
||||
@@ -67,25 +66,27 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
||||
if (deck.userId !== command.userId && deck.isPrivate) {
|
||||
throw new ForbiddenException(`You can't get a private deck that you don't own`)
|
||||
}
|
||||
|
||||
const cards = await this.cardsRepository.findCardsByDeckIdWithGrade(
|
||||
command.userId,
|
||||
command.deckId
|
||||
)
|
||||
const smartRandomCard = await this.getNotDuplicateRandomCard(cards, command.previousCardId)
|
||||
|
||||
return pick(smartRandomCard, [
|
||||
'id',
|
||||
'question',
|
||||
'answer',
|
||||
'deckId',
|
||||
'questionImg',
|
||||
'answerImg',
|
||||
'questionVideo',
|
||||
'answerVideo',
|
||||
'created',
|
||||
'updated',
|
||||
'shots',
|
||||
])
|
||||
return {
|
||||
...pick(smartRandomCard, [
|
||||
'id',
|
||||
'question',
|
||||
'answer',
|
||||
'deckId',
|
||||
'questionImg',
|
||||
'answerImg',
|
||||
'questionVideo',
|
||||
'answerVideo',
|
||||
'created',
|
||||
'updated',
|
||||
'shots',
|
||||
]),
|
||||
grade: smartRandomCard.grades[0]?.grade || 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user