Skip to content

Commit e99f878

Browse files
committed
[Refactor]: 인증 객체에서 유저 정보 받아오도록 수정
1 parent e8e625d commit e99f878

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

back/src/main/java/com/back/domain/comment/controller/CommentController.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.back.domain.comment.service.CommentService;
77
import com.back.global.common.ApiResponse;
88
import com.back.global.common.PageResponse;
9+
import com.back.global.security.CustomUserDetails;
910
import io.swagger.v3.oas.annotations.Operation;
1011
import io.swagger.v3.oas.annotations.Parameter;
1112
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -16,6 +17,7 @@
1617
import org.springframework.data.domain.Pageable;
1718
import org.springframework.data.domain.Sort;
1819
import org.springframework.http.HttpStatus;
20+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1921
import org.springframework.web.bind.annotation.*;
2022

2123
/**
@@ -38,9 +40,9 @@ public ApiResponse<CommentResponse> createPost(
3840
)
3941
@RequestBody @Valid CommentRequest request,
4042
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable("postId") Long postId,
41-
@RequestParam Long userId
43+
@AuthenticationPrincipal CustomUserDetails cs
4244
) {
43-
CommentResponse response = commentService.createComment(userId, postId, request);
45+
CommentResponse response = commentService.createComment(cs.getUser().getId(), postId, request);
4446
return ApiResponse.success(response, "성공적으로 생성되었습니다.", HttpStatus.OK);
4547
}
4648

@@ -51,7 +53,7 @@ public ApiResponse<PageResponse<CommentResponse>> getPosts(
5153
@Parameter(description = "페이지 정보") Pageable pageable,
5254
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable("postId") Long postId,
5355
@Parameter(description = "정렬 조건 LATEST or LIKES") @RequestParam(defaultValue = "LATEST") CommentSortType sortType,
54-
@RequestParam Long userId) {
56+
@AuthenticationPrincipal CustomUserDetails cs) {
5557

5658
Sort sort = Sort.by(Sort.Direction.DESC, sortType.getProperty());
5759

@@ -61,7 +63,7 @@ public ApiResponse<PageResponse<CommentResponse>> getPosts(
6163
sort
6264
);
6365

64-
Page<CommentResponse> responses = commentService.getComments(userId, postId, sortedPageable);
66+
Page<CommentResponse> responses = commentService.getComments(cs.getUser().getId(), postId, sortedPageable);
6567
return ApiResponse.success(PageResponse.of(responses), "성공적으로 조회되었습니다.", HttpStatus.OK);
6668
}
6769

@@ -75,16 +77,16 @@ public ApiResponse<Long> updateComment(
7577
required = true
7678
)
7779
@RequestBody @Valid CommentRequest request,
78-
@RequestParam Long userId) {
79-
return ApiResponse.success(commentService.updateComment(userId, commentId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
80+
@AuthenticationPrincipal CustomUserDetails cs) {
81+
return ApiResponse.success(commentService.updateComment(cs.getUser().getId(), commentId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
8082
}
8183

8284
@DeleteMapping("/{commentId}")
8385
@Operation(summary = "댓글 삭제", description = "자신의 댓글을 삭제합니다.")
8486
public ApiResponse<Void> deletePost(
8587
@Parameter(description = "삭제할 댓글 ID", required = true) @PathVariable Long commentId,
86-
@RequestParam Long userId) {
87-
commentService.deleteComment(userId, commentId);
88+
@AuthenticationPrincipal CustomUserDetails cs) {
89+
commentService.deleteComment(cs.getUser().getId(), commentId);
8890
return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
8991
}
9092
}

back/src/main/java/com/back/domain/like/controller/LikeController.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.back.domain.like.service.LikeService;
44
import com.back.global.common.ApiResponse;
5+
import com.back.global.security.CustomUserDetails;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
810
import org.springframework.web.bind.annotation.*;
911

1012
/**
@@ -17,14 +19,14 @@ public class LikeController {
1719
private final LikeService likeService;
1820

1921
@PostMapping("/{postId}/likes")
20-
public ApiResponse<Void> addLike(@PathVariable Long postId, @RequestParam Long userId) {
21-
likeService.addLike(userId, postId);
22+
public ApiResponse<Void> addLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
23+
likeService.addLike(cs.getUser().getId(), postId);
2224
return ApiResponse.success(null, "좋아요 추가했습니다.", HttpStatus.OK);
2325
}
2426

2527
@DeleteMapping("/{postId}/likes")
26-
public ApiResponse<Void> removeLike(@PathVariable Long postId, @RequestParam Long userId) {
27-
likeService.removeLike(postId, userId);
28+
public ApiResponse<Void> removeLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
29+
likeService.removeLike(postId, cs.getUser().getId());
2830
return ApiResponse.success(null, "좋아요 취소하였습니다.", HttpStatus.OK);
2931
}
3032
}

back/src/main/java/com/back/domain/post/controller/PostController.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.back.domain.post.service.PostService;
88
import com.back.global.common.ApiResponse;
99
import com.back.global.common.PageResponse;
10+
import com.back.global.security.CustomUserDetails;
1011
import io.swagger.v3.oas.annotations.Operation;
1112
import io.swagger.v3.oas.annotations.Parameter;
1213
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -15,11 +16,11 @@
1516
import org.springframework.data.domain.Page;
1617
import org.springframework.data.domain.Pageable;
1718
import org.springframework.http.HttpStatus;
19+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1820
import 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

Comments
 (0)