diff --git a/src/modules/cards/cards.controller.ts b/src/modules/cards/cards.controller.ts index 4f4261c..a68b132 100644 --- a/src/modules/cards/cards.controller.ts +++ b/src/modules/cards/cards.controller.ts @@ -3,6 +3,8 @@ import { Controller, Delete, Get, + HttpCode, + HttpStatus, Param, Patch, Req, @@ -12,12 +14,20 @@ import { } from '@nestjs/common' import { CommandBus } from '@nestjs/cqrs' 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 { CardsService } from './cards.service' import { UpdateCardDto } from './dto' +import { Card } from './entities/cards.entity' import { DeleteCardByIdCommand, GetDeckByIdCommand, UpdateCardCommand } from './use-cases' @ApiTags('Cards') @@ -26,11 +36,18 @@ export class CardsController { constructor(private readonly decksService: CardsService, private commandBus: CommandBus) {} @UseGuards(JwtAuthGuard) + @ApiOperation({ summary: 'Get card by id', description: 'Get card by id' }) + @ApiUnauthorizedResponse({ description: 'Unauthorized' }) + @ApiNotFoundResponse({ description: 'Card not found' }) @Get(':id') - findOne(@Param('id') id: string) { + findOne(@Param('id') id: string): Promise { 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) @UseInterceptors( FileFieldsInterceptor([ @@ -45,7 +62,7 @@ export class CardsController { @UploadedFiles() files: { questionImg: Express.Multer.File[]; answerImg: Express.Multer.File[] }, @Body() body: UpdateCardDto - ) { + ): Promise { return this.commandBus.execute( new UpdateCardCommand(id, body, req.user.id, files.answerImg?.[0], files.questionImg?.[0]) ) @@ -53,7 +70,12 @@ export class CardsController { @UseGuards(JwtAuthGuard) @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 { return this.commandBus.execute(new DeleteCardByIdCommand(id, req.user.id)) } } diff --git a/src/modules/cards/dto/update-card.dto.ts b/src/modules/cards/dto/update-card.dto.ts index 0348cbc..621bad7 100644 --- a/src/modules/cards/dto/update-card.dto.ts +++ b/src/modules/cards/dto/update-card.dto.ts @@ -1,5 +1,33 @@ import { PartialType } from '@nestjs/mapped-types' +import { ApiProperty } from '@nestjs/swagger' +import { IsOptional, Length } from 'class-validator' 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 +} diff --git a/src/modules/cards/use-cases/delete-card-by-id-use-case.ts b/src/modules/cards/use-cases/delete-card-by-id-use-case.ts index 7f6b8ab..96ca3c6 100644 --- a/src/modules/cards/use-cases/delete-card-by-id-use-case.ts +++ b/src/modules/cards/use-cases/delete-card-by-id-use-case.ts @@ -11,7 +11,7 @@ export class DeleteCardByIdCommand { export class DeleteCardByIdHandler implements ICommandHandler { constructor(private readonly cardsRepository: CardsRepository) {} - async execute(command: DeleteCardByIdCommand) { + async execute(command: DeleteCardByIdCommand): Promise { const card = await this.cardsRepository.findCardById(command.id) if (!card) throw new NotFoundException(`Card with id ${command.id} not found`) @@ -19,6 +19,6 @@ export class DeleteCardByIdHandler implements ICommandHandler { constructor(private readonly deckRepository: CardsRepository) {} - async execute(command: GetDeckByIdCommand) { + async execute(command: GetDeckByIdCommand): Promise { return await this.deckRepository.findCardById(command.id) } } diff --git a/src/modules/cards/use-cases/update-card-use-case.ts b/src/modules/cards/use-cases/update-card-use-case.ts index 0b782ed..a200715 100644 --- a/src/modules/cards/use-cases/update-card-use-case.ts +++ b/src/modules/cards/use-cases/update-card-use-case.ts @@ -3,6 +3,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service' import { UpdateCardDto } from '../dto' +import { Card } from '../entities/cards.entity' import { CardsRepository } from '../infrastructure/cards.repository' export class UpdateCardCommand { @@ -22,7 +23,7 @@ export class UpdateCardHandler implements ICommandHandler { private readonly fileUploadService: FileUploadService ) {} - async execute(command: UpdateCardCommand) { + async execute(command: UpdateCardCommand): Promise { const card = await this.cardsRepository.findCardById(command.cardId) if (!card) throw new NotFoundException(`Card with id ${command.cardId} not found`)