@@ -21,6 +21,7 @@ import { GetAllPostsResponse } from './dtos/all-posts.response';
21
21
import {
22
22
GetMyPostsResponse ,
23
23
GetOtherPostsResponse ,
24
+ PostDto ,
24
25
} from './dtos/user-posts.response' ;
25
26
import { PostDetailResponse } from './dtos/post.response' ;
26
27
@@ -98,7 +99,7 @@ export class PostService {
98
99
posts : GetMyPostsResponse | GetOtherPostsResponse ;
99
100
total : number ;
100
101
} > {
101
- const queryBuilder = this . dataSource
102
+ const [ posts , total ] = await this . dataSource
102
103
. getRepository ( Post )
103
104
. createQueryBuilder ( 'post' )
104
105
. leftJoinAndSelect ( 'post.user' , 'user' )
@@ -109,25 +110,26 @@ export class PostService {
109
110
{ status : 'activated' , orderNum : 1 } ,
110
111
)
111
112
. leftJoinAndSelect ( 'post.postLikes' , 'postLike' )
113
+ . leftJoinAndSelect ( 'postLike.user' , 'postLikeUser' )
112
114
. leftJoinAndSelect ( 'post.postComments' , 'postComment' )
115
+ . leftJoinAndSelect ( 'postComment.user' , 'postCommentUser' )
113
116
. 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 } )
117
118
. select ( [
118
119
'post.id' ,
119
120
'post.isRepresentative' ,
120
121
'post.createdAt' ,
121
122
'user.id' ,
122
123
'postImage.url' ,
123
- 'postLike.user.id' ,
124
- 'postComment.user.id' ,
124
+ 'postComment.id' ,
125
+ 'postLike.id' ,
126
+ 'postLikeUser.id' ,
127
+ 'postCommentUser.id' ,
125
128
] )
126
129
. orderBy ( 'post.createdAt' , 'DESC' )
127
130
. take ( pageOptionsDto . take )
128
131
. skip ( ( pageOptionsDto . page - 1 ) * pageOptionsDto . take )
129
132
. getManyAndCount ( ) ;
130
-
131
133
return {
132
134
posts :
133
135
userId === currentUserId
@@ -137,45 +139,12 @@ export class PostService {
137
139
} ;
138
140
}
139
141
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
-
163
142
private formatMyPosts (
164
143
posts : Post [ ] ,
165
144
currentUserId : number ,
166
145
) : GetMyPostsResponse {
167
146
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 ) ) ,
179
148
totalPostCommentsCount : this . calculateTotalComments ( posts ) ,
180
149
totalPostsCount : posts . length ,
181
150
totalPostLikesCount : this . calculateTotalLikes ( posts ) ,
@@ -187,20 +156,19 @@ export class PostService {
187
156
currentUserId : number ,
188
157
) : GetOtherPostsResponse {
189
158
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 ) ) ,
199
160
totalPostsCount : posts . length ,
200
161
totalPostLikesCount : this . calculateTotalLikes ( posts ) ,
201
162
} ;
202
163
}
203
164
165
+ private formatAllPosts (
166
+ posts : Post [ ] ,
167
+ currentUserId : number ,
168
+ ) : GetAllPostsResponse {
169
+ return new GetAllPostsResponse ( posts , currentUserId ) ;
170
+ }
171
+
204
172
async createPost ( uploadPostDto : CreatePostRequest , currentUserId : number ) {
205
173
const {
206
174
content,
0 commit comments