@@ -11,11 +11,11 @@ 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
21
DeletePostSwagger ,
@@ -33,6 +33,10 @@ import { GetPostResponse } from './dtos/get-post.dto';
33
33
import { PatchPostDto } from './dtos/patch-Post.dto' ;
34
34
import { PageOptionsDto } from './dtos/page-options.dto' ;
35
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' ;
36
40
37
41
@Controller ( 'post' )
38
42
@ApiBearerAuth ( 'Authorization' )
@@ -41,12 +45,13 @@ import { PageDto } from './dtos/page.dto';
41
45
export class PostController {
42
46
constructor ( private readonly postService : PostService ) { }
43
47
44
- @Get ( '/' )
48
+ @Get ( )
45
49
@GetPostsSwagger ( '게시글 리스트 조회 API' )
46
50
@ApiQuery ( {
47
51
name : 'userId' ,
48
52
required : false ,
49
- description : 'User ID' ,
53
+ description :
54
+ 'User ID가 제공되면 사용자 게시글 조회, 제공되지 않으면 전체 게시글이 조회됩니다.' ,
50
55
type : Number ,
51
56
} )
52
57
@ApiQuery ( {
@@ -66,25 +71,104 @@ export class PostController {
66
71
@Query ( 'userId' ) userId ?: number ,
67
72
) : Promise <
68
73
BaseResponse <
69
- PageDto < GetPostsResponse | GetMyPostsResponse | GetOtherPostsResponse >
74
+ PageDto < GetAllPostsResponse | GetMyPostsResponse | GetOtherPostsResponse >
70
75
>
71
76
> {
72
- const currentUserId = req . user . id ;
73
-
74
- const options = pageOptionsDto
77
+ const pageOptions = pageOptionsDto
75
78
? {
76
79
...new PageOptionsDto ( ) ,
77
- ...pageOptionsDto , // Dto에 전달된 기본값
80
+ ...pageOptionsDto ,
78
81
}
79
82
: new PageOptionsDto ( ) ;
80
83
81
- const postsResponse = await this . postService . getPosts (
82
- options ,
83
- userId ,
84
- 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 ) ,
85
104
) ;
105
+ }
86
106
87
- 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
+ } ;
88
172
}
89
173
90
174
@Get ( ':postId' )
0 commit comments