mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 20:59:28 +00:00
fix learn endpoint
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { omit } from 'remeda'
|
||||
|
||||
import { GetAllCardsInDeckDto } from '../../cards/dto'
|
||||
import { PaginatedCards } from '../../cards/entities/cards.entity'
|
||||
import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
import { DecksRepository } from '../infrastructure/decks.repository'
|
||||
|
||||
@@ -20,6 +21,24 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
|
||||
private readonly cardsRepository: CardsRepository,
|
||||
private readonly decksRepository: DecksRepository
|
||||
) {}
|
||||
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(
|
||||
{
|
||||
...card,
|
||||
grade,
|
||||
},
|
||||
['grades']
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async execute(command: GetAllCardsInDeckCommand): Promise<PaginatedCards> {
|
||||
const deck = await this.decksRepository.findDeckById(command.deckId)
|
||||
@@ -29,7 +48,12 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
|
||||
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.findCardsByDeckId(
|
||||
command.deckId,
|
||||
command.userId,
|
||||
command.params
|
||||
)
|
||||
|
||||
return await this.cardsRepository.findCardsByDeckId(command.deckId, command.params)
|
||||
return this.transformGrade(cards)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user