Skip to content

Commit

Permalink
merge : 작성한 댓글의 내용이 아닌 부모의 댓글이 조회되는 현상 해결 #415
Browse files Browse the repository at this point in the history
Fix/#415 작성한 댓글의 내용이 아닌 부모의 댓글이 조회되는 현상 해결
  • Loading branch information
java-saeng authored Aug 17, 2023
2 parents 583a5ec + cfd4f86 commit 55e2349
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 21 deletions.
8 changes: 4 additions & 4 deletions backend/emm-sale/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ include::{snippets}/delete-event/http-response.adoc[]
=== `GET` : 댓글 모두 조회

.HTTP request 설명
include::{snippets}/get-comment/request-parameters.adoc[]
include::{snippets}/get-comments/request-parameters.adoc[]

.HTTP request
include::{snippets}/get-comment/http-request.adoc[]
include::{snippets}/get-comments/http-request.adoc[]

.HTTP response
include::{snippets}/get-comment/http-response.adoc[]
include::{snippets}/get-comments/http-response.adoc[]

.HTTP response 설명
include::{snippets}/get-comment/response-fields.adoc[]
include::{snippets}/get-comments/response-fields.adoc[]

=== `GET` : 특정 댓글 조회(대댓글 있을 경우 같이 조회)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.emmsale.comment.application;

import static com.emmsale.comment.exception.CommentExceptionType.NOT_EVENT_AND_MEMBER_ID_BOTH_NULL;
import static com.emmsale.comment.exception.CommentExceptionType.NOT_FOUND_COMMENT;

import com.emmsale.block.domain.Block;
import com.emmsale.block.domain.BlockRepository;
Expand Down Expand Up @@ -55,8 +56,12 @@ public CommentHierarchyResponse findParentWithChildren(final Long commentId,

final List<Long> blockedMemberIds = getBlockedMemberIds(member);

final List<Comment> parentWithChildrenComments
= commentRepository.findParentAndChildrenByParentId(commentId);
final Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CommentException(NOT_FOUND_COMMENT));

final List<Comment> parentWithChildrenComments = comment.getParent()
.map(it -> commentRepository.findParentAndChildrenByParentId(it.getId()))
.orElseGet(() -> commentRepository.findParentAndChildrenByParentId(commentId));

return CommentHierarchyResponse.from(parentWithChildrenComments, blockedMemberIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UpdateNotificationEvent {
public static UpdateNotificationEvent from(final Comment comment) {
return new UpdateNotificationEvent(
comment.getMember().getId(),
comment.getParentIdOrSelfId(),
comment.getId(),
UPDATE_NOTIFICATION_COMMENT_TYPE,
LocalDateTime.now()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static UpdateNotificationResponse convertCommentNotification(
new CommentTypeNotification(
comment.getContent(),
comment.getEvent().getName(),
comment.getMember().getImageUrl()
comment.getMember().getImageUrl(),
comment.getParentIdOrSelfId(),
comment.getEvent().getId()
)
);
}
Expand All @@ -50,16 +52,18 @@ public static UpdateNotificationResponse convertEventNotification(
);
}

private boolean getIsRead() {
return isRead;
}

@RequiredArgsConstructor
@Getter
public static class CommentTypeNotification {

private final String content;
private final String eventName;
private final String commenterImageUrl;
private final Long parentId;
private final Long eventId;
}

private boolean getIsRead() {
return isRead;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ void test_findAll() throws Exception {
.header("Authorization", accessToken))
.andExpect(status().isOk())
.andDo(print())
.andDo(document("get-comment", requestParam, responseFieldsSnippet));
.andDo(document("get-comments", requestParam, responseFieldsSnippet));
}

@Test
@DisplayName("findParentWithChildren() : 부모, 자식 댓글들이 성공적으로 조회되면 200 OK 를 반환할 수 있다.")
void test_findChildren() throws Exception {
//given
final PathParametersSnippet pathParams = pathParameters(
parameterWithName("comment-id").description("부모 ID")
parameterWithName("comment-id").description("댓글 ID")
);

final ResponseFieldsSnippet responseFields = responseFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ void test_create_child_not_notification() throws Exception {
}

/**
* 1 ㄴ2 (1에게 알림) ㄴ3 (1,2에게 알림)
* 1
* ㄴ2 (1에게 알림)
* ㄴ3 (1,2에게 알림)
* <p>
* <p>
* 새로운 대댓글 ㄴ2 (1,3에게 알림)
* 새로운 대댓글
* ㄴ2 (1,3에게 알림)
*/
@Test
@DisplayName("create() : 자신이 작성한 댓글, 대댓글들 중에서 다른 사람이 댓글을 작성할 경우 알림이 온다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ void test_findAllComments_memberId() throws Exception {
}

@Test
@DisplayName("findParentWithChildren() : 부모 댓글에 있는 자식 댓글들을 모두 조회할 수 있다.")
void test_findChildrenComments() throws Exception {
@DisplayName("findParentWithChildren() : 부모 댓글이라면 해당 부모 댓글과 그 자식 댓글들을 모두 조회할 수 있다.")
void test_findChildrenComments_parentId() throws Exception {
//given
final Comment 자식댓글2 = commentRepository.save(
Comment.createChild(event, 부모_댓글1, member, "자식댓글2"));
Expand All @@ -155,6 +155,34 @@ void test_findChildrenComments() throws Exception {
.isEqualTo(expected);
}

@Test
@DisplayName("findParentWithChildren() : 자식 댓글이라면 해당 부모 댓글과 그 자식 댓글들을 모두 조회할 수 있다.")
void test_findChildrenComments_childId() throws Exception {
//given
final Comment 자식댓글2 = commentRepository.save(
Comment.createChild(event, 부모_댓글1, member, "자식댓글2"));
final Comment 자식댓글1 = commentRepository.save(
Comment.createChild(event, 부모_댓글1, member, "자식댓글1"));

final CommentHierarchyResponse expected =
new CommentHierarchyResponse(
CommentResponse.from(부모_댓글1),
List.of(
CommentResponse.from(자식댓글2),
CommentResponse.from(자식댓글1)
)
);

//when
final CommentHierarchyResponse actual =
commentQueryService.findParentWithChildren(자식댓글1.getId(), member);

//then
assertThat(actual)
.usingRecursiveComparison()
.isEqualTo(expected);
}

@Nested
@DisplayName("차단된 사용자의 댓글 조회 테스트")
class HideContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void test_find() throws Exception {
.optional(),
fieldWithPath("[].commentTypeNotification.eventName").description("(댓글 알림일 경우)이벤트 이름")
.optional(),
fieldWithPath("[].commentTypeNotification.parentId").description("(댓글 알림일 경우)부모 댓글 ID")
.optional(),
fieldWithPath("[].commentTypeNotification.eventId").description("(댓글 알림일 경우)이벤트 ID")
.optional(),
fieldWithPath("[].commentTypeNotification.commenterImageUrl").description(
"(댓글 알림일 경우) 댓글 작성자 이미지 Url")
.optional()
Expand All @@ -74,7 +78,9 @@ void test_find() throws Exception {
final CommentTypeNotification commentTypeNotification = new CommentTypeNotification(
"대댓글 내용",
"이벤트 이름",
"대댓글 단 사용자의 이미지 URL"
"대댓글 단 사용자의 이미지 URL",
3L,
5L
);

final List<UpdateNotificationResponse> responses = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ void test_findAll() throws Exception {
new CommentTypeNotification(
comment.getContent(),
comment.getEvent().getName(),
comment.getMember().getImageUrl()
comment.getMember().getImageUrl(),
comment.getParentIdOrSelfId(),
comment.getEvent().getId()
)
)
);
Expand Down

0 comments on commit 55e2349

Please sign in to comment.