Skip to content

Commit

Permalink
✅ Feat #71 : [동네정보] 댓글 목록 조회 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Suanna01 committed Aug 22, 2023
1 parent 90ca979 commit fd13ccc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,9 +43,7 @@ public String postCommentLike(@PathVariable("commentId") Long commentId) {

// [동네정보] 댓글 목록 조회
@GetMapping("")
public ResponseEntity<DongnaeCommentDto.CommentListResponse> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -36,4 +37,17 @@ public static CommentListResponse of(List<DongnaeComment> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
public interface DongnaeCommentLikeRepository extends JpaRepository<DongnaeCommentLike, Long> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public interface DongnaeCommentRepository extends JpaRepository<DongnaeComment,
@Query("SELECT db FROM DongnaeBoard db WHERE db.id = :dongnae_board_id")
DongnaeBoard findByDongnaeBoardId(@Param("dongnae_board_id") Long dongnae_board_id);

@Query(value = "SELECT dongnae_comment.* FROM dongnae_comment WHERE dongnae_comment.dongnae_board_id = :dongnae_board_id", nativeQuery = true)
List<DongnaeComment> 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<DongnaeComment> findAllByBoardId(@Param("dongnae_board_id") DongnaeBoard dongnae_board_id);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -115,14 +117,25 @@ public String newLike(Long commentId) {
return "동네정보 댓글 좋아요 삭제 성공";
}

public DongnaeCommentDto.CommentListResponse getList(Long id) {
public List<DongnaeCommentDto.ListResponse> getList(Long id) {

// 게시판 가져오기
DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(id);

List<DongnaeComment> list = dongnaeCommentRepository.findListByBoardId(dongnaeBoard);
return DongnaeCommentDto.CommentListResponse.of(list);
List<DongnaeComment> list = dongnaeCommentRepository.findAllByBoardId(dongnaeBoard);
return getListResponse(list);

}

private List<DongnaeCommentDto.ListResponse> getListResponse(List<DongnaeComment> 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() {
Expand Down

0 comments on commit fd13ccc

Please sign in to comment.