Skip to content

Commit 6802e73

Browse files
authored
Merge pull request #19 from oodd-team/OD-48
댓글 리스트 조회 기능
2 parents 11b5d22 + 752b623 commit 6802e73

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsBoolean, IsString } from 'class-validator';
3+
4+
class CommentDto {
5+
@ApiProperty({ example: '댓글 내용' })
6+
@IsString()
7+
content: string;
8+
9+
@ApiProperty({ example: '2024-10-04 17:49:53' })
10+
createdAt: String;
11+
12+
@ApiProperty({
13+
properties: {
14+
nickname: { type: 'string', example: '닉넴' },
15+
profilePictureUrl: { type: 'string', example: 'profilePictureUrl.jpeg' },
16+
},
17+
})
18+
user: {
19+
nickname: string;
20+
profilePictureUrl: string;
21+
};
22+
23+
@ApiProperty()
24+
@IsBoolean()
25+
isCommentWriter: boolean; // 현재 사용자가 댓글 작성자인지 여부
26+
}
27+
28+
export class GetCommentsDto {
29+
@ApiProperty({ type: [CommentDto] })
30+
comments: CommentDto[];
31+
32+
@ApiProperty({ example: '10' })
33+
@IsBoolean()
34+
totalComments: number;
35+
}

src/post-comment/post-comment.controller.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { PostService } from 'src/post/post.service';
2222
import { AuthGuard } from 'src/auth/guards/jwt.auth.guard';
2323
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
2424
import { PostComment } from 'src/common/entities/post-comment.entity';
25+
import { GetCommentsDto } from './dtos/get-comment.dto';
26+
import dayjs from 'dayjs';
2527

2628
@ApiBearerAuth()
2729
@Controller('post-comment')
@@ -53,10 +55,30 @@ export class PostCommentController {
5355
return new BaseResponse(true, '댓글 작성 성공', postComment);
5456
}
5557

56-
@Get()
58+
@Get(':postId')
5759
@GetPostCommentsSwagger('게시글 댓글 리스트 조회 API')
58-
getPostCommenst() {
59-
// return this.userService.getHello();
60+
async getPostComments(
61+
@Query('postId') postId: number,
62+
@Req() req: Request,
63+
): Promise<BaseResponse<GetCommentsDto>> {
64+
const currentUserId = req.user.userId;
65+
66+
const comments = await this.postCommentService.getPostComments(postId);
67+
68+
const commenteResponse: GetCommentsDto = {
69+
comments: comments.map((comment) => ({
70+
content: comment.content,
71+
createdAt: dayjs(comment.createdAt).format('YYYY-MM-DDTHH:mm:ssZ'),
72+
user: {
73+
nickname: comment.user.nickname,
74+
profilePictureUrl: comment.user.profilePictureUrl,
75+
},
76+
isCommentWriter: comment.user.id == currentUserId,
77+
})),
78+
totalComments: comments.length,
79+
};
80+
81+
return new BaseResponse(true, '댓글 목록 조회 성공', commenteResponse);
6082
}
6183

6284
@Delete(':commentId')

src/post-comment/post-comment.service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ export class PostCommentService {
8484
}
8585
}
8686

87+
// 댓글 리스트 조회
88+
async getPostComments(postId: number): Promise<PostComment[]> {
89+
return await this.postCommentRepository.find({
90+
where: { post: { id: postId }, status: 'activated' },
91+
relations: ['user'],
92+
});
93+
}
94+
8795
private async findCommentById(commentId: number): Promise<PostComment> {
8896
const comment = await this.postCommentRepository.findOne({
8997
where: { id: commentId, status: 'activated' },

src/post-comment/post-comment.swagger.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {
55
ApiForbiddenResponse,
66
ApiInternalServerErrorResponse,
77
ApiNotFoundResponse,
8-
ApiOperation,
98
ApiUnauthorizedResponse,
109
} from '@nestjs/swagger';
1110
import { BaseSwaggerDecorator } from 'nestjs-swagger-decorator';
1211
import { BaseResponse } from 'src/common/response/dto';
1312
import { CreateCommentDto } from './dtos/create-comment.dto';
13+
import { GetCommentsDto } from './dtos/get-comment.dto';
1414

1515
// 게시글 댓글 생성 API Swagger
1616
export function CreatePostCommentSwagger(text: string) {
@@ -43,8 +43,33 @@ export function CreatePostCommentSwagger(text: string) {
4343
}
4444

4545
// 게시글 댓글 조회 API Swagger
46-
export function GetPostCommentsSwagger(apiSummary: string) {
47-
return ApiOperation({ summary: apiSummary });
46+
export function GetPostCommentsSwagger(text: string) {
47+
return BaseSwaggerDecorator(
48+
{ summary: text },
49+
[],
50+
[
51+
ApiAcceptedResponse({
52+
description: '댓글 리스트 조회 성공',
53+
type: GetCommentsDto,
54+
}),
55+
ApiBadRequestResponse({
56+
description: '잘못된 요청입니다.',
57+
type: BaseResponse,
58+
}),
59+
ApiUnauthorizedResponse({
60+
description: '인증되지 않은 사용자입니다.',
61+
type: BaseResponse,
62+
}),
63+
ApiNotFoundResponse({
64+
description: '존재하지 않는 댓글입니다.',
65+
type: BaseResponse,
66+
}),
67+
ApiInternalServerErrorResponse({
68+
description: '서버 에러입니다.',
69+
type: BaseResponse,
70+
}),
71+
],
72+
);
4873
}
4974

5075
// 게시글 댓글 삭제 API Swagger

0 commit comments

Comments
 (0)