Skip to content

Commit fb62d9a

Browse files
committed
[Feat]: 게시글, 댓글 조회 모두 허용, 투표 게스트는 안되게 설정
1 parent b0b9c4f commit fb62d9a

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public ResponseEntity<PageResponse<CommentResponse>> getPosts(
6060
sort
6161
);
6262

63-
Page<CommentResponse> responses = commentService.getComments(cs.getUser().getId(), postId, sortedPageable);
63+
Long userId = (cs != null && cs.getUser() != null) ? cs.getUser().getId() : null;
64+
65+
Page<CommentResponse> responses = commentService.getComments(userId, postId, sortedPageable);
6466
return ResponseEntity.ok(PageResponse.of(responses));
6567
}
6668

back/src/main/java/com/back/domain/comment/service/CommentService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.stereotype.Service;
2020
import org.springframework.transaction.annotation.Transactional;
2121

22+
import java.util.Collections;
2223
import java.util.Set;
2324
import java.util.stream.Collectors;
2425

@@ -47,12 +48,17 @@ public CommentResponse createComment(Long userId, Long postId, CommentRequest re
4748
}
4849

4950
public Page<CommentResponse> getComments(Long userId, Long postId, Pageable pageable) {
50-
User user = userRepository.findById(userId)
51-
.orElse(null);
51+
User user = userId != null
52+
? userRepository.findById(userId).orElse(null)
53+
: null;
54+
5255
Post post = postRepository.findById(postId)
5356
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
5457
Page<Comment> commentsPage = commentRepository.findCommentsByPostId(postId, pageable);
55-
Set<Long> userLikedComments = getUserLikedComments(userId, commentsPage);
58+
59+
Set<Long> userLikedComments = userId != null
60+
? getUserLikedComments(userId, commentsPage)
61+
: Collections.emptySet();
5662

5763
return commentsPage.map(comment -> CommentMappers.toCommentResponse(
5864
comment,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public ResponseEntity<PageResponse<PostSummaryResponse>> getPosts(
5252
@Parameter(description = "검색 조건") @ModelAttribute PostSearchCondition condition,
5353
@Parameter(description = "페이지 정보") Pageable pageable,
5454
@AuthenticationPrincipal CustomUserDetails cs) {
55-
Page<PostSummaryResponse> responses = postService.getPosts(cs.getUser().getId(), condition, pageable);
55+
Long userId = (cs != null && cs.getUser() != null) ? cs.getUser().getId() : null;
56+
Page<PostSummaryResponse> responses = postService.getPosts(userId, condition, pageable);
5657
return ResponseEntity.ok(PageResponse.of(responses));
5758
}
5859

@@ -62,7 +63,8 @@ public ResponseEntity<PageResponse<PostSummaryResponse>> getPosts(
6263
public ResponseEntity<PostDetailResponse> getPost(
6364
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable Long postId,
6465
@AuthenticationPrincipal CustomUserDetails cs) {
65-
return ResponseEntity.ok(postService.getPost(cs.getUser().getId(), postId));
66+
Long userId = (cs != null && cs.getUser() != null) ? cs.getUser().getId() : null;
67+
return ResponseEntity.ok(postService.getPost(userId, postId));
6668
}
6769

6870
@PutMapping("/{postId}")

back/src/main/java/com/back/domain/post/mapper/PostMappers.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public PostDetailResponse toDetailResponse(Post post, Boolean isLiked) {
5858
}
5959

6060
public PostSummaryResponse toSummaryResponse(Post post, Boolean isLiked) {
61-
log.info("투표 내용 {}", post.getVoteContent());
6261
return new PostSummaryResponse(
6362
post.getId(),
6463
post.getTitle(),

back/src/main/java/com/back/domain/post/service/PostService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public PostDetailResponse getPost(Long userId, Long postId) {
5656
Post post = postRepository.findById(postId)
5757
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
5858

59-
boolean isLiked = postLikeRepository.existsByPostIdAndUserId(postId, userId);
59+
boolean isLiked = userId != null &&
60+
postLikeRepository.existsByPostIdAndUserId(postId, userId);
6061

6162
if (post.getCategory() == PostCategory.CHAT) {
6263
return postMappers.toDetailResponse(post, isLiked);
@@ -65,9 +66,11 @@ public PostDetailResponse getPost(Long userId, Long postId) {
6566
List<PollOptionResponse.VoteOption> options =
6667
pollConverter.fromPollOptionJson(post.getVoteContent()).options();
6768

68-
List<Integer> selected = pollVoteRepository.findByPostIdAndUserId(postId, userId)
69+
List<Integer> selected = userId != null
70+
? pollVoteRepository.findByPostIdAndUserId(postId, userId)
6971
.map(vote -> pollConverter.fromChoiceJson(vote.getChoiceJson()))
70-
.orElse(Collections.emptyList());
72+
.orElse(Collections.emptyList())
73+
: Collections.emptyList();
7174

7275
PollOptionResponse pollResponse = new PollOptionResponse(selected, options);
7376

@@ -77,7 +80,9 @@ public PostDetailResponse getPost(Long userId, Long postId) {
7780
public Page<PostSummaryResponse> getPosts(Long userId, PostSearchCondition condition, Pageable pageable) {
7881
Page<Post> posts = postRepository.searchPosts(condition, pageable);
7982

80-
Set<Long> likedPostIds = getUserLikedPostIds(userId, posts);
83+
Set<Long> likedPostIds = userId != null
84+
? getUserLikedPostIds(userId, posts)
85+
: Collections.emptySet();
8186

8287
return posts.map(post -> postMappers.toSummaryResponse(
8388
post,

back/src/main/java/com/back/global/security/SecurityConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.HttpMethod;
910
import org.springframework.http.HttpStatus;
1011
import org.springframework.security.authentication.AuthenticationManager;
1112
import org.springframework.security.config.Customizer;
@@ -53,6 +54,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5354
.authorizeHttpRequests(auth -> auth
5455
.requestMatchers("/h2-console/**").permitAll()
5556
.requestMatchers("/api/v1/users-auth/**", "/oauth2/**", "/login/oauth2/**").permitAll()
57+
.requestMatchers(HttpMethod.GET, "/api/v1/posts", "/api/v1/posts/**").permitAll()
58+
.requestMatchers(HttpMethod.GET, "/api/v1/posts/*/comments").permitAll()
59+
.requestMatchers("/api/v1/posts/*/polls").hasAnyRole("USER", "ADMIN")
5660
.requestMatchers("/admin/**").hasRole("ADMIN")
5761
.requestMatchers("/actuator/**").permitAll()
5862
.anyRequest().authenticated()

0 commit comments

Comments
 (0)