Skip to content

Commit

Permalink
feat: (#129) 게시글 댓글 목록 조회 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
woo-chang committed Aug 1, 2023
1 parent dd56d26 commit 8ca946e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.votogether.domain.post.dto.response;

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.post.entity.comment.Comment;
import java.time.LocalDateTime;

public record CommentResponse(
Long id,
CommentMember member,
String content,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {

public static CommentResponse from(final Comment comment) {
return new CommentResponse(
comment.getId(),
CommentMember.from(comment.getMember()),
comment.getContent(),
comment.getCreatedAt(),
comment.getUpdatedAt()
);
}

record CommentMember(Long id, String nickname) {

public static CommentMember from(final Member member) {
return new CommentMember(member.getId(), member.getNickname());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.votogether.domain.member.entity.Member;
import com.votogether.domain.post.dto.request.CommentRegisterRequest;
import com.votogether.domain.post.dto.request.CommentUpdateRequest;
import com.votogether.domain.post.dto.response.CommentResponse;
import com.votogether.domain.post.entity.Post;
import com.votogether.domain.post.entity.comment.Comment;
import com.votogether.domain.post.exception.CommentExceptionType;
import com.votogether.domain.post.exception.PostExceptionType;
import com.votogether.domain.post.repository.CommentRepository;
import com.votogether.domain.post.repository.PostRepository;
import com.votogether.exception.NotFoundException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -38,6 +40,17 @@ public void createComment(
post.addComment(comment);
}

@Transactional(readOnly = true)
public List<CommentResponse> getComments(final Long postId) {
final Post post = postRepository.findById(postId)
.orElseThrow(() -> new NotFoundException(PostExceptionType.POST_NOT_FOUND));

return commentRepository.findAllByPostOrderByCreatedAtAsc(post)
.stream()
.map(CommentResponse::from)
.toList();
}

@Transactional
public void updateComment(
final Long postId,
Expand Down Expand Up @@ -68,4 +81,5 @@ public void deleteComment(final Long postId, final Long commentId, final Member

commentRepository.delete(comment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.votogether.domain.member.repository.MemberRepository;
import com.votogether.domain.post.dto.request.CommentRegisterRequest;
import com.votogether.domain.post.dto.request.CommentUpdateRequest;
import com.votogether.domain.post.dto.response.CommentResponse;
import com.votogether.domain.post.entity.Post;
import com.votogether.domain.post.entity.PostBody;
import com.votogether.domain.post.entity.comment.Comment;
Expand All @@ -17,6 +18,7 @@
import com.votogether.exception.NotFoundException;
import com.votogether.fixtures.MemberFixtures;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -77,6 +79,56 @@ void createComment() {

}

@Nested
@DisplayName("게시글 댓글 목록 조회")
class GetComments {

@Test
@DisplayName("존재하지 않는 게시글이라면 예외를 던진다.")
void emptyPost() {
// given, when, then
assertThatThrownBy(() -> postCommentService.getComments(-1L))
.isInstanceOf(NotFoundException.class)
.hasMessage("해당 게시글이 존재하지 않습니다.");
}

@Test
@DisplayName("게시글 댓글 목록을 조회한다.")
void getComments() {
// given
Member member = memberRepository.save(MemberFixtures.MALE_20.get());
Post post = postRepository.save(
Post.builder()
.writer(member)
.postBody(PostBody.builder().title("titleA").content("contentA").build())
.deadline(LocalDateTime.of(2100, 7, 12, 0, 0))
.build()
);
Comment commentA = commentRepository.save(
Comment.builder()
.member(member)
.post(post)
.content("commentA")
.build()
);
Comment commentB = commentRepository.save(
Comment.builder()
.member(member)
.post(post)
.content("commentB")
.build()
);

// when
List<CommentResponse> response = postCommentService.getComments(post.getId());

// then
assertThat(response).usingRecursiveComparison()
.isEqualTo(List.of(CommentResponse.from(commentA), CommentResponse.from(commentB)));
}

}

@Nested
@DisplayName("게시글 댓글 수정")
class UpdateComment {
Expand Down

0 comments on commit 8ca946e

Please sign in to comment.