Skip to content

Commit

Permalink
✨ Feat #71 : [동네정보] 댓글 목록 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Suanna01 committed Aug 20, 2023
1 parent 4032a74 commit cccf44e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto;
import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
Expand Down Expand Up @@ -40,4 +42,11 @@ public String postCommentLike(@PathVariable("commentId") Long commentId) {
return "";
}

// [동네정보] 댓글 목록 조회
@GetMapping("")
public ResponseEntity<DongnaeCommentDto.CommentListResponse> getList(@RequestParam Long
townInformationId) {
return ResponseEntity.status(HttpStatus.OK).body(dongnaeCommentService.getList(
townInformationId));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
package com.umc.DongnaeFriend.domain.dongnae.dto;

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

import java.util.List;
import java.util.stream.Collectors;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class DongnaeCommentDto {
Long parentCommentId;
String content;

public static DongnaeCommentDto from(DongnaeComment dongnaeComment) {
return new DongnaeCommentDto(
dongnaeComment.getContent()
);
}
@Getter
public static class CommentListResponse {
private List<DongnaeCommentDto> list;

public CommentListResponse(List<DongnaeCommentDto> list) {
this.list = list;
}

public static CommentListResponse of(List<DongnaeComment> list) {
List<DongnaeCommentDto> dongnaeCommentDtos = list
.stream()
.map(DongnaeCommentDto::from)
.collect(Collectors.toList());

return new CommentListResponse(dongnaeCommentDtos);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.DongnaeFriend.domain.dongnae.respository;

import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment;
import com.umc.DongnaeFriend.domain.user.entity.User;
Expand All @@ -23,4 +24,8 @@ 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
Expand All @@ -27,26 +28,26 @@ public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommen
// 게시판 가져오기
DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(townInformationId);

// 대댓글 등록
if (!(dongnaeCommentDto.getParentCommentId() == null)){
// 부모 댓글 가져오기
Optional<DongnaeComment> parentCommentOptional = dongnaeCommentRepository.findById(dongnaeCommentDto.getParentCommentId());
DongnaeComment parentComment = parentCommentOptional.get();

// 댓글 빌드
DongnaeComment comment = DongnaeComment.builder()
.parentComment(parentComment)
.content(dongnaeCommentDto.getContent())
.isDeleted(false)
.dongnaeBoard(dongnaeBoard)
.user(user)
.build();

dongnaeCommentRepository.save(comment);

return "대댓글 등록 성공";

}
// // 대댓글 등록
// if (!(dongnaeCommentDto.getParentCommentId() == null)){
// // 부모 댓글 가져오기
// Optional<DongnaeComment> parentCommentOptional = dongnaeCommentRepository.findById(dongnaeCommentDto.getParentCommentId());
// DongnaeComment parentComment = parentCommentOptional.get();
//
// // 댓글 빌드
// DongnaeComment comment = DongnaeComment.builder()
// .parentComment(parentComment)
// .content(dongnaeCommentDto.getContent())
// .isDeleted(false)
// .dongnaeBoard(dongnaeBoard)
// .user(user)
// .build();
//
// dongnaeCommentRepository.save(comment);
//
// return "대댓글 등록 성공";
//
// }

// 댓글 빌드
DongnaeComment comment = DongnaeComment.builder()
Expand Down Expand Up @@ -110,4 +111,14 @@ public String newLike(Long commentId) {

return "동네정보 댓글 좋아요 삭제 성공";
}

public DongnaeCommentDto.CommentListResponse getList(Long id) {

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

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

}
}

0 comments on commit cccf44e

Please sign in to comment.