Skip to content

Commit

Permalink
feat(comments): add find own comments api
Browse files Browse the repository at this point in the history
  • Loading branch information
astorverse committed Jul 12, 2024
1 parent 3b2248f commit 6cd362c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
Empty file.
28 changes: 22 additions & 6 deletions src/APIs/comments/comments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<CommentsGetResponseDto[]> {
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<CommentDto[]> {
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,
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/APIs/comments/comments.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class CommentsRepository extends Repository<Comment> {
.getOne();
}

async fetchComments({
async fetchAll({
articleId,
}: ICommentsRepositoryfetchComments): Promise<CommentsGetResponseDto[]> {
let comments = await this.createQueryBuilder('c')
Expand Down
13 changes: 12 additions & 1 deletion src/APIs/comments/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ICommentsServiceCreateComment,
ICommentsServiceDeleteComment,
ICommentsServiceFindComments,
ICommentsServiceFindUserComments,
ICommentsServiceId,
ICommentsServicePatchComment,
} from './interfaces/comments.service.interface';
Expand Down Expand Up @@ -125,7 +126,17 @@ export class CommentsService {
async fetchComments({
articleId,
}: ICommentsServiceFindComments): Promise<CommentsGetResponseDto[]> {
return await this.repo_comments.fetchComments({ articleId });
return await this.repo_comments.fetchAll({ articleId });
}

async fetchUserComments({
userId,
}: ICommentsServiceFindUserComments): Promise<CommentDto[]> {
return await this.repo_comments.find({
where: { userId },
order: { dateCreated: 'DESC' },
take: 10,
});
}

async delete({
Expand Down
4 changes: 4 additions & 0 deletions src/APIs/comments/interfaces/comments.service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface ICommentsServiceFindComments {
articleId: number;
}

export interface ICommentsServiceFindUserComments {
userId: number;
}

export interface ICommentsServiceDeleteComment {
commentId: number;
userId: number;
Expand Down

0 comments on commit 6cd362c

Please sign in to comment.