add cards documentation

This commit is contained in:
2023-07-16 23:34:35 +02:00
parent 9c13a57804
commit 111ca55cbf
5 changed files with 61 additions and 9 deletions

View File

@@ -3,6 +3,8 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpCode,
HttpStatus,
Param, Param,
Patch, Patch,
Req, Req,
@@ -12,12 +14,20 @@ import {
} from '@nestjs/common' } from '@nestjs/common'
import { CommandBus } from '@nestjs/cqrs' import { CommandBus } from '@nestjs/cqrs'
import { FileFieldsInterceptor } from '@nestjs/platform-express' import { FileFieldsInterceptor } from '@nestjs/platform-express'
import { ApiTags } from '@nestjs/swagger' import {
ApiConsumes,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger'
import { JwtAuthGuard } from '../auth/guards' import { JwtAuthGuard } from '../auth/guards'
import { CardsService } from './cards.service' import { CardsService } from './cards.service'
import { UpdateCardDto } from './dto' import { UpdateCardDto } from './dto'
import { Card } from './entities/cards.entity'
import { DeleteCardByIdCommand, GetDeckByIdCommand, UpdateCardCommand } from './use-cases' import { DeleteCardByIdCommand, GetDeckByIdCommand, UpdateCardCommand } from './use-cases'
@ApiTags('Cards') @ApiTags('Cards')
@@ -26,11 +36,18 @@ export class CardsController {
constructor(private readonly decksService: CardsService, private commandBus: CommandBus) {} constructor(private readonly decksService: CardsService, private commandBus: CommandBus) {}
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ApiOperation({ summary: 'Get card by id', description: 'Get card by id' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiNotFoundResponse({ description: 'Card not found' })
@Get(':id') @Get(':id')
findOne(@Param('id') id: string) { findOne(@Param('id') id: string): Promise<Card> {
return this.commandBus.execute(new GetDeckByIdCommand(id)) return this.commandBus.execute(new GetDeckByIdCommand(id))
} }
@ApiConsumes('multipart/form-data')
@ApiOperation({ summary: 'Get card by id', description: 'Get card by id' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiNotFoundResponse({ description: 'Card not found' })
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@UseInterceptors( @UseInterceptors(
FileFieldsInterceptor([ FileFieldsInterceptor([
@@ -45,7 +62,7 @@ export class CardsController {
@UploadedFiles() @UploadedFiles()
files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] }, files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] },
@Body() body: UpdateCardDto @Body() body: UpdateCardDto
) { ): Promise<Card> {
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])
) )
@@ -53,7 +70,12 @@ export class CardsController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Delete(':id') @Delete(':id')
remove(@Param('id') id: string, @Req() req) { @ApiOperation({ summary: 'Delete card by id', description: 'Delete card by id' })
@ApiNoContentResponse({ description: 'New tokens generated successfully' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiNotFoundResponse({ description: 'Card not found' })
@HttpCode(HttpStatus.NO_CONTENT)
remove(@Param('id') id: string, @Req() req): Promise<void> {
return this.commandBus.execute(new DeleteCardByIdCommand(id, req.user.id)) return this.commandBus.execute(new DeleteCardByIdCommand(id, req.user.id))
} }
} }

View File

@@ -1,5 +1,33 @@
import { PartialType } from '@nestjs/mapped-types' import { PartialType } from '@nestjs/mapped-types'
import { ApiProperty } from '@nestjs/swagger'
import { IsOptional, Length } from 'class-validator'
import { CreateCardDto } from './create-card.dto' import { CreateCardDto } from './create-card.dto'
export class UpdateCardDto extends PartialType(CreateCardDto) {} export class UpdateCardDto extends PartialType(CreateCardDto) {
@IsOptional()
@Length(3, 500)
question?: string
@IsOptional()
@Length(3, 500)
answer?: string
@IsOptional()
@Length(0, 0)
@ApiProperty({ type: 'string', format: 'binary' })
questionImg?: string
@IsOptional()
@Length(0, 0)
@ApiProperty({ type: 'string', format: 'binary' })
answerImg?: string
@IsOptional()
@Length(3, 500)
questionVideo?: string
@IsOptional()
@Length(3, 500)
answerVideo?: string
}

View File

@@ -11,7 +11,7 @@ export class DeleteCardByIdCommand {
export class DeleteCardByIdHandler implements ICommandHandler<DeleteCardByIdCommand> { export class DeleteCardByIdHandler implements ICommandHandler<DeleteCardByIdCommand> {
constructor(private readonly cardsRepository: CardsRepository) {} constructor(private readonly cardsRepository: CardsRepository) {}
async execute(command: DeleteCardByIdCommand) { async execute(command: DeleteCardByIdCommand): Promise<void> {
const card = await this.cardsRepository.findCardById(command.id) const card = await this.cardsRepository.findCardById(command.id)
if (!card) throw new NotFoundException(`Card with id ${command.id} not found`) if (!card) throw new NotFoundException(`Card with id ${command.id} not found`)
@@ -19,6 +19,6 @@ export class DeleteCardByIdHandler implements ICommandHandler<DeleteCardByIdComm
throw new BadRequestException(`You can't delete a card that you don't own`) throw new BadRequestException(`You can't delete a card that you don't own`)
} }
return await this.cardsRepository.deleteCardById(command.id) await this.cardsRepository.deleteCardById(command.id)
} }
} }

View File

@@ -1,5 +1,6 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { Card } from '../entities/cards.entity'
import { CardsRepository } from '../infrastructure/cards.repository' import { CardsRepository } from '../infrastructure/cards.repository'
export class GetDeckByIdCommand { export class GetDeckByIdCommand {
@@ -10,7 +11,7 @@ export class GetDeckByIdCommand {
export class GetDeckByIdHandler implements ICommandHandler<GetDeckByIdCommand> { export class GetDeckByIdHandler implements ICommandHandler<GetDeckByIdCommand> {
constructor(private readonly deckRepository: CardsRepository) {} constructor(private readonly deckRepository: CardsRepository) {}
async execute(command: GetDeckByIdCommand) { async execute(command: GetDeckByIdCommand): Promise<Card> {
return await this.deckRepository.findCardById(command.id) return await this.deckRepository.findCardById(command.id)
} }
} }

View File

@@ -3,6 +3,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service' import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
import { UpdateCardDto } from '../dto' import { UpdateCardDto } from '../dto'
import { Card } from '../entities/cards.entity'
import { CardsRepository } from '../infrastructure/cards.repository' import { CardsRepository } from '../infrastructure/cards.repository'
export class UpdateCardCommand { export class UpdateCardCommand {
@@ -22,7 +23,7 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
private readonly fileUploadService: FileUploadService private readonly fileUploadService: FileUploadService
) {} ) {}
async execute(command: UpdateCardCommand) { async execute(command: UpdateCardCommand): Promise<Card> {
const card = await this.cardsRepository.findCardById(command.cardId) const card = await this.cardsRepository.findCardById(command.cardId)
if (!card) throw new NotFoundException(`Card with id ${command.cardId} not found`) if (!card) throw new NotFoundException(`Card with id ${command.cardId} not found`)