mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
fix infinite loop
This commit is contained in:
@@ -193,13 +193,20 @@ export class DecksController {
|
|||||||
description: 'Save the grade of a card',
|
description: 'Save the grade of a card',
|
||||||
summary: 'Save the grade of a card',
|
summary: 'Save the grade of a card',
|
||||||
})
|
})
|
||||||
async saveGrade(@Param('id') id: string, @Req() req, @Body() body: SaveGradeDto) {
|
async saveGrade(@Param('id') deckId: string, @Req() req, @Body() body: SaveGradeDto) {
|
||||||
const saved = await this.commandBus.execute(
|
const promises = [
|
||||||
|
this.commandBus.execute(new GetRandomCardInDeckCommand(req.user.id, deckId, body.cardId)),
|
||||||
|
this.commandBus.execute(
|
||||||
new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade })
|
new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade })
|
||||||
)
|
),
|
||||||
|
]
|
||||||
|
|
||||||
return await this.commandBus.execute(
|
try {
|
||||||
new GetRandomCardInDeckCommand(req.user.id, saved.deckId, saved.id)
|
const [card] = await Promise.all(promises)
|
||||||
)
|
|
||||||
|
return card
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
|||||||
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<Card> {
|
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<Card> {
|
||||||
const selectionPool: Array<CardWithGrade> = []
|
const selectionPool: Array<CardWithGrade> = []
|
||||||
|
|
||||||
|
console.log(cards.length)
|
||||||
cards.forEach(card => {
|
cards.forEach(card => {
|
||||||
// Calculate the average grade for the card
|
// Calculate the average grade for the card
|
||||||
const averageGrade =
|
const averageGrade =
|
||||||
@@ -51,7 +52,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
|||||||
): Promise<Card> {
|
): Promise<Card> {
|
||||||
const randomCard = await this.getSmartRandomCard(cards)
|
const randomCard = await this.getSmartRandomCard(cards)
|
||||||
|
|
||||||
if (randomCard.id === previousCardId) {
|
if (randomCard.id === previousCardId && cards.length !== 1) {
|
||||||
return this.getNotDuplicateRandomCard(cards, previousCardId)
|
return this.getNotDuplicateRandomCard(cards, previousCardId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user