@@ -11,13 +11,14 @@ import {
11
11
UseGuards ,
12
12
} from '@nestjs/common' ;
13
13
import { PostService } from './post.service' ;
14
- import { GetPostsResponse } from './dtos/total-postsResponse.dto ' ;
14
+ import { GetAllPostsResponse } from './dtos/all-posts.response ' ;
15
15
import {
16
16
GetMyPostsResponse ,
17
17
GetOtherPostsResponse ,
18
- } from './dtos/user-postsResponse.dto ' ;
18
+ } from './dtos/user-posts.response ' ;
19
19
import {
20
20
CreatePostsSwagger ,
21
+ DeletePostSwagger ,
21
22
GetPostsSwagger ,
22
23
GetPostSwagger ,
23
24
PatchIsRepresentativeSwagger ,
@@ -32,20 +33,25 @@ import { GetPostResponse } from './dtos/get-post.dto';
32
33
import { PatchPostDto } from './dtos/patch-Post.dto' ;
33
34
import { PageOptionsDto } from './dtos/page-options.dto' ;
34
35
import { PageDto } from './dtos/page.dto' ;
36
+ import dayjs from 'dayjs' ;
37
+ import { PageMetaDto } from './dtos/page-meta.dto' ;
38
+ import { DataNotFoundException } from 'src/common/exception/service.exception' ;
39
+ import { Post as PostEntity } from 'src/common/entities/post.entity' ;
35
40
36
41
@Controller ( 'post' )
37
- @ApiBearerAuth ( )
42
+ @ApiBearerAuth ( 'Authorization' )
38
43
@UseGuards ( AuthGuard )
39
44
@ApiTags ( '[서비스] 게시글' )
40
45
export class PostController {
41
46
constructor ( private readonly postService : PostService ) { }
42
47
43
- @Get ( '/' )
48
+ @Get ( )
44
49
@GetPostsSwagger ( '게시글 리스트 조회 API' )
45
50
@ApiQuery ( {
46
51
name : 'userId' ,
47
52
required : false ,
48
- description : 'User ID' ,
53
+ description :
54
+ 'User ID가 제공되면 사용자 게시글 조회, 제공되지 않으면 전체 게시글이 조회됩니다.' ,
49
55
type : Number ,
50
56
} )
51
57
@ApiQuery ( {
@@ -65,25 +71,104 @@ export class PostController {
65
71
@Query ( 'userId' ) userId ?: number ,
66
72
) : Promise <
67
73
BaseResponse <
68
- PageDto < GetPostsResponse | GetMyPostsResponse | GetOtherPostsResponse >
74
+ PageDto < GetAllPostsResponse | GetMyPostsResponse | GetOtherPostsResponse >
69
75
>
70
76
> {
71
- const currentUserId = req . user . id ;
72
-
73
- const options = pageOptionsDto
77
+ const pageOptions = pageOptionsDto
74
78
? {
75
79
...new PageOptionsDto ( ) ,
76
- ...pageOptionsDto , // Dto에 전달된 기본값
80
+ ...pageOptionsDto ,
77
81
}
78
82
: new PageOptionsDto ( ) ;
79
83
80
- const postsResponse = await this . postService . getPosts (
81
- options ,
82
- userId ,
83
- currentUserId ,
84
+ const { posts, total } = userId
85
+ ? await this . postService . getUserPosts ( pageOptions , userId )
86
+ : await this . postService . getAllPosts ( pageOptions , req . user . id ) ;
87
+
88
+ const pageMetaDto = new PageMetaDto ( {
89
+ pageOptionsDto : pageOptions ,
90
+ total,
91
+ } ) ;
92
+
93
+ if ( pageMetaDto . last_page < pageMetaDto . page ) {
94
+ throw DataNotFoundException ( '해당 페이지는 존재하지 않습니다' ) ;
95
+ }
96
+ const postsResponse = userId
97
+ ? this . createUserPostsResponse ( posts , req . user . id , userId )
98
+ : this . createAllPostsResponse ( posts , req . user . id ) ;
99
+
100
+ return new BaseResponse (
101
+ true ,
102
+ '게시글 리스트 조회 성공' ,
103
+ new PageDto ( [ postsResponse ] , pageMetaDto ) ,
84
104
) ;
105
+ }
85
106
86
- return new BaseResponse ( true , '게시글 리스트 조회 성공' , postsResponse ) ;
107
+ private createUserPostsResponse (
108
+ posts : PostEntity [ ] ,
109
+ currentUserId : number ,
110
+ userId : number ,
111
+ ) : GetMyPostsResponse | GetOtherPostsResponse {
112
+ if ( userId == currentUserId ) {
113
+ return {
114
+ post : posts . map ( ( post ) => ( {
115
+ userId : post . user . id ,
116
+ postId : post . id ,
117
+ createdAt : dayjs ( post . createdAt ) . format ( 'YYYY-MM-DDTHH:mm:ssZ' ) ,
118
+ imageUrl : post . postImages . find (
119
+ ( image ) => image . orderNum == 1 && image . status === 'activated' ,
120
+ ) ?. url ,
121
+ isRepresentative : post . isRepresentative ,
122
+ likeCount : post . postLikes . length ,
123
+ commentCount : post . postComments . length ,
124
+ isPostLike : this . postService . checkIsPostLiked ( post , currentUserId ) ,
125
+ isPostComment : this . postService . checkIsPostCommented (
126
+ post ,
127
+ currentUserId ,
128
+ ) ,
129
+ } ) ) ,
130
+ totalComments : this . postService . calculateTotalComments ( posts ) ,
131
+ totalPosts : posts . length ,
132
+ totalLikes : this . postService . calculateTotalLikes ( posts ) ,
133
+ } ;
134
+ } else {
135
+ return {
136
+ post : posts . map ( ( post ) => ( {
137
+ userId : post . user . id ,
138
+ postId : post . id ,
139
+ createdAt : dayjs ( post . createdAt ) . format ( 'YYYY-MM-DDTHH:mm:ssZ' ) ,
140
+ imageUrl : post . postImages . find (
141
+ ( image ) => image . orderNum == 1 && image . status === 'activated' ,
142
+ ) ?. url ,
143
+ isRepresentative : post . isRepresentative ,
144
+ likeCount : post . postLikes . length ,
145
+ isPostLike : this . postService . checkIsPostLiked ( post , currentUserId ) ,
146
+ } ) ) ,
147
+ totalPosts : posts . length ,
148
+ totalLikes : this . postService . calculateTotalLikes ( posts ) ,
149
+ } ;
150
+ }
151
+ }
152
+ private createAllPostsResponse ( posts : PostEntity [ ] , currentUserId : number ) {
153
+ return {
154
+ post : posts . map ( ( post ) => ( {
155
+ postId : post . id ,
156
+ content : post . content ,
157
+ createdAt : dayjs ( post . createdAt ) . format ( 'YYYY-MM-DDTHH:mm:ssZ' ) ,
158
+ postImages : post . postImages
159
+ . filter ( ( image ) => image . status === 'activated' )
160
+ . map ( ( image ) => ( {
161
+ url : image . url ,
162
+ orderNum : image . orderNum ,
163
+ } ) ) ,
164
+ isPostLike : this . postService . checkIsPostLiked ( post , currentUserId ) ,
165
+ user : {
166
+ userId : post . user . id ,
167
+ nickname : post . user . nickname ,
168
+ profilePictureUrl : post . user . profilePictureUrl ,
169
+ } ,
170
+ } ) ) ,
171
+ } ;
87
172
}
88
173
89
174
@Get ( ':postId' )
@@ -165,7 +250,7 @@ export class PostController {
165
250
}
166
251
167
252
@Delete ( ':postId' )
168
- @PatchPostSwagger ( '게시글 삭제 API' )
253
+ @DeletePostSwagger ( '게시글 삭제 API' )
169
254
async deletePost (
170
255
@Param ( 'postId' ) postId : number ,
171
256
@Req ( ) req : Request ,
0 commit comments