Skip to content

Commit bae6543

Browse files
committed
[Refactor]: 내 댓글, 좋아요 여부 응답 추가
1 parent c26e0d1 commit bae6543

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public ResponseEntity<CommentResponse> createPost(
4444
return ResponseEntity.status(HttpStatus.CREATED).body(response);
4545
}
4646

47-
// fixme 게시글 목록 조회 - 정렬 조건 최신순, 좋아요순 추가하였는데 변경될 수 있음
4847
@GetMapping
4948
@Operation(summary = "댓글 목록 조회", description = "게시글 목록을 조회합니다.")
5049
public ResponseEntity<PageResponse<CommentResponse>> getPosts(

back/src/main/java/com/back/domain/comment/mapper/CommentMappers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public final class CommentMappers {
2323

2424
private CommentMappers() {}
2525

26-
public static final Mapper<Comment, CommentResponse> COMMENT_READ = e -> {
26+
public static CommentResponse toCommentResponse(Comment e, User user, boolean liked) {
2727
if (e == null) throw new MappingException("Comment is null");
2828
return new CommentResponse(
2929
e.getId(),
3030
e.isHide() ? "익명" : (e.getUser() != null ? e.getUser().getNickname() : null),
3131
e.getContent(),
3232
e.getLikeCount(),
33-
false, // todo : 내가 쓴 댓글 여부 추후 구현
34-
false, // todo : 좋아요 여부 추후 구현
33+
(user != null && user.getId().equals(e.getUser().getId())),
34+
liked,
3535
e.getCreatedDate()
3636
);
37-
};
37+
}
3838

3939
public static final class CommentCtxMapper implements TwoWayMapper<CommentRequest, Comment, CommentResponse> {
4040
private final User user;
@@ -58,7 +58,7 @@ public Comment toEntity(CommentRequest req) {
5858

5959
@Override
6060
public CommentResponse toResponse(Comment entity) {
61-
return COMMENT_READ.map(entity);
61+
return toCommentResponse(entity, user, false);
6262
}
6363
}
6464
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.back.domain.comment.entity.Comment;
66
import com.back.domain.comment.mapper.CommentMappers;
77
import com.back.domain.comment.repository.CommentRepository;
8+
import com.back.domain.like.repository.CommentLikeRepository;
9+
import com.back.domain.post.dto.PostSummaryResponse;
810
import com.back.domain.post.entity.Post;
911
import com.back.domain.post.repository.PostRepository;
1012
import com.back.domain.user.entity.User;
@@ -17,6 +19,9 @@
1719
import org.springframework.stereotype.Service;
1820
import org.springframework.transaction.annotation.Transactional;
1921

22+
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
2025
/**
2126
* 댓글 관련 비즈니스 로직을 처리하는 서비스.
2227
*/
@@ -28,6 +33,7 @@ public class CommentService {
2833
private final UserRepository userRepository;
2934
private final PostRepository postRepository;
3035
private final CommentRepository commentRepository;
36+
private final CommentLikeRepository commentLikeRepository;
3137

3238
public CommentResponse createComment(Long userId, Long postId, CommentRequest request) {
3339
User user = userRepository.findById(userId)
@@ -41,10 +47,18 @@ public CommentResponse createComment(Long userId, Long postId, CommentRequest re
4147
}
4248

4349
public Page<CommentResponse> getComments(Long userId, Long postId, Pageable pageable) {
50+
User user = userRepository.findById(userId)
51+
.orElse(null);
4452
Post post = postRepository.findById(postId)
4553
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
4654
Page<Comment> commentsPage = commentRepository.findCommentsByPostId(postId, pageable);
47-
return commentsPage.map(CommentMappers.COMMENT_READ::map);
55+
Set<Long> userLikedComments = getUserLikedComments(userId, commentsPage);
56+
57+
return commentsPage.map(comment -> CommentMappers.toCommentResponse(
58+
comment,
59+
user,
60+
userLikedComments.contains(comment.getId())
61+
));
4862
}
4963

5064
@Transactional
@@ -63,4 +77,14 @@ public void deleteComment(Long userId, Long commentId) {
6377
comment.checkUser(userId);
6478
commentRepository.delete(comment);
6579
}
80+
81+
// 특정 사용자가 한 게시글 내 댓글에서 좋아요를 누른 댓글 ID 집합 조회
82+
private Set<Long> getUserLikedComments(Long userId, Page<Comment> comments) {
83+
Set<Long> commentIds = comments.getContent()
84+
.stream()
85+
.map(Comment::getId)
86+
.collect(Collectors.toSet());
87+
88+
return commentLikeRepository.findLikedCommentsIdsByUserAndCommentIds(userId, commentIds);
89+
}
6690
}

back/src/main/java/com/back/domain/like/repository/CommentLikeRepository.java

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

33
import com.back.domain.like.entity.CommentLike;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
57
import org.springframework.stereotype.Repository;
68

9+
import java.util.Set;
10+
711
/**
812
* 댓글 좋아요 엔티티에 대한 데이터베이스 접근을 담당하는 JpaRepository.
913
*/
1014
@Repository
1115
public interface CommentLikeRepository extends JpaRepository<CommentLike, Long> {
16+
@Query("SELECT cl.comment.id FROM CommentLike cl WHERE cl.user.id = :userId AND cl.comment.id IN :commentIds")
17+
Set<Long> findLikedCommentsIdsByUserAndCommentIds(@Param("userId") Long userId, @Param("commentIds") Set<Long> commentIds);
1218
}

0 commit comments

Comments
 (0)