1
1
import { ApiProperty , OmitType } from '@nestjs/swagger' ;
2
2
import { Type } from 'class-transformer' ;
3
+ import dayjs from 'dayjs' ;
4
+ import { PostClothing } from 'src/common/entities/post-clothing.entity' ;
5
+ import { PostImage } from 'src/common/entities/post-image.entity' ;
6
+ import { Post } from 'src/common/entities/post.entity' ;
7
+ import { User } from 'src/common/entities/user.entity' ;
3
8
4
9
class PostImageDto {
5
10
@ApiProperty ( {
@@ -13,6 +18,11 @@ class PostImageDto {
13
18
description : '이미지의 순서 번호' ,
14
19
} )
15
20
orderNum : number ;
21
+
22
+ constructor ( postImage : PostImage ) {
23
+ this . imageUrl = postImage . url ;
24
+ this . orderNum = postImage . orderNum ;
25
+ }
16
26
}
17
27
18
28
class UserDto {
@@ -33,6 +43,12 @@ class UserDto {
33
43
description : '사용자의 프로필 사진 URL' ,
34
44
} )
35
45
profilePictureUrl : string ;
46
+
47
+ constructor ( user : User ) {
48
+ this . userId = user . id ;
49
+ this . nickname = user . nickname ;
50
+ this . profilePictureUrl = user . profilePictureUrl ;
51
+ }
36
52
}
37
53
38
54
class PostClothingDto {
@@ -59,6 +75,14 @@ class PostClothingDto {
59
75
description : '옷 상품 링크입니다.' ,
60
76
} )
61
77
url : string ;
78
+
79
+ constructor ( postClothing : PostClothing ) {
80
+ this . imageUrl = postClothing . clothing . imageUrl ;
81
+ this . brandName = postClothing . clothing . brandName ;
82
+ this . modelName = postClothing . clothing . modelName ;
83
+ this . modelNumber = postClothing . clothing . modelNumber ;
84
+ this . url = postClothing . clothing . url ;
85
+ }
62
86
}
63
87
64
88
export class PostResponse {
@@ -106,11 +130,69 @@ export class PostResponse {
106
130
description : '대표 게시물 여부입니다.' ,
107
131
} )
108
132
isRepresentative : boolean ;
133
+
134
+ constructor ( post : Post ) {
135
+ this . userId = post . user . id ;
136
+ this . postId = post . id ;
137
+ this . content = post . content ;
138
+ this . isRepresentative = post . isRepresentative ;
139
+ this . postImages =
140
+ post . postImages ?. map ( ( image ) => new PostImageDto ( image ) ) || [ ] ;
141
+ this . postStyletags =
142
+ post . postStyletags ?. map ( ( postStyleTag ) => postStyleTag . styletag . tag ) ||
143
+ [ ] ;
144
+ this . postClothings =
145
+ post . postClothings ?. map ( ( clothing ) => new PostClothingDto ( clothing ) ) ||
146
+ [ ] ;
147
+ }
109
148
}
149
+ export class PostDetailResponse {
150
+ @ApiProperty ( {
151
+ example : 1 ,
152
+ description : '게시물 번호입니다.' ,
153
+ } )
154
+ postId : number ;
155
+
156
+ @ApiProperty ( {
157
+ type : UserDto ,
158
+ description : '게시물 작성자 정보입니다.' ,
159
+ } )
160
+ @Type ( ( ) => UserDto )
161
+ user : UserDto ;
162
+
163
+ @ApiProperty ( {
164
+ example : '게시물 내용' ,
165
+ description : '게시물 내용입니다. 최대 100자까지 입력할 수 있습니다.' ,
166
+ } )
167
+ content : string ;
110
168
111
- class PostDetailDto extends OmitType ( PostResponse , [ 'userId' ] ) { }
169
+ @ApiProperty ( {
170
+ type : [ PostImageDto ] ,
171
+ description : '게시물에 포함된 이미지 목록입니다.' ,
172
+ } )
173
+ postImages ?: PostImageDto [ ] ;
174
+
175
+ @ApiProperty ( {
176
+ type : [ String ] ,
177
+ example : [ 'classic' , 'basic' ] ,
178
+ description :
179
+ '게시글에 포함된 스타일 태그 목록입니다. 스타일 태그에 저장된 태그만 입력 가능합니다.' ,
180
+ } )
181
+ postStyletags ?: string [ ] ;
182
+
183
+ @ApiProperty ( {
184
+ type : [ PostClothingDto ] ,
185
+ description : '게시물에 포함된 옷 정보 리스트입니다.' ,
186
+ } )
187
+ @Type ( ( ) => PostClothingDto )
188
+ postClothings ?: PostClothingDto [ ] ;
189
+
190
+ @ApiProperty ( {
191
+ example : false ,
192
+ description : '대표 게시물 여부입니다.' ,
193
+ } )
194
+ isRepresentative : boolean ;
112
195
113
- export class PostDetailResponse extends PostDetailDto {
114
196
@ApiProperty ( {
115
197
example : '2024-10-11T09:00:00.000Z' ,
116
198
description : '생성 시각' ,
@@ -123,13 +205,6 @@ export class PostDetailResponse extends PostDetailDto {
123
205
} )
124
206
updatedAt : string ;
125
207
126
- @ApiProperty ( {
127
- type : UserDto ,
128
- description : '게시물 작성자 정보입니다.' ,
129
- } )
130
- @Type ( ( ) => UserDto )
131
- user : UserDto ;
132
-
133
208
@ApiProperty ( {
134
209
example : 10 ,
135
210
description : '게시글에 달린 댓글 수입니다.' ,
@@ -147,4 +222,24 @@ export class PostDetailResponse extends PostDetailDto {
147
222
description : '현재 사용자가 게시물에 좋아요를 눌렀는지 여부' ,
148
223
} )
149
224
isPostLike : boolean ;
225
+ constructor ( post : Post , currentUserId : number ) {
226
+ this . postId = post . id ;
227
+ this . content = post . content ;
228
+ this . isRepresentative = post . isRepresentative ;
229
+ this . postImages =
230
+ post . postImages ?. map ( ( image ) => new PostImageDto ( image ) ) || [ ] ;
231
+ this . postStyletags =
232
+ post . postStyletags ?. map ( ( postStyleTag ) => postStyleTag . styletag . tag ) ||
233
+ [ ] ;
234
+ this . postClothings =
235
+ post . postClothings ?. map ( ( clothing ) => new PostClothingDto ( clothing ) ) ||
236
+ [ ] ;
237
+ ( this . createdAt = dayjs ( post . createdAt ) . format ( 'YYYY-MM-DDTHH:mm:ssZ' ) ) ,
238
+ ( this . updatedAt = dayjs ( post . updatedAt ) . format ( 'YYYY-MM-DDTHH:mm:ssZ' ) ) ,
239
+ ( this . user = new UserDto ( post . user ) ) ;
240
+ this . postCommentsCount = post . postComments ?. length || 0 ;
241
+ this . postLikesCount = post . postLikes ?. length || 0 ;
242
+ this . isPostLike =
243
+ post . postLikes ?. some ( ( like ) => like . user . id === currentUserId ) || false ;
244
+ }
150
245
}
0 commit comments