diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java index ed66c4e..839b45f 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -1,6 +1,5 @@ package com.umc.DongnaeFriend.domain.dongnae.controller; -import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeCommentService; import lombok.RequiredArgsConstructor; @@ -44,9 +43,7 @@ public String postCommentLike(@PathVariable("commentId") Long commentId) { // [동네정보] 댓글 목록 조회 @GetMapping("") - public ResponseEntity getList(@RequestParam Long - townInformationId) { - return ResponseEntity.status(HttpStatus.OK).body(dongnaeCommentService.getList( - townInformationId)); + public ResponseEntity getList(@RequestParam Long townInformationId) { + return ResponseEntity.ok(dongnaeCommentService.getList(townInformationId)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java index 3bc307c..c3b4c2a 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java @@ -2,6 +2,7 @@ import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -36,4 +37,17 @@ public static CommentListResponse of(List list) { return new CommentListResponse(dongnaeCommentDtos); } } + + @Getter @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class ListResponse { + private String nickname; + + private String content; + + private String createdAt; + + private int likes; + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java index fc64cfb..99b5bdb 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java @@ -13,4 +13,6 @@ public interface DongnaeCommentLikeRepository extends JpaRepository { @Query(value = "SELECT dongnae_comment_like.* FROM dongnae_comment_like WHERE dongnae_comment_like.dongnae_comment_id = :comment_id", nativeQuery = true) DongnaeCommentLike findByCommentId(@Param("comment_id") DongnaeComment comment_id); + + int countAllByDongnaeCommentId(Long dongnae_comment_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java index bb3572f..4660cd2 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java @@ -25,7 +25,10 @@ public interface DongnaeCommentRepository extends JpaRepository findListByBoardId(@Param("dongnae_board_id") DongnaeBoard dongnae_board_id); + @Query(value = "SELECT dongnae_comment.*, COUNT(dongnae_comment_like.dongnae_comment_id) AS cnt FROM dongnae_comment\n" + + "LEFT JOIN dongnae_comment_like ON dongnae_comment.dongnae_comment_id = dongnae_comment_like.dongnae_comment_id\n" + + "WHERE dongnae_comment.dongnae_board_id = :dongnae_board_id\n" + + "GROUP BY dongnae_comment.dongnae_comment_id ORDER BY cnt DESC;", nativeQuery = true) + List findAllByBoardId(@Param("dongnae_board_id") DongnaeBoard dongnae_board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java index 264dea4..8ed34c2 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -1,6 +1,5 @@ package com.umc.DongnaeFriend.domain.dongnae.service; -import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; @@ -17,6 +16,9 @@ import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; + +import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime; @RequiredArgsConstructor @Service @@ -115,14 +117,25 @@ public String newLike(Long commentId) { return "동네정보 댓글 좋아요 삭제 성공"; } - public DongnaeCommentDto.CommentListResponse getList(Long id) { + public List getList(Long id) { // 게시판 가져오기 DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(id); - List list = dongnaeCommentRepository.findListByBoardId(dongnaeBoard); - return DongnaeCommentDto.CommentListResponse.of(list); + List list = dongnaeCommentRepository.findAllByBoardId(dongnaeBoard); + return getListResponse(list); + + } + private List getListResponse(List dongnaeCommentList) { + return dongnaeCommentList.stream() + .map(origin -> DongnaeCommentDto.ListResponse.builder() + .nickname(origin.getUser().getNickname()) + .content(origin.getContent()) + .createdAt(getTime(origin.getCreatedAt())) + .likes(dongnaeCommentLikeRepository.countAllByDongnaeCommentId(origin.getId())) + .build()) + .collect(Collectors.toList()); } public User findUser() {