mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +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,
|
||||
|
||||
@@ -9,7 +9,9 @@ import {
|
||||
Query,
|
||||
Req,
|
||||
Request,
|
||||
UploadedFiles,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common'
|
||||
import { DecksService } from './decks.service'
|
||||
import { CreateDeckDto } from './dto/create-deck.dto'
|
||||
@@ -31,6 +33,7 @@ import { CreateCardDto } from '../cards/dto/create-card.dto'
|
||||
import { Pagination } from '../../infrastructure/common/pagination/pagination.service'
|
||||
import { GetRandomCardInDeckCommand } from './use-cases/get-random-card-in-deck-use-case'
|
||||
import { SaveGradeCommand } from './use-cases/save-grade-use-case'
|
||||
import { FileFieldsInterceptor } from '@nestjs/platform-express'
|
||||
|
||||
@Controller('decks')
|
||||
export class DecksController {
|
||||
@@ -62,11 +65,13 @@ export class DecksController {
|
||||
const finalQuery = Pagination.getPaginationData(query)
|
||||
return this.commandBus.execute(new GetAllCardsInDeckCommand(req.user.id, id, finalQuery))
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':id/learn')
|
||||
findRandomCardInDeck(@Param('id') id: string, @Req() req) {
|
||||
return this.commandBus.execute(new GetRandomCardInDeckCommand(req.user.id, id))
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post(':id/learn')
|
||||
saveGrade(@Param('id') id: string, @Req() req, @Body() body: any) {
|
||||
@@ -74,10 +79,25 @@ export class DecksController {
|
||||
new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade })
|
||||
)
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseInterceptors(
|
||||
FileFieldsInterceptor([
|
||||
{ name: 'questionImg', maxCount: 1 },
|
||||
{ name: 'answerImg', maxCount: 1 },
|
||||
])
|
||||
)
|
||||
@Post(':id/cards')
|
||||
createCardInDeck(@Param('id') id: string, @Req() req, @Body() card: CreateCardDto) {
|
||||
return this.commandBus.execute(new CreateCardCommand(req.user.id, id, card))
|
||||
createCardInDeck(
|
||||
@Param('id') id: string,
|
||||
@Req() req,
|
||||
@UploadedFiles()
|
||||
files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] },
|
||||
@Body() card: CreateCardDto
|
||||
) {
|
||||
return this.commandBus.execute(
|
||||
new CreateCardCommand(req.user.id, id, card, files.answerImg?.[0], files.questionImg?.[0])
|
||||
)
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
|
||||
@@ -16,6 +16,7 @@ import { CardsRepository } from '../cards/infrastructure/cards.repository'
|
||||
import { GetRandomCardInDeckHandler } from './use-cases/get-random-card-in-deck-use-case'
|
||||
import { GradesRepository } from './infrastructure/grades.repository'
|
||||
import { SaveGradeHandler } from './use-cases/save-grade-use-case'
|
||||
import { FileUploadService } from '../../infrastructure/file-upload-service/file-upload.service'
|
||||
|
||||
const commandHandlers = [
|
||||
CreateDeckHandler,
|
||||
@@ -32,7 +33,14 @@ const commandHandlers = [
|
||||
@Module({
|
||||
imports: [CqrsModule],
|
||||
controllers: [DecksController],
|
||||
providers: [DecksService, DecksRepository, CardsRepository, GradesRepository, ...commandHandlers],
|
||||
providers: [
|
||||
DecksService,
|
||||
DecksRepository,
|
||||
CardsRepository,
|
||||
GradesRepository,
|
||||
FileUploadService,
|
||||
...commandHandlers,
|
||||
],
|
||||
exports: [CqrsModule],
|
||||
})
|
||||
export class DecksModule {}
|
||||
|
||||
@@ -1,20 +1,67 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { CreateCardDto } from '../../cards/dto/create-card.dto'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
|
||||
|
||||
export class CreateCardCommand {
|
||||
constructor(
|
||||
public readonly userId: string,
|
||||
public readonly deckId: string,
|
||||
public readonly card: CreateCardDto
|
||||
public readonly card: CreateCardDto,
|
||||
public readonly answerImg?: Express.Multer.File,
|
||||
public readonly questionImg?: Express.Multer.File
|
||||
) {}
|
||||
}
|
||||
|
||||
@CommandHandler(CreateCardCommand)
|
||||
export class CreateCardHandler implements ICommandHandler<CreateCardCommand> {
|
||||
constructor(private readonly cardsRepository: CardsRepository) {}
|
||||
constructor(
|
||||
private readonly cardsRepository: CardsRepository,
|
||||
private readonly fileUploadService: FileUploadService
|
||||
) {}
|
||||
|
||||
async execute(command: CreateCardCommand) {
|
||||
return await this.cardsRepository.createCard(command.deckId, command.userId, command.card)
|
||||
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.createCard(command.deckId, command.userId, {
|
||||
...command.card,
|
||||
questionImg,
|
||||
answerImg,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user