@@ -36,16 +36,7 @@ public class PostController {
3636 @ Operation (summary = "게시글 등록" )
3737 @ PostMapping
3838 public ResponseEntity <ApiResponse <PostDto >> createPost (@ RequestBody PostRequestDto postRequestDto ) {
39- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
40- Object principal = authentication .getPrincipal ();
41- Long memberId ;
42- if (principal instanceof org .springframework .security .core .userdetails .User user ) {
43- memberId = Long .valueOf (user .getUsername ());
44- } else if (principal instanceof Long ) {
45- memberId = (Long ) principal ;
46- } else {
47- throw new IllegalArgumentException ("올바른 회원 ID가 아닙니다" );
48- }
39+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
4940 PostDto created = postService .createPost (postRequestDto , memberId );
5041 return ResponseEntity .ok (new ApiResponse <>(201 , "게시글이 등록되었습니다." , created ));
5142 }
@@ -91,43 +82,31 @@ public ResponseEntity<ApiResponse<List<PostDetailDto>>> getPostsByMember(@PathVa
9182 @ Operation (summary = "게시글 수정" )
9283 @ PutMapping ("/{postId}" )
9384 public ResponseEntity <ApiResponse <PostDetailDto >> updatePost (@ PathVariable Long postId , @ RequestBody PostUpdateDto postUpdateDto ) {
94- Long currentMemberId = AuthUtil .getCurrentMemberId ();
95- String currentRole = AuthUtil .getCurrentMemberRole ();
96- PostDetailDto postDetail = postService .getPostDetailById (postId , currentMemberId );
85+ PostDetailDto postDetail = postService .getPostDetailById (postId , AuthUtil .getAuthenticatedMemberId ());
9786 Long postOwnerId = postDetail .getPost ().getMemberId ();
98- if (!postOwnerId .equals (currentMemberId ) && !"ADMIN" .equals (currentRole )) {
99- return ResponseEntity .status (403 ).body (new ApiResponse <>(403 , "본인 또는 관리자만 수정 가능합니다." , null ));
100- }
87+ AuthUtil .validateOwnerOrAdmin (postOwnerId );
10188 postService .updatePost (postId , postUpdateDto );
102- PostDetailDto updated = postService .getPostDetailById (postId , currentMemberId );
89+ PostDetailDto updated = postService .getPostDetailById (postId , AuthUtil . getAuthenticatedMemberId () );
10390 return ResponseEntity .ok (new ApiResponse <>(200 , "게시글이 수정되었습니다." , updated ));
10491 }
10592
10693 @ Operation (summary = "게시글 부분 수정(PATCH)" )
10794 @ PatchMapping ("/{postId}" )
10895 public ResponseEntity <ApiResponse <PostDetailDto >> patchUpdatePost (@ PathVariable Long postId , @ RequestBody PostUpdateDto postUpdateDto ) {
109- Long currentMemberId = AuthUtil .getCurrentMemberId ();
110- String currentRole = AuthUtil .getCurrentMemberRole ();
111- PostDetailDto postDetail = postService .getPostDetailById (postId , currentMemberId );
96+ PostDetailDto postDetail = postService .getPostDetailById (postId , AuthUtil .getAuthenticatedMemberId ());
11297 Long postOwnerId = postDetail .getPost ().getMemberId ();
113- if (!postOwnerId .equals (currentMemberId ) && !"ADMIN" .equals (currentRole )) {
114- return ResponseEntity .status (403 ).body (new ApiResponse <>(403 , "본인 또는 관리자만 수정 가능합니다." , null ));
115- }
98+ AuthUtil .validateOwnerOrAdmin (postOwnerId );
11699 postService .patchUpdatePost (postId , postUpdateDto );
117- PostDetailDto updated = postService .getPostDetailById (postId , currentMemberId );
100+ PostDetailDto updated = postService .getPostDetailById (postId , AuthUtil . getAuthenticatedMemberId () );
118101 return ResponseEntity .ok (new ApiResponse <>(200 , "게시글이 수정되었습니다." , updated ));
119102 }
120103
121104 @ Operation (summary = "게시글 삭제" )
122105 @ DeleteMapping ("/{postId}" )
123106 public ResponseEntity <ApiResponse <Void >> deletePost (@ PathVariable Long postId ) {
124- Long currentMemberId = AuthUtil .getCurrentMemberId ();
125- String currentRole = AuthUtil .getCurrentMemberRole ();
126- PostDetailDto postDetail = postService .getPostDetailById (postId , currentMemberId );
107+ PostDetailDto postDetail = postService .getPostDetailById (postId , AuthUtil .getAuthenticatedMemberId ());
127108 Long postOwnerId = postDetail .getPost ().getMemberId ();
128- if (!postOwnerId .equals (currentMemberId ) && !"ADMIN" .equals (currentRole )) {
129- return ResponseEntity .status (403 ).body (new ApiResponse <>(403 , "본인 또는 관리자만 삭제 가능합니다." , null ));
130- }
109+ AuthUtil .validateOwnerOrAdmin (postOwnerId );
131110 postService .deletePost (postId );
132111 return ResponseEntity .ok (new ApiResponse <>(200 , "게시글이 삭제되었습니다." , null ));
133112 }
@@ -142,50 +121,23 @@ public ResponseEntity<ApiResponse<Void>> handleResponseStatusException(ResponseS
142121 @ Operation (summary = "본인 게시글 단일 조회" )
143122 @ GetMapping ("/my/{postId}" )
144123 public ResponseEntity <ApiResponse <PostDto >> getMyPostById (@ PathVariable Long postId ) {
145- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
146- Object principal = authentication .getPrincipal ();
147- Long memberId ;
148- if (principal instanceof org .springframework .security .core .userdetails .User user ) {
149- memberId = Long .valueOf (user .getUsername ());
150- } else if (principal instanceof Long ) {
151- memberId = (Long ) principal ;
152- } else {
153- throw new IllegalArgumentException ("올바른 회원 ID가 아닙니다" );
154- }
124+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
155125 PostDto postDto = postService .getMyPostById (postId , memberId );
156126 return ResponseEntity .ok (new ApiResponse <>(200 , "본인 게시글 단일 조회 성공" , postDto ));
157127 }
158128
159129 @ Operation (summary = "본인 게시글 전체 조회" )
160130 @ GetMapping ("/my" )
161131 public ResponseEntity <ApiResponse <List <PostDto >>> getMyPosts () {
162- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
163- Object principal = authentication .getPrincipal ();
164- Long memberId ;
165- if (principal instanceof org .springframework .security .core .userdetails .User user ) {
166- memberId = Long .valueOf (user .getUsername ());
167- } else if (principal instanceof Long ) {
168- memberId = (Long ) principal ;
169- } else {
170- throw new IllegalArgumentException ("올바른 회원 ID가 아닙니다" );
171- }
132+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
172133 List <PostDto > posts = postService .getMyPosts (memberId );
173134 return ResponseEntity .ok (new ApiResponse <>(200 , "본인 게시글 전체 조회 성공" , posts ));
174135 }
175136
176137 @ Operation (summary = "게시글+투표 동시 등록" )
177138 @ PostMapping ("/createPost" )
178139 public ResponseEntity <ApiResponse <PostDetailDto >> createPostWithPoll (@ RequestBody PostWithPollCreateDto dto ) {
179- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
180- Object principal = authentication .getPrincipal ();
181- Long memberId ;
182- if (principal instanceof org .springframework .security .core .userdetails .User user ) {
183- memberId = Long .valueOf (user .getUsername ());
184- } else if (principal instanceof Long ) {
185- memberId = (Long ) principal ;
186- } else {
187- throw new ResponseStatusException (org .springframework .http .HttpStatus .UNAUTHORIZED , "인증 정보가 올바르지 않습니다." );
188- }
140+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
189141 PostDetailDto result = postService .createPostWithPoll (dto , memberId );
190142 return ResponseEntity .ok (new ApiResponse <>(200 , "게시글+투표 등록 완료" , result ));
191143 }
@@ -271,4 +223,43 @@ public ResponseEntity<ApiResponse<PostDto>> getTopClosedPoll() {
271223 PostDto post = postService .getTopPollByStatus (PollDto .PollStatus .CLOSED , memberId );
272224 return ResponseEntity .ok (new ApiResponse <>(200 , "마감된 투표 Top 1 조회 성공" , post ));
273225 }
274- }
226+
227+ @ Operation (summary = "내가 참여한 진행중 투표 게시글 페이징 조회" )
228+ @ GetMapping ("/my/ongoingPaged" )
229+ public ResponseEntity <ApiResponse <PostPageDto >> getMyOngoingPostsPaged (
230+ @ RequestParam (defaultValue = "0" ) int page ,
231+ @ RequestParam (defaultValue = "10" ) int size
232+ ) {
233+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
234+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
235+ Page <PostDto > posts = postService .getMyOngoingPostsPaged (pageable , memberId );
236+ PostPageDto response = new PostPageDto (posts );
237+ return ResponseEntity .ok (new ApiResponse <>(200 , "내가 참여한 진행중 투표 게시글 페이징 조회 성공" , response ));
238+ }
239+
240+ @ Operation (summary = "내가 참여한 마감 투표 게시글 페이징 조회" )
241+ @ GetMapping ("/my/closedPaged" )
242+ public ResponseEntity <ApiResponse <PostPageDto >> getMyClosedPostsPaged (
243+ @ RequestParam (defaultValue = "0" ) int page ,
244+ @ RequestParam (defaultValue = "10" ) int size
245+ ) {
246+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
247+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
248+ Page <PostDto > posts = postService .getMyClosedPostsPaged (pageable , memberId );
249+ PostPageDto response = new PostPageDto (posts );
250+ return ResponseEntity .ok (new ApiResponse <>(200 , "내가 참여한 마감 투표 게시글 페이징 조회 성공" , response ));
251+ }
252+
253+ @ Operation (summary = "내가 참여한 모든 투표 게시글 페이징 조회" )
254+ @ GetMapping ("/my/votedPaged" )
255+ public ResponseEntity <ApiResponse <PostPageDto >> getMyVotedPostsPaged (
256+ @ RequestParam (defaultValue = "0" ) int page ,
257+ @ RequestParam (defaultValue = "10" ) int size
258+ ) {
259+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
260+ Long memberId = AuthUtil .getAuthenticatedMemberId ();
261+ Page <PostDto > posts = postService .getMyVotedPostsPaged (pageable , memberId );
262+ PostPageDto response = new PostPageDto (posts );
263+ return ResponseEntity .ok (new ApiResponse <>(200 , "내가 참여한 모든 투표 게시글 페이징 조회 성공" , response ));
264+ }
265+ }
0 commit comments