Skip to content

Commit c4cbd42

Browse files
authored
Merge pull request #51 from oodd-team/OD-38-new
likecount, 사용자 게시물 응답에 commentCount추가
2 parents 604e5e5 + 5ba97c4 commit c4cbd42

8 files changed

+81
-88
lines changed

src/matching/dto/matching.response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class PatchMatchingResponse {
3636
class RequesterResponse {
3737
@ApiProperty({
3838
description: '매칭 요청자의 ID',
39-
example: '19',
39+
example: 19,
4040
})
4141
requesterId: number;
4242

src/post-comment/dtos/get-comment.dto.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
22
import { IsBoolean, IsNumber, IsString } from 'class-validator';
33

44
class UserDto {
5-
@ApiProperty({ example: '10' })
5+
@ApiProperty({ example: 10 })
66
id: number;
77

88
@ApiProperty({ example: '닉네임' })
@@ -13,7 +13,7 @@ class UserDto {
1313
}
1414

1515
class CommentDto {
16-
@ApiProperty({ example: '10' })
16+
@ApiProperty({ example: 10 })
1717
@IsNumber()
1818
id: number;
1919

@@ -41,7 +41,7 @@ export class GetCommentsDto {
4141
@ApiProperty({ type: [CommentDto] })
4242
comments: CommentDto[];
4343

44-
@ApiProperty({ example: '10' })
44+
@ApiProperty({ example: 10 })
4545
@IsBoolean()
4646
totalCount: number;
4747
}

src/post/dtos/all-posts.response.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { ApiProperty } from '@nestjs/swagger';
2+
import dayjs from 'dayjs';
3+
import { Post } from 'src/common/entities/post.entity';
4+
import { PostImage } from 'src/common/entities/post-image.entity';
25

36
class PostImageDto {
47
@ApiProperty({
@@ -12,11 +15,16 @@ class PostImageDto {
1215
description: '이미지의 순서를 나타내는 번호입니다.',
1316
})
1417
orderNum: number;
18+
19+
constructor(postImage: PostImage) {
20+
this.imageUrl = postImage.url;
21+
this.orderNum = postImage.orderNum;
22+
}
1523
}
1624

1725
class UserDto {
1826
@ApiProperty({
19-
example: '19',
27+
example: 19,
2028
description: '작성자의 user ID 입니다.',
2129
})
2230
userId: number;
@@ -32,11 +40,17 @@ class UserDto {
3240
description: '작성자의 프로필 사진 URL입니다.',
3341
})
3442
profilePictureUrl: string;
43+
44+
constructor(user: any) {
45+
this.userId = user.id;
46+
this.nickname = user.nickname;
47+
this.profilePictureUrl = user.profilePictureUrl;
48+
}
3549
}
3650

3751
export class PostDto {
3852
@ApiProperty({
39-
example: '3',
53+
example: 3,
4054
description: '게시글의 번호입니다.',
4155
})
4256
postId: number;
@@ -70,6 +84,17 @@ export class PostDto {
7084
description: '게시글 작성자의 정보입니다.',
7185
})
7286
user: UserDto;
87+
88+
constructor(post: Post, currentUserId: number) {
89+
this.postId = post.id;
90+
this.content = post.content;
91+
this.createdAt = dayjs(post.createdAt).format('YYYY-MM-DDTHH:mm:ssZ');
92+
this.postImages =
93+
post.postImages?.map((image) => new PostImageDto(image)) || [];
94+
this.isPostLike =
95+
post.postLikes?.some((like) => like.user.id === currentUserId) || false;
96+
this.user = new UserDto(post.user);
97+
}
7398
}
7499

75100
export class GetAllPostsResponse {
@@ -78,4 +103,8 @@ export class GetAllPostsResponse {
78103
description: '조회된 게시글 목록입니다.',
79104
})
80105
post: PostDto[];
106+
107+
constructor(posts: Post[], currentUserId: number) {
108+
this.post = posts.map((post) => new PostDto(post, currentUserId));
109+
}
81110
}

src/post/dtos/post.response.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class PostImageDto {
1717

1818
class UserDto {
1919
@ApiProperty({
20-
example: '작성자 번호',
20+
example: 1,
2121
description: 'userId',
2222
})
2323
userId: number;
@@ -63,13 +63,13 @@ class PostClothingDto {
6363

6464
export class PostResponse {
6565
@ApiProperty({
66-
example: '게시물 번호',
66+
example: 1,
6767
description: '게시물 번호입니다.',
6868
})
6969
postId: number;
7070

7171
@ApiProperty({
72-
example: '작성자 번호',
72+
example: 1,
7373
description: 'userId',
7474
})
7575
userId: number;

src/post/dtos/user-posts.response.ts

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { ApiProperty, OmitType } from '@nestjs/swagger';
2+
import dayjs from 'dayjs';
3+
import { Post } from 'src/common/entities/post.entity';
24

3-
class PostDto {
5+
export class PostDto {
46
@ApiProperty({
5-
example: '19',
7+
example: 19,
68
description: '조회한 작성자의 user ID 입니다.',
79
})
810
userId: number;
911

1012
@ApiProperty({
11-
example: '3',
13+
example: 3,
1214
description: '게시글의 번호입니다.',
1315
})
1416
postId: number;
@@ -54,7 +56,23 @@ class PostDto {
5456
description: '현재 사용자가 게시글에 댓글을 작성했는지 여부입니다.',
5557
})
5658
isPostComment: boolean;
59+
60+
constructor(post: Post, currentUserId: number) {
61+
this.userId = post.user.id;
62+
this.postId = post.id;
63+
this.isRepresentative = post.isRepresentative;
64+
(this.createdAt = dayjs(post.createdAt).format('YYYY-MM-DDTHH:mm:ssZ')),
65+
(this.imageUrl = post.postImages?.[0]?.url || '');
66+
this.postCommentsCount = post.postComments?.length || 0;
67+
this.postLikesCount = post.postLikes?.length || 0;
68+
this.isPostLike =
69+
post.postLikes?.some((like) => like.user.id === currentUserId) || false;
70+
this.isPostComment =
71+
post.postComments?.some((comment) => comment.user.id === currentUserId) ||
72+
false;
73+
}
5774
}
75+
5876
export class GetMyPostsResponse {
5977
@ApiProperty({
6078
type: [PostDto],
@@ -80,28 +98,6 @@ export class GetMyPostsResponse {
8098
})
8199
totalPostLikesCount: number;
82100
}
83-
84-
class OtherUserPostDto extends OmitType(PostDto, [
85-
'postCommentsCount',
86-
'isPostComment',
101+
export class GetOtherPostsResponse extends OmitType(GetMyPostsResponse, [
102+
'totalPostCommentsCount',
87103
]) {}
88-
89-
export class GetOtherPostsResponse {
90-
@ApiProperty({
91-
type: [OtherUserPostDto],
92-
description: '다른 사용자의 게시글 목록입니다.',
93-
})
94-
post: OtherUserPostDto[];
95-
96-
@ApiProperty({
97-
example: 30,
98-
description: '다른 사용자가 작성한 총 게시글 수입니다.',
99-
})
100-
totalPostsCount: number;
101-
102-
@ApiProperty({
103-
example: 200,
104-
description: '다른 사용자가 받은 총 좋아요 수입니다.',
105-
})
106-
totalPostLikesCount: number;
107-
}

src/post/post.service.ts

+18-50
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { GetAllPostsResponse } from './dtos/all-posts.response';
2121
import {
2222
GetMyPostsResponse,
2323
GetOtherPostsResponse,
24+
PostDto,
2425
} from './dtos/user-posts.response';
2526
import { PostDetailResponse } from './dtos/post.response';
2627

@@ -98,7 +99,7 @@ export class PostService {
9899
posts: GetMyPostsResponse | GetOtherPostsResponse;
99100
total: number;
100101
}> {
101-
const queryBuilder = this.dataSource
102+
const [posts, total] = await this.dataSource
102103
.getRepository(Post)
103104
.createQueryBuilder('post')
104105
.leftJoinAndSelect('post.user', 'user')
@@ -109,25 +110,26 @@ export class PostService {
109110
{ status: 'activated', orderNum: 1 },
110111
)
111112
.leftJoinAndSelect('post.postLikes', 'postLike')
113+
.leftJoinAndSelect('postLike.user', 'postLikeUser')
112114
.leftJoinAndSelect('post.postComments', 'postComment')
115+
.leftJoinAndSelect('postComment.user', 'postCommentUser')
113116
.where('post.status = :status', { status: 'activated' })
114-
.andWhere('post.user.id = :userId', { userId });
115-
116-
const [posts, total] = await queryBuilder
117+
.andWhere('post.user.id = :userId', { userId })
117118
.select([
118119
'post.id',
119120
'post.isRepresentative',
120121
'post.createdAt',
121122
'user.id',
122123
'postImage.url',
123-
'postLike.user.id',
124-
'postComment.user.id',
124+
'postComment.id',
125+
'postLike.id',
126+
'postLikeUser.id',
127+
'postCommentUser.id',
125128
])
126129
.orderBy('post.createdAt', 'DESC')
127130
.take(pageOptionsDto.take)
128131
.skip((pageOptionsDto.page - 1) * pageOptionsDto.take)
129132
.getManyAndCount();
130-
131133
return {
132134
posts:
133135
userId === currentUserId
@@ -137,45 +139,12 @@ export class PostService {
137139
};
138140
}
139141

140-
private formatAllPosts(
141-
posts: Post[],
142-
currentUserId: number,
143-
): GetAllPostsResponse {
144-
return {
145-
post: posts.map((post) => ({
146-
postId: post.id,
147-
content: post.content,
148-
createdAt: dayjs(post.createdAt).format('YYYY-MM-DDTHH:mm:ssZ'),
149-
postImages: post.postImages.map((image) => ({
150-
imageUrl: image.url,
151-
orderNum: image.orderNum,
152-
})),
153-
isPostLike: this.checkIsPostLiked(post, currentUserId),
154-
user: {
155-
userId: post.user.id,
156-
nickname: post.user.nickname,
157-
profilePictureUrl: post.user.profilePictureUrl,
158-
},
159-
})),
160-
};
161-
}
162-
163142
private formatMyPosts(
164143
posts: Post[],
165144
currentUserId: number,
166145
): GetMyPostsResponse {
167146
return {
168-
post: posts.map((post) => ({
169-
postId: post.id,
170-
userId: post.user.id,
171-
createdAt: dayjs(post.createdAt).format('YYYY-MM-DDTHH:mm:ssZ'),
172-
imageUrl: post.postImages[0]?.url,
173-
isRepresentative: post.isRepresentative,
174-
isPostLike: this.checkIsPostLiked(post, currentUserId),
175-
isPostComment: this.checkIsPostCommented(post, currentUserId),
176-
postLikesCount: post.postLikes.length,
177-
postCommentsCount: post.postComments.length,
178-
})),
147+
post: posts.map((post) => new PostDto(post, currentUserId)),
179148
totalPostCommentsCount: this.calculateTotalComments(posts),
180149
totalPostsCount: posts.length,
181150
totalPostLikesCount: this.calculateTotalLikes(posts),
@@ -187,20 +156,19 @@ export class PostService {
187156
currentUserId: number,
188157
): GetOtherPostsResponse {
189158
return {
190-
post: posts.map((post) => ({
191-
postId: post.id,
192-
userId: post.user.id,
193-
createdAt: dayjs(post.createdAt).format('YYYY-MM-DDTHH:mm:ssZ'),
194-
imageUrl: post.postImages[0]?.url,
195-
isRepresentative: post.isRepresentative,
196-
isPostLike: this.checkIsPostLiked(post, currentUserId),
197-
postLikesCount: post.postLikes.length,
198-
})),
159+
post: posts.map((post) => new PostDto(post, currentUserId)),
199160
totalPostsCount: posts.length,
200161
totalPostLikesCount: this.calculateTotalLikes(posts),
201162
};
202163
}
203164

165+
private formatAllPosts(
166+
posts: Post[],
167+
currentUserId: number,
168+
): GetAllPostsResponse {
169+
return new GetAllPostsResponse(posts, currentUserId);
170+
}
171+
204172
async createPost(uploadPostDto: CreatePostRequest, currentUserId: number) {
205173
const {
206174
content,

src/user/dto/response/get-user.response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApiProperty } from '@nestjs/swagger';
33
export class GetUserInfo {
44
@ApiProperty({
55
description: 'user ID',
6-
example: '19',
6+
example: 19,
77
})
88
userId: number;
99

src/user/dto/response/patch-user.response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApiProperty } from '@nestjs/swagger';
33
export class PatchUserResponse {
44
@ApiProperty({
55
description: 'user ID',
6-
example: '19',
6+
example: 19,
77
})
88
userId: number;
99

0 commit comments

Comments
 (0)