mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
fix card images upload
This commit is contained in:
@@ -155,9 +155,9 @@ export class DecksController {
|
||||
createCardInDeck(
|
||||
@Param('id') id: string,
|
||||
@Req() req,
|
||||
@UploadedFiles()
|
||||
@Body()
|
||||
card: CreateCardDto,
|
||||
@UploadedFiles()
|
||||
files?: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] }
|
||||
): Promise<Card> {
|
||||
return this.commandBus.execute(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ForbiddenException } from '@nestjs/common'
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
|
||||
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
|
||||
@@ -30,6 +30,9 @@ export class CreateCardHandler implements ICommandHandler<CreateCardCommand> {
|
||||
|
||||
const deck = await this.decksRepository.findDeckById(command.deckId)
|
||||
|
||||
if (!deck) {
|
||||
throw new NotFoundException(`Deck with id ${command.deckId} not found`)
|
||||
}
|
||||
if (deck.userId !== command.userId) {
|
||||
throw new ForbiddenException(`You can't create cards in a deck that you don't own`)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common'
|
||||
import { ForbiddenException, Logger, NotFoundException } from '@nestjs/common'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { pick } from 'remeda'
|
||||
@@ -18,6 +18,7 @@ type CardWithGrade = Prisma.cardGetPayload<{ include: { grades: true } }>
|
||||
|
||||
@CommandHandler(GetRandomCardInDeckCommand)
|
||||
export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCardInDeckCommand> {
|
||||
logger = new Logger(GetRandomCardInDeckHandler.name)
|
||||
constructor(
|
||||
private readonly cardsRepository: CardsRepository,
|
||||
private readonly decksRepository: DecksRepository
|
||||
@@ -50,6 +51,14 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
|
||||
): Promise<CardWithGrade> {
|
||||
const randomCard = await this.getSmartRandomCard(cards)
|
||||
|
||||
if (!randomCard) {
|
||||
this.logger.error(`No cards found in deck with id ${randomCard.deckId}`, {
|
||||
previousCardId,
|
||||
randomCard,
|
||||
cards,
|
||||
})
|
||||
throw new NotFoundException(`No cards found in deck with id ${randomCard.deckId}`)
|
||||
}
|
||||
if (randomCard.id === previousCardId && cards.length !== 1) {
|
||||
return this.getNotDuplicateRandomCard(cards, previousCardId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user