add cover to deck create/update

This commit is contained in:
andres
2023-07-16 14:59:03 +02:00
parent f2437db3b7
commit 2f1579ed48
5 changed files with 65 additions and 12 deletions

View File

@@ -40,10 +40,20 @@ export class DecksController {
constructor(private readonly decksService: DecksService, private commandBus: CommandBus) {} constructor(private readonly decksService: DecksService, private commandBus: CommandBus) {}
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@UseInterceptors(FileFieldsInterceptor([{ name: 'cover', maxCount: 1 }]))
@Post() @Post()
create(@Request() req, @Body() createDeckDto: CreateDeckDto) { create(
@Request() req,
@UploadedFiles()
files: {
cover: Express.Multer.File[]
},
@Body() createDeckDto: CreateDeckDto
) {
const userId = req.user.id const userId = req.user.id
return this.commandBus.execute(new CreateDeckCommand({ ...createDeckDto, userId: userId })) return this.commandBus.execute(
new CreateDeckCommand({ ...createDeckDto, userId: userId }, files?.cover?.[0])
)
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@@ -101,9 +111,20 @@ export class DecksController {
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@UseInterceptors(FileFieldsInterceptor([{ name: 'cover', maxCount: 1 }]))
@Patch(':id') @Patch(':id')
update(@Param('id') id: string, @Body() updateDeckDto: UpdateDeckDto, @Req() req) { update(
return this.commandBus.execute(new UpdateDeckCommand(id, updateDeckDto, req.user.id)) @Param('id') id: string,
@UploadedFiles()
files: {
cover: Express.Multer.File[]
},
@Body() updateDeckDto: UpdateDeckDto,
@Req() req
) {
return this.commandBus.execute(
new UpdateDeckCommand(id, updateDeckDto, req.user.id, files?.cover?.[0])
)
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)

View File

@@ -1,15 +1,17 @@
import { IsBoolean, IsOptional, IsString, Length } from 'class-validator' import { IsBoolean, IsOptional, Length } from 'class-validator'
import { Transform } from 'class-transformer'
export class CreateDeckDto { export class CreateDeckDto {
@Length(3, 30) @Length(3, 30)
name: string name: string
@IsOptional() @IsOptional()
@IsString() @Length(0, 0)
cover?: string cover?: string
@IsOptional() @IsOptional()
@IsBoolean() @IsBoolean()
@Transform((val: string) => [true, 'true', 1, '1'].indexOf(val) > -1)
isPrivate?: boolean isPrivate?: boolean
userId: string userId: string

View File

@@ -125,6 +125,7 @@ export class DecksRepository {
throw new InternalServerErrorException(e?.message) throw new InternalServerErrorException(e?.message)
} }
} }
public async findDeckByCardId(cardId: string) { public async findDeckByCardId(cardId: string) {
try { try {
const card = await this.prisma.card.findUnique({ const card = await this.prisma.card.findUnique({

View File

@@ -1,16 +1,30 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { CreateDeckDto } from '../dto/create-deck.dto' import { CreateDeckDto } from '../dto/create-deck.dto'
import { DecksRepository } from '../infrastructure/decks.repository' import { DecksRepository } from '../infrastructure/decks.repository'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
export class CreateDeckCommand { export class CreateDeckCommand {
constructor(public readonly deck: CreateDeckDto) {} constructor(public readonly deck: CreateDeckDto, public readonly cover: Express.Multer.File) {}
} }
@CommandHandler(CreateDeckCommand) @CommandHandler(CreateDeckCommand)
export class CreateDeckHandler implements ICommandHandler<CreateDeckCommand> { export class CreateDeckHandler implements ICommandHandler<CreateDeckCommand> {
constructor(private readonly deckRepository: DecksRepository) {} constructor(
private readonly deckRepository: DecksRepository,
private readonly fileUploadService: FileUploadService
) {}
async execute(command: CreateDeckCommand) { async execute(command: CreateDeckCommand) {
return await this.deckRepository.createDeck(command.deck) let cover
if (command.cover) {
const result = await this.fileUploadService.uploadFile(
command.cover.buffer,
command.cover.originalname
)
cover = result.fileUrl
}
return await this.deckRepository.createDeck({ ...command.deck, cover })
} }
} }

View File

@@ -2,18 +2,23 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { DecksRepository } from '../infrastructure/decks.repository' import { DecksRepository } from '../infrastructure/decks.repository'
import { UpdateDeckDto } from '../dto/update-deck.dto' import { UpdateDeckDto } from '../dto/update-deck.dto'
import { BadRequestException, NotFoundException } from '@nestjs/common' import { BadRequestException, NotFoundException } from '@nestjs/common'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
export class UpdateDeckCommand { export class UpdateDeckCommand {
constructor( constructor(
public readonly deckId: string, public readonly deckId: string,
public readonly deck: UpdateDeckDto, public readonly deck: UpdateDeckDto,
public readonly userId: string public readonly userId: string,
public readonly cover: Express.Multer.File
) {} ) {}
} }
@CommandHandler(UpdateDeckCommand) @CommandHandler(UpdateDeckCommand)
export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> { export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> {
constructor(private readonly deckRepository: DecksRepository) {} constructor(
private readonly deckRepository: DecksRepository,
private readonly fileUploadService: FileUploadService
) {}
async execute(command: UpdateDeckCommand) { async execute(command: UpdateDeckCommand) {
const deck = await this.deckRepository.findDeckById(command.deckId) const deck = await this.deckRepository.findDeckById(command.deckId)
@@ -24,7 +29,17 @@ export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> {
if (deck.userId !== command.userId) { if (deck.userId !== command.userId) {
throw new BadRequestException(`You can't modify a deck that you don't own`) throw new BadRequestException(`You can't modify a deck that you don't own`)
} }
let cover
return await this.deckRepository.updateDeckById(command.deckId, command.deck) if (command.cover) {
const result = await this.fileUploadService.uploadFile(
command.cover.buffer,
command.cover.originalname
)
cover = result.fileUrl
} else if (command.deck.cover === '') {
cover = null
}
return await this.deckRepository.updateDeckById(command.deckId, { ...command.deck, cover })
} }
} }