mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 05:09:29 +00:00
add file upload to card create
This commit is contained in:
@@ -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])
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user