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) {}
@UseGuards(JwtAuthGuard)
@UseInterceptors(FileFieldsInterceptor([{ name: 'cover', maxCount: 1 }]))
@Post()
create(@Request() req, @Body() createDeckDto: CreateDeckDto) {
create(
@Request() req,
@UploadedFiles()
files: {
cover: Express.Multer.File[]
},
@Body() createDeckDto: CreateDeckDto
) {
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)
@@ -101,9 +111,20 @@ export class DecksController {
}
@UseGuards(JwtAuthGuard)
@UseInterceptors(FileFieldsInterceptor([{ name: 'cover', maxCount: 1 }]))
@Patch(':id')
update(@Param('id') id: string, @Body() updateDeckDto: UpdateDeckDto, @Req() req) {
return this.commandBus.execute(new UpdateDeckCommand(id, updateDeckDto, req.user.id))
update(
@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)

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 {
@Length(3, 30)
name: string
@IsOptional()
@IsString()
@Length(0, 0)
cover?: string
@IsOptional()
@IsBoolean()
@Transform((val: string) => [true, 'true', 1, '1'].indexOf(val) > -1)
isPrivate?: boolean
userId: string

View File

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

View File

@@ -1,16 +1,30 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { CreateDeckDto } from '../dto/create-deck.dto'
import { DecksRepository } from '../infrastructure/decks.repository'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
export class CreateDeckCommand {
constructor(public readonly deck: CreateDeckDto) {}
constructor(public readonly deck: CreateDeckDto, public readonly cover: Express.Multer.File) {}
}
@CommandHandler(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) {
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 { UpdateDeckDto } from '../dto/update-deck.dto'
import { BadRequestException, NotFoundException } from '@nestjs/common'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
export class UpdateDeckCommand {
constructor(
public readonly deckId: string,
public readonly deck: UpdateDeckDto,
public readonly userId: string
public readonly userId: string,
public readonly cover: Express.Multer.File
) {}
}
@CommandHandler(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) {
const deck = await this.deckRepository.findDeckById(command.deckId)
@@ -24,7 +29,17 @@ export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> {
if (deck.userId !== command.userId) {
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 })
}
}