77import com .back .domain .post .service .PostService ;
88import com .back .global .common .ApiResponse ;
99import com .back .global .common .PageResponse ;
10+ import com .back .global .security .CustomUserDetails ;
1011import io .swagger .v3 .oas .annotations .Operation ;
1112import io .swagger .v3 .oas .annotations .Parameter ;
1213import io .swagger .v3 .oas .annotations .tags .Tag ;
1516import org .springframework .data .domain .Page ;
1617import org .springframework .data .domain .Pageable ;
1718import org .springframework .http .HttpStatus ;
19+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
1820import org .springframework .web .bind .annotation .*;
1921
2022/**
2123 * 게시글 관련 API 요청을 처리하는 컨트롤러.
22- * TODO UserId는 추후 인증/인가 기능이 추가되면 인증 객체에서 추출하도록 변경할 예정.
2324 */
2425@ Tag (name = "Post" , description = "게시글 관련 API" )
2526@ RestController
@@ -38,9 +39,9 @@ public ApiResponse<PostDetailResponse> createPost(
3839 required = true
3940 )
4041 @ RequestBody @ Valid PostRequest request ,
41- @ RequestParam Long userId
42+ @ AuthenticationPrincipal CustomUserDetails cs
4243 ) {
43- PostDetailResponse response = postService .createPost (userId , request );
44+ PostDetailResponse response = postService .createPost (cs . getUser (). getId () , request );
4445 return ApiResponse .success (response , "성공적으로 생성되었습니다." , HttpStatus .OK );
4546 }
4647
@@ -50,8 +51,8 @@ public ApiResponse<PostDetailResponse> createPost(
5051 public ApiResponse <PageResponse <PostSummaryResponse >> getPosts (
5152 @ Parameter (description = "검색 조건" ) @ ModelAttribute PostSearchCondition condition ,
5253 @ Parameter (description = "페이지 정보" ) Pageable pageable ,
53- @ RequestParam Long userId ) {
54- Page <PostSummaryResponse > responses = postService .getPosts (userId , condition , pageable );
54+ @ AuthenticationPrincipal CustomUserDetails cs ) {
55+ Page <PostSummaryResponse > responses = postService .getPosts (cs . getUser (). getId () , condition , pageable );
5556 return ApiResponse .success (PageResponse .of (responses ), "성공적으로 조회되었습니다." , HttpStatus .OK );
5657 }
5758
@@ -60,8 +61,8 @@ public ApiResponse<PageResponse<PostSummaryResponse>> getPosts(
6061 @ Operation (summary = "게시글 상세 조회" , description = "게시글 ID로 게시글을 조회합니다." )
6162 public ApiResponse <PostDetailResponse > getPost (
6263 @ Parameter (description = "조회할 게시글 ID" , required = true ) @ PathVariable Long postId ,
63- @ RequestParam Long userId ) {
64- return ApiResponse .success (postService .getPost (userId , postId ), "성공적으로 조회되었습니다." , HttpStatus .OK );
64+ @ AuthenticationPrincipal CustomUserDetails cs ) {
65+ return ApiResponse .success (postService .getPost (cs . getUser (). getId () , postId ), "성공적으로 조회되었습니다." , HttpStatus .OK );
6566 }
6667
6768 @ PutMapping ("/{postId}" )
@@ -73,16 +74,16 @@ public ApiResponse<Long> updatePost(
7374 required = true
7475 )
7576 @ RequestBody @ Valid PostRequest request ,
76- @ RequestParam Long userId ) {
77- return ApiResponse .success (postService .updatePost (userId , postId , request ), "성공적으로 수정되었습니다." , HttpStatus .OK );
77+ @ AuthenticationPrincipal CustomUserDetails cs ) {
78+ return ApiResponse .success (postService .updatePost (cs . getUser (). getId () , postId , request ), "성공적으로 수정되었습니다." , HttpStatus .OK );
7879 }
7980
8081 @ DeleteMapping ("/{postId}" )
8182 @ Operation (summary = "게시글 삭제" , description = "게시글 ID로 게시글을 삭제합니다." )
8283 public ApiResponse <Void > deletePost (
8384 @ Parameter (description = "삭제할 게시글 ID" , required = true ) @ PathVariable Long postId ,
84- @ RequestParam Long userId ) {
85- postService .deletePost (userId , postId );
85+ @ AuthenticationPrincipal CustomUserDetails cs ) {
86+ postService .deletePost (cs . getUser (). getId () , postId );
8687 return ApiResponse .success (null , "성공적으로 삭제되었습니다." , HttpStatus .OK );
8788 }
8889}
0 commit comments