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

@@ -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,