From 6cd362c90a7b4c5d6b0e5e4a19e7accbea57e678 Mon Sep 17 00:00:00 2001 From: do-huni Date: Fri, 12 Jul 2024 16:09:21 +0900 Subject: [PATCH] feat(comments): add find own comments api --- .github/workflows/deploy-to-master.yml | 0 src/APIs/comments/comments.controller.ts | 28 +++++++++++++++---- src/APIs/comments/comments.repository.ts | 2 +- src/APIs/comments/comments.service.ts | 13 ++++++++- .../interfaces/comments.service.interface.ts | 4 +++ 5 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/deploy-to-master.yml diff --git a/.github/workflows/deploy-to-master.yml b/.github/workflows/deploy-to-master.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/APIs/comments/comments.controller.ts b/src/APIs/comments/comments.controller.ts index 4b58f7f..fb16f94 100644 --- a/src/APIs/comments/comments.controller.ts +++ b/src/APIs/comments/comments.controller.ts @@ -26,18 +26,18 @@ import { CommentsGetResponseDto } from './dtos/response/comments-get-response.dt import { CommentDto } from './dtos/common/comment.dto'; import { CommentPatchRequestDto } from './dtos/request/comment-patch-request.dto'; -@ApiTags('게시글 API') -@Controller('articles/:articleId/comments') +@Controller() export class CommentsController { constructor(private readonly svc_comments: CommentsService) {} + @ApiTags('게시글 API') @ApiOperation({ summary: '댓글을 작성한다.', description: '댓글을 작성한다.', }) @ApiOkResponse({ type: CommentChildrenDto }) @ApiCookieAuth() - @Post() + @Post('articles/:articleId/comments') @UseGuards(AuthGuardV2) @HttpCode(200) async createComment( @@ -53,22 +53,37 @@ export class CommentsController { }); } + @ApiTags('게시글 API') @ApiOperation({ summary: '특정 게시글에 대한 댓글 조회', }) @ApiOkResponse({ type: [CommentsGetResponseDto] }) - @Get() + @Get('articles/:articleId/comments') async fetchComments( @Param('articleId') articleId: number, ): Promise { return await this.svc_comments.fetchComments({ articleId }); } + @ApiTags('유저 API') + @ApiOperation({ + summary: '자신의 최근 댓글 10개 조회', + }) + @UseGuards(AuthGuardV2) + @ApiCookieAuth() + @ApiOkResponse({ type: [CommentDto] }) + @Get('users/me/comments') + async fetchUserComments(@Req() req: Request): Promise { + const userId = req.user.userId; + return await this.svc_comments.fetchUserComments({ userId }); + } + + @ApiTags('게시글 API') @ApiOperation({ summary: '특정 게시글에 대한 댓글 수정' }) @ApiCookieAuth() @ApiOkResponse({ type: CommentDto }) @UseGuards(AuthGuardV2) - @Patch(':commentId') + @Patch('articles/:articleId/comments/:commentId') async patchComment( @Req() req: Request, @Param('articleId') articleId: number, @@ -84,13 +99,14 @@ export class CommentsController { }); } + @ApiTags('게시글 API') @ApiOperation({ summary: '댓글을 삭제한다.', description: '댓글을 논리삭제한다. date_deleted 칼럼에 값이 생긴다.', }) @ApiCookieAuth() @ApiNoContentResponse({ description: '삭제 성공' }) - @Delete(':commentId') + @Delete('articles/:articleId/comments/:commentId') @UseGuards(AuthGuardV2) @HttpCode(204) async deleteComment( diff --git a/src/APIs/comments/comments.repository.ts b/src/APIs/comments/comments.repository.ts index c210c7a..c2f3650 100644 --- a/src/APIs/comments/comments.repository.ts +++ b/src/APIs/comments/comments.repository.ts @@ -42,7 +42,7 @@ export class CommentsRepository extends Repository { .getOne(); } - async fetchComments({ + async fetchAll({ articleId, }: ICommentsRepositoryfetchComments): Promise { let comments = await this.createQueryBuilder('c') diff --git a/src/APIs/comments/comments.service.ts b/src/APIs/comments/comments.service.ts index 9dce56e..fc2e007 100644 --- a/src/APIs/comments/comments.service.ts +++ b/src/APIs/comments/comments.service.ts @@ -11,6 +11,7 @@ import { ICommentsServiceCreateComment, ICommentsServiceDeleteComment, ICommentsServiceFindComments, + ICommentsServiceFindUserComments, ICommentsServiceId, ICommentsServicePatchComment, } from './interfaces/comments.service.interface'; @@ -125,7 +126,17 @@ export class CommentsService { async fetchComments({ articleId, }: ICommentsServiceFindComments): Promise { - return await this.repo_comments.fetchComments({ articleId }); + return await this.repo_comments.fetchAll({ articleId }); + } + + async fetchUserComments({ + userId, + }: ICommentsServiceFindUserComments): Promise { + return await this.repo_comments.find({ + where: { userId }, + order: { dateCreated: 'DESC' }, + take: 10, + }); } async delete({ diff --git a/src/APIs/comments/interfaces/comments.service.interface.ts b/src/APIs/comments/interfaces/comments.service.interface.ts index e401231..77fd85e 100644 --- a/src/APIs/comments/interfaces/comments.service.interface.ts +++ b/src/APIs/comments/interfaces/comments.service.interface.ts @@ -20,6 +20,10 @@ export interface ICommentsServiceFindComments { articleId: number; } +export interface ICommentsServiceFindUserComments { + userId: number; +} + export interface ICommentsServiceDeleteComment { commentId: number; userId: number;