Skip to content

Commit

Permalink
fix: 게시글 상세 조회 시 로그인하지 않은 사용자 예외 처리 강화
Browse files Browse the repository at this point in the history
- 좋아요 여부를 직접적으로 반환받을 때 로그인 하지 않은 경우 member가 NULL이라 오류 발생
- 미리 좋아요 여부를 false로 지정해두고 null이 아닐 때만 DB에 접근하게 변경
  • Loading branch information
yeonjae02 committed Aug 1, 2024
1 parent 279aa99 commit ff82412
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public ResponseEntity<?> getAllPosts(Authentication authentication) {
@GetMapping("/{postId}")
public ResponseEntity<?> getPost(@PathVariable Long postId, Authentication authentication) {
try {
Member member = null;
Member read_member = null;

if (!(authentication == null || !authentication.isAuthenticated())) {
String memberEmail = authentication.getName();
member = memberService.getMemberByEmail(memberEmail);
read_member = memberService.getMemberByEmail(memberEmail);
}

PostDetailDto postDetailDto = postService.getPostById(postId, member);
PostDetailDto postDetailDto = postService.getPostById(postId, read_member);
return ResponseEntity.ok(postDetailDto);
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("해당 게시글이 존재하지 않습니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PostService {
private final PostHashtagRepository postHashtagsRepository;

@Transactional(readOnly = true)
public PostDetailDto getPostById(Long postId, Member member) {
public PostDetailDto getPostById(Long postId, Member read_member) {
Post post = postRepository.findById(postId).orElseThrow(EntityNotFoundException::new);
String memberEmail = post.getMember().getEmail();

Expand All @@ -52,6 +52,11 @@ public PostDetailDto getPostById(Long postId, Member member) {

int likeCnt = likesRepository.countByPostIdAndIsLiked(postId);

Boolean likes = false;
if (read_member != null) {
likes = likesRepository.isMemberLikesPostByEmail(post.getId(), read_member.getEmail());
}

return new PostDetailDto(
memberProfileResponseDto.getNickname(),
memberProfileResponseDto.getProfileImageUrl(),
Expand All @@ -61,7 +66,7 @@ public PostDetailDto getPostById(Long postId, Member member) {
hashtags,
images,
likeCnt,
likesRepository.isMemberLikesPostByEmail(post.getId(), member.getEmail())
likes
);
}

Expand Down

0 comments on commit ff82412

Please sign in to comment.