Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 비회원 게시글 조회 #200

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ public ResponseEntity<PostDetailResponse> getPostDetail(
@AuthenticationPrincipal CustomOAuth2User auth,
@PathVariable Long id) {

if (auth == null || auth.getId() == null) {
throw new GeneralException(ResponseCode.EMPTY_TOKEN);
String memberId = null; // 기본적으로 비회원일 경우 null로 설정
String role = null;

if (auth != null && auth.getId() != null) {
memberId = auth.getId();
role = auth.getAuthorities().stream()
.findFirst().orElseThrow(() -> new GeneralException(ResponseCode.FORBIDDEN))
.getAuthority(); // role 가져오기
}

PostDetailResponse response = postDetailService.getPostDetail(auth.getId(), id);
// 게시글 조회
PostDetailResponse response = postDetailService.getPostDetail(memberId, role, id);

// 조회수 증가
postDetailService.incrementViewCount(id);

return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class PostDetailService {
private final PostRepository postRepository;

@Transactional(readOnly = true)
public PostDetailResponse getPostDetail(String memberId, Long postId) {
public PostDetailResponse getPostDetail(String memberId, String role, Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new GeneralException(ResponseCode.RESOURCE_NOT_FOUND));
.orElseThrow(() -> new GeneralException(ResponseCode.RESOURCE_NOT_FOUND));

// 비공개 접근 권한 확인
if (!post.getIsVisible() && !post.getMember().getId().equals(memberId)) {
// 비공개 접근 권한 확인 - 비회원과 게스트는 비공개된 게시글에 접근 불가
if (!post.getIsVisible() && (memberId == null || !post.getMember().getId().equals(memberId))) {
throw new GeneralException(ResponseCode.FORBIDDEN);
}

Expand All @@ -42,8 +42,8 @@ public PostDetailResponse getPostDetail(String memberId, Long postId) {
// 작성자의 프로필 이미지 조회
String profileImage = post.getIsAnonymous() ? null : fileService.getProfileImgUrl(post.getWriterId());

// 본인 글 여부 확인
boolean isMyPost = post.getMember().getId().equals(memberId);
// 본인 글 여부 확인 - 회원(GENERAL)인 경우에만 확인
boolean isMyPost = "GENERAL".equals(role) && post.getMember().getId().equals(memberId);

// ResponseDto 생성
return toResponseDto(post, imageUrls, profileImage, isMyPost);
Expand All @@ -56,24 +56,25 @@ public void incrementViewCount(Long postId) {
}

// Dto 컨버터
private PostDetailResponse toResponseDto(Post post, List<String> imageUrls, String profileImageUrl, boolean isMyPost) {
private PostDetailResponse toResponseDto(Post post, List<String> imageUrls, String profileImageUrl,
boolean isMyPost) {
return PostDetailResponse.builder()
.id(post.getId())
.memberId(post.getMember().getId())
.title(post.getTitle())
.content(post.getContent())
.nickname(post.getIsAnonymous() ? "익명" : post.getMember().getNickname())
.profileImageUrl(profileImageUrl)
.postImageUrls(imageUrls)
.isAnonymous(post.getIsAnonymous())
.isVisible(post.getIsVisible())
.viewCount(post.getViewCount())
.likeCount(post.getLikeCount())
.commentCount(post.getCommentCount())
.communityCategory(post.getCommunityCategory())
.registeredAt(post.getRegisteredAt())
.updatedAt(post.getUpdatedAt())
.isMyPost(isMyPost) // 본인 글 여부 추가
.build();
.id(post.getId())
.memberId(post.getMember().getId())
.title(post.getTitle())
.content(post.getContent())
.nickname(post.getIsAnonymous() ? "익명" : post.getMember().getNickname())
.profileImageUrl(profileImageUrl)
.postImageUrls(imageUrls)
.isAnonymous(post.getIsAnonymous())
.isVisible(post.getIsVisible())
.viewCount(post.getViewCount())
.likeCount(post.getLikeCount())
.commentCount(post.getCommentCount())
.communityCategory(post.getCommunityCategory())
.registeredAt(post.getRegisteredAt())
.updatedAt(post.getUpdatedAt())
.isMyPost(isMyPost) // 본인 글 여부 추가
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public PostDetailServiceTest() {
void getPostDetail_SuccessfullyIncrementsViewCount() {
// Given
Long postId = 1L;
String memberId = "test-member-id";
String memberId = null; // 기본적으로 비회원일 경우 null로 설정
String role = null;

Member member = Member.builder().id(memberId).build();
Post post = Post.builder()
Expand All @@ -54,7 +55,7 @@ void getPostDetail_SuccessfullyIncrementsViewCount() {
when(postRepository.findById(postId)).thenReturn(Optional.of(post));

// Act
PostDetailResponse response = postDetailService.getPostDetail(memberId, postId);
PostDetailResponse response = postDetailService.getPostDetail(memberId, role, postId);

// Assert
assertNotNull(response);
Expand All @@ -67,6 +68,7 @@ void getPostDetail_SuccessForPrivatePostByOwner() {
// Given
Long postId = 2L;
String memberId = "test-member-id";
String role = "GENERAL";

Member member = Member.builder().id(memberId).build();
Post post = Post.builder()
Expand All @@ -81,7 +83,7 @@ void getPostDetail_SuccessForPrivatePostByOwner() {
when(postRepository.findById(postId)).thenReturn(Optional.of(post));

// Act
PostDetailResponse response = postDetailService.getPostDetail(memberId, postId);
PostDetailResponse response = postDetailService.getPostDetail(memberId, role, postId);

// Assert
assertNotNull(response);
Expand All @@ -94,6 +96,7 @@ void getPostDetail_ThrowsExceptionForUnauthorizedAccessToPrivatePost() {
// Given
Long postId = 3L;
String memberId = "unauthorized-user-id";
String role = "GENERAL";

Member owner = Member.builder().id("owner-id").build();
Post post = Post.builder()
Expand All @@ -110,7 +113,7 @@ void getPostDetail_ThrowsExceptionForUnauthorizedAccessToPrivatePost() {
// Act & Assert
GeneralException exception = assertThrows(
GeneralException.class,
() -> postDetailService.getPostDetail(memberId, postId)
() -> postDetailService.getPostDetail(memberId, role, postId)
);

assertEquals(ResponseCode.FORBIDDEN, exception.getErrorCode()); // 예외 코드 확인
Expand All @@ -123,13 +126,14 @@ void getPostDetail_ThrowsExceptionForNonExistentPost() {
// Given
Long postId = 4L;
String memberId = "test-member-id";
String role = "GENERAL";

when(postRepository.findById(postId)).thenReturn(Optional.empty());

// Act & Assert
GeneralException exception = assertThrows(
GeneralException.class,
() -> postDetailService.getPostDetail(memberId, postId)
() -> postDetailService.getPostDetail(memberId, role, postId)
);

assertEquals(ResponseCode.RESOURCE_NOT_FOUND, exception.getErrorCode()); // 예외 코드 확인
Expand Down