mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09: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[] },
|
files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] },
|
||||||
@Body() body: UpdateCardDto
|
@Body() body: UpdateCardDto
|
||||||
) {
|
) {
|
||||||
console.log({ body })
|
|
||||||
console.log(files)
|
|
||||||
return this.commandBus.execute(
|
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
|
answer: string
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Length(3, 500)
|
@Length(0, 0)
|
||||||
questionImg?: string
|
questionImg?: string
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Length(3, 500)
|
@Length(0, 0)
|
||||||
answerImg?: string
|
answerImg?: string
|
||||||
|
|
||||||
@IsOptional()
|
@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`)
|
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
|
let questionImg, answerImg
|
||||||
|
|
||||||
if (command.questionImg && command.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])
|
const result = await Promise.all([addQuestionImagePromise, addAnswerImagePromise])
|
||||||
questionImg = result[0].fileUrl
|
questionImg = result[0].fileUrl
|
||||||
answerImg = result[1].fileUrl
|
answerImg = result[1].fileUrl
|
||||||
} else if (command.answerImg) {
|
} else if (command.answerImg) {
|
||||||
|
const addAnswerImagePromise = this.fileUploadService.uploadFile(
|
||||||
|
command.answerImg?.buffer,
|
||||||
|
command.answerImg?.originalname
|
||||||
|
)
|
||||||
const result = await addAnswerImagePromise
|
const result = await addAnswerImagePromise
|
||||||
answerImg = result.fileUrl
|
answerImg = result.fileUrl
|
||||||
} else if (command.questionImg) {
|
} else if (command.questionImg) {
|
||||||
|
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||||
|
command.questionImg?.buffer,
|
||||||
|
command.questionImg?.originalname
|
||||||
|
)
|
||||||
const result = await addQuestionImagePromise
|
const result = await addQuestionImagePromise
|
||||||
questionImg = result.fileUrl
|
questionImg = result.fileUrl
|
||||||
}
|
}
|
||||||
|
if (command.card.questionImg === '') {
|
||||||
|
questionImg = null
|
||||||
|
}
|
||||||
|
if (command.card.answerImg === '') {
|
||||||
|
answerImg = null
|
||||||
|
}
|
||||||
return await this.cardsRepository.updateCardById(command.cardId, {
|
return await this.cardsRepository.updateCardById(command.cardId, {
|
||||||
...command.card,
|
...command.card,
|
||||||
answerImg,
|
answerImg,
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import {
|
|||||||
Query,
|
Query,
|
||||||
Req,
|
Req,
|
||||||
Request,
|
Request,
|
||||||
|
UploadedFiles,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
|
UseInterceptors,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { DecksService } from './decks.service'
|
import { DecksService } from './decks.service'
|
||||||
import { CreateDeckDto } from './dto/create-deck.dto'
|
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 { Pagination } from '../../infrastructure/common/pagination/pagination.service'
|
||||||
import { GetRandomCardInDeckCommand } from './use-cases/get-random-card-in-deck-use-case'
|
import { GetRandomCardInDeckCommand } from './use-cases/get-random-card-in-deck-use-case'
|
||||||
import { SaveGradeCommand } from './use-cases/save-grade-use-case'
|
import { SaveGradeCommand } from './use-cases/save-grade-use-case'
|
||||||
|
import { FileFieldsInterceptor } from '@nestjs/platform-express'
|
||||||
|
|
||||||
@Controller('decks')
|
@Controller('decks')
|
||||||
export class DecksController {
|
export class DecksController {
|
||||||
@@ -62,11 +65,13 @@ export class DecksController {
|
|||||||
const finalQuery = Pagination.getPaginationData(query)
|
const finalQuery = Pagination.getPaginationData(query)
|
||||||
return this.commandBus.execute(new GetAllCardsInDeckCommand(req.user.id, id, finalQuery))
|
return this.commandBus.execute(new GetAllCardsInDeckCommand(req.user.id, id, finalQuery))
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':id/learn')
|
@Get(':id/learn')
|
||||||
findRandomCardInDeck(@Param('id') id: string, @Req() req) {
|
findRandomCardInDeck(@Param('id') id: string, @Req() req) {
|
||||||
return this.commandBus.execute(new GetRandomCardInDeckCommand(req.user.id, id))
|
return this.commandBus.execute(new GetRandomCardInDeckCommand(req.user.id, id))
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post(':id/learn')
|
@Post(':id/learn')
|
||||||
saveGrade(@Param('id') id: string, @Req() req, @Body() body: any) {
|
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 })
|
new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade })
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@UseInterceptors(
|
||||||
|
FileFieldsInterceptor([
|
||||||
|
{ name: 'questionImg', maxCount: 1 },
|
||||||
|
{ name: 'answerImg', maxCount: 1 },
|
||||||
|
])
|
||||||
|
)
|
||||||
@Post(':id/cards')
|
@Post(':id/cards')
|
||||||
createCardInDeck(@Param('id') id: string, @Req() req, @Body() card: CreateCardDto) {
|
createCardInDeck(
|
||||||
return this.commandBus.execute(new CreateCardCommand(req.user.id, id, card))
|
@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)
|
@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 { GetRandomCardInDeckHandler } from './use-cases/get-random-card-in-deck-use-case'
|
||||||
import { GradesRepository } from './infrastructure/grades.repository'
|
import { GradesRepository } from './infrastructure/grades.repository'
|
||||||
import { SaveGradeHandler } from './use-cases/save-grade-use-case'
|
import { SaveGradeHandler } from './use-cases/save-grade-use-case'
|
||||||
|
import { FileUploadService } from '../../infrastructure/file-upload-service/file-upload.service'
|
||||||
|
|
||||||
const commandHandlers = [
|
const commandHandlers = [
|
||||||
CreateDeckHandler,
|
CreateDeckHandler,
|
||||||
@@ -32,7 +33,14 @@ const commandHandlers = [
|
|||||||
@Module({
|
@Module({
|
||||||
imports: [CqrsModule],
|
imports: [CqrsModule],
|
||||||
controllers: [DecksController],
|
controllers: [DecksController],
|
||||||
providers: [DecksService, DecksRepository, CardsRepository, GradesRepository, ...commandHandlers],
|
providers: [
|
||||||
|
DecksService,
|
||||||
|
DecksRepository,
|
||||||
|
CardsRepository,
|
||||||
|
GradesRepository,
|
||||||
|
FileUploadService,
|
||||||
|
...commandHandlers,
|
||||||
|
],
|
||||||
exports: [CqrsModule],
|
exports: [CqrsModule],
|
||||||
})
|
})
|
||||||
export class DecksModule {}
|
export class DecksModule {}
|
||||||
|
|||||||
@@ -1,20 +1,67 @@
|
|||||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||||
import { CreateCardDto } from '../../cards/dto/create-card.dto'
|
import { CreateCardDto } from '../../cards/dto/create-card.dto'
|
||||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||||
|
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
|
||||||
|
|
||||||
export class CreateCardCommand {
|
export class CreateCardCommand {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly userId: string,
|
public readonly userId: string,
|
||||||
public readonly deckId: 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)
|
@CommandHandler(CreateCardCommand)
|
||||||
export class CreateCardHandler implements ICommandHandler<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) {
|
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