Skip to content

Commit

Permalink
merge : 댓글 단건 조회 API 구현 #194
Browse files Browse the repository at this point in the history
Feature/#194 댓글 단건 조회 api 구현
  • Loading branch information
java-saeng authored Aug 3, 2023
2 parents 7c07c61 + 8756f16 commit 6e00d3f
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 139 deletions.
4 changes: 2 additions & 2 deletions backend/emm-sale/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= EMM-SALE API Docs
= Kerdy API Docs
:doctype: book
:icons: font
:source-highlighter: highlightjs
Expand Down Expand Up @@ -229,7 +229,7 @@ include::{snippets}/get-comment/http-response.adoc[]
.HTTP response 설명
include::{snippets}/get-comment/response-fields.adoc[]

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

.HTTP request 설명
include::{snippets}/get-children-comment/path-parameters.adoc[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public List<CommentHierarchyResponse> findAll(@RequestParam final Long eventId)
return commentQueryService.findAllCommentsByEventId(eventId);
}

@GetMapping("/comments/{comment-id}/children")
public List<CommentResponse> findChildren(
@GetMapping("/comments/{comment-id}")
public CommentHierarchyResponse findParentWithChildren(
@PathVariable("comment-id") final Long commentId
) {
return commentQueryService.findChildrenComments(commentId);
return commentQueryService.findParentWithChildren(commentId);
}

@DeleteMapping("/comments/{comment-id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.emmsale.comment.application;

import com.emmsale.base.BaseEntity;
import com.emmsale.comment.application.dto.CommentHierarchyResponse;
import com.emmsale.comment.application.dto.CommentResponse;
import com.emmsale.comment.domain.Comment;
import com.emmsale.comment.domain.CommentRepository;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,16 +19,14 @@ public List<CommentHierarchyResponse> findAllCommentsByEventId(final Long eventI

final List<Comment> comments = commentRepository.findByEventId(eventId);

return CommentHierarchyResponse.from(comments);
return CommentHierarchyResponse.convertAllFrom(comments);
}

public List<CommentResponse> findChildrenComments(final Long parentId) {
public CommentHierarchyResponse findParentWithChildren(final Long commentId) {

final List<Comment> childrenComments = commentRepository.findByParentId(parentId);
final List<Comment> parentWithChildrenComments
= commentRepository.findParentAndChildrenByParentId(commentId);

return childrenComments.stream()
.sorted(Comparator.comparing(BaseEntity::getCreatedAt))
.map(CommentResponse::from)
.collect(Collectors.toList());
return CommentHierarchyResponse.from(parentWithChildrenComments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.emmsale.base.BaseEntity;
import com.emmsale.comment.domain.Comment;
import com.emmsale.comment.exception.CommentException;
import com.emmsale.comment.exception.CommentExceptionType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
Expand All @@ -19,7 +21,22 @@ public class CommentHierarchyResponse {
private final CommentResponse parentComment;
private final List<CommentResponse> childComments;

public static List<CommentHierarchyResponse> from(final List<Comment> comments) {
public static CommentHierarchyResponse from(final List<Comment> comments) {
final List<CommentResponse> childComments = comments.stream()
.filter(Comment::isChild)
.map(CommentResponse::from)
.collect(Collectors.toList());

final CommentResponse parentComment = comments.stream()
.filter(Comment::isRoot)
.map(CommentResponse::from)
.findFirst()
.orElseThrow(() -> new CommentException(CommentExceptionType.NOT_FOUND_COMMENT));

return new CommentHierarchyResponse(parentComment, childComments);
}

public static List<CommentHierarchyResponse> convertAllFrom(final List<Comment> comments) {

final Map<Comment, List<Comment>> groupedByParent =
groupingByParentAndSortedByCreatedAt(comments);
Expand All @@ -39,7 +56,7 @@ public static List<CommentHierarchyResponse> from(final List<Comment> comments)
return result;
}

private static LinkedHashMap<Comment, List<Comment>> groupingByParentAndSortedByCreatedAt(
private static Map<Comment, List<Comment>> groupingByParentAndSortedByCreatedAt(
final List<Comment> comments
) {
return comments.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void modify(final String content) {
this.content = content;
}

public boolean isRoot() {
return parent == null;
}

public boolean isChild() {
return parent != null;
}

public Optional<Comment> getParent() {
return Optional.ofNullable(parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query("select c From Comment c inner join Event e on c.event.id = e.id where e.id = :eventId")
List<Comment> findByEventId(@Param("eventId") final Long eventId);

List<Comment> findByParentId(Long parentId);
@Query("select c1 From Comment c1 left outer join c1.parent p where c1.id=:id or p.id=:id")
List<Comment> findParentAndChildrenByParentId(@Param("id") Long commentId);
}
4 changes: 2 additions & 2 deletions backend/emm-sale/src/main/resources/http/comment.http
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Authorization: bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjkwMTk2OTY5L
GET http://localhost:8080/comments?eventId=1
Content-Type: application/json

### 부모 ID로 대댓글 조회하기
GET http://localhost:8080/comments/1/children
### 댓글 ID로 조회하기(대댓글 있을 경우 같이 조회)
GET http://localhost:8080/comments/1

### 댓글 삭제 /comments/{comment-id}

Expand Down
Loading

0 comments on commit 6e00d3f

Please sign in to comment.