add file upload to card create

This commit is contained in:
andres
2023-07-16 14:35:23 +02:00
parent cde1d9fd02
commit f2437db3b7
6 changed files with 107 additions and 20 deletions

View File

@@ -42,10 +42,8 @@ export class CardsController {
files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] },
@Body() body: UpdateCardDto
) {
console.log({ body })
console.log(files)
return this.commandBus.execute(
new UpdateCardCommand(id, body, req.user.id, files.answerImg[0], files.questionImg[0])
new UpdateCardCommand(id, body, req.user.id, files.answerImg?.[0], files.questionImg?.[0])
)
}

View File

@@ -8,11 +8,11 @@ export class CreateCardDto {
answer: string
@IsOptional()
@Length(3, 500)
@Length(0, 0)
questionImg?: string
@IsOptional()
@Length(3, 500)
@Length(0, 0)
answerImg?: string
@IsOptional()

View File

@@ -30,28 +30,42 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
throw new BadRequestException(`You can't change a card that you don't own`)
}
const addQuestionImagePromise = this.fileUploadService.uploadFile(
command.questionImg.buffer,
command.questionImg.originalname
)
const addAnswerImagePromise = this.fileUploadService.uploadFile(
command.answerImg.buffer,
command.answerImg.originalname
)
let questionImg, answerImg
if (command.questionImg && command.answerImg) {
const addQuestionImagePromise = this.fileUploadService.uploadFile(
command.questionImg?.buffer,
command.questionImg?.originalname
)
const addAnswerImagePromise = this.fileUploadService.uploadFile(
command.answerImg?.buffer,
command.answerImg?.originalname
)
const result = await Promise.all([addQuestionImagePromise, addAnswerImagePromise])
questionImg = result[0].fileUrl
answerImg = result[1].fileUrl
} else if (command.answerImg) {
const addAnswerImagePromise = this.fileUploadService.uploadFile(
command.answerImg?.buffer,
command.answerImg?.originalname
)
const result = await addAnswerImagePromise
answerImg = result.fileUrl
} else if (command.questionImg) {
const addQuestionImagePromise = this.fileUploadService.uploadFile(
command.questionImg?.buffer,
command.questionImg?.originalname
)
const result = await addQuestionImagePromise
questionImg = result.fileUrl
}
if (command.card.questionImg === '') {
questionImg = null
}
if (command.card.answerImg === '') {
answerImg = null
}
return await this.cardsRepository.updateCardById(command.cardId, {
...command.card,
answerImg,