mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09:26 +00:00
fix card images upload
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
@@ -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`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user