fix card images upload

This commit is contained in:
2023-08-12 15:40:47 +02:00
parent 2626bf3815
commit 43a2f11f80
3 changed files with 15 additions and 3 deletions

View File

@@ -155,9 +155,9 @@ export class DecksController {
createCardInDeck( createCardInDeck(
@Param('id') id: string, @Param('id') id: string,
@Req() req, @Req() req,
@UploadedFiles()
@Body() @Body()
card: CreateCardDto, card: CreateCardDto,
@UploadedFiles()
files?: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] } files?: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] }
): Promise<Card> { ): Promise<Card> {
return this.commandBus.execute( return this.commandBus.execute(

View File

@@ -1,4 +1,4 @@
import { ForbiddenException } from '@nestjs/common' import { ForbiddenException, NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service' 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) 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) { if (deck.userId !== command.userId) {
throw new ForbiddenException(`You can't create cards in a deck that you don't own`) throw new ForbiddenException(`You can't create cards in a deck that you don't own`)
} }

View File

@@ -1,4 +1,4 @@
import { ForbiddenException, NotFoundException } from '@nestjs/common' import { ForbiddenException, Logger, NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { Prisma } from '@prisma/client' import { Prisma } from '@prisma/client'
import { pick } from 'remeda' import { pick } from 'remeda'
@@ -18,6 +18,7 @@ type CardWithGrade = Prisma.cardGetPayload<{ include: { grades: true } }>
@CommandHandler(GetRandomCardInDeckCommand) @CommandHandler(GetRandomCardInDeckCommand)
export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCardInDeckCommand> { export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCardInDeckCommand> {
logger = new Logger(GetRandomCardInDeckHandler.name)
constructor( constructor(
private readonly cardsRepository: CardsRepository, private readonly cardsRepository: CardsRepository,
private readonly decksRepository: DecksRepository private readonly decksRepository: DecksRepository
@@ -50,6 +51,14 @@ export class GetRandomCardInDeckHandler implements ICommandHandler<GetRandomCard
): Promise<CardWithGrade> { ): Promise<CardWithGrade> {
const randomCard = await this.getSmartRandomCard(cards) 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) { if (randomCard.id === previousCardId && cards.length !== 1) {
return this.getNotDuplicateRandomCard(cards, previousCardId) return this.getNotDuplicateRandomCard(cards, previousCardId)
} }