Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 댓글 조회 API #131

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.example.stepperbackend.converter;

import com.example.stepperbackend.domain.BadgeCategory;
import com.example.stepperbackend.domain.Comment;
import com.example.stepperbackend.domain.Member;
import com.example.stepperbackend.domain.Post;
import com.example.stepperbackend.domain.mapping.Badge;
import com.example.stepperbackend.web.dto.BadgeDto;
import com.example.stepperbackend.web.dto.CommentDto;

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

public class CommentConverter {

Expand All @@ -31,6 +36,32 @@ public static CommentDto.CommentResponseDto toDto(Comment comment) {
.build();
}

public static CommentDto.ReplyResponseDto toReplyDto(Comment comment) {
return CommentDto.ReplyResponseDto.builder()
.postId(comment.getPost().getId())
.parentCommentId(comment.getParentComment().getId())
.content(comment.getContent())
.anonymous(comment.isAnonymous())
.build();

}

public static CommentDto.CommentResponseDto toCommentResponseDto(List<Comment> replyList, Comment comment){

List<CommentDto.ReplyResponseDto> replyDtoList = replyList.stream()
.map(CommentConverter::toReplyDto).collect(Collectors.toList());

return CommentDto.CommentResponseDto.builder()
.postId(comment.getPost().getId())
.commentId(comment.getId())
.memberName(comment.getAnonymousName())
.profileImage(comment.getMember().getProfileImage())
.content(comment.getContent())
.dateTime(comment.getCreatedAt())
.replyList(replyDtoList)
.build();
}

public static Comment toReplyEntity(CommentDto.ReplyRequestDto dto, Member member, Post post, String memberName, Comment parentComment) {
return Comment.builder()
.content(dto.getContent())
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/example/stepperbackend/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "post_id")
private Post post;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_comment_id")
private Comment parentComment;

@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL)
private List<Comment> replies = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByMember(Member member);

@Query("SELECT e FROM Comment e WHERE e.post.id = :postId")
List<Comment> findByPostId(@Param("postId") Long postId);
List<Comment> findAllByPostId(@Param("postId") Long postId);

// 특정 댓글의 대댓글들을 조회하는 메소드
@Query("SELECT e FROM Comment e WHERE e.parentComment = :parentComment")
List<Comment> findByParentComment(Comment parentComment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public interface CommentService {
List<CommentDto.CommentResponseDto> getComment(Long postId);

CommentDto.CommentResponseDto writeReply(CommentDto.ReplyRequestDto request, String email);

List<CommentDto.CommentResponseDto> getReply(Long parentCommentId);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.stepperbackend.service.CommentService;

import com.example.stepperbackend.apiPayload.code.status.ErrorStatus;
import com.example.stepperbackend.apiPayload.exception.handler.BadgeHandler;
import com.example.stepperbackend.apiPayload.exception.handler.CommentHandler;
import com.example.stepperbackend.apiPayload.exception.handler.MemberHandler;
import com.example.stepperbackend.apiPayload.exception.handler.PostHandler;
import com.example.stepperbackend.converter.BadgeConverter;
import com.example.stepperbackend.converter.CommentConverter;
import com.example.stepperbackend.converter.PostConverter;
import com.example.stepperbackend.domain.Comment;
Expand All @@ -12,6 +14,7 @@
import com.example.stepperbackend.repository.CommentRepository;
import com.example.stepperbackend.repository.MemberRepository;
import com.example.stepperbackend.repository.PostRepository;
import com.example.stepperbackend.web.dto.BadgeDto;
import com.example.stepperbackend.web.dto.CommentDto;
import com.example.stepperbackend.web.dto.PostDto;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -112,21 +115,16 @@ private String getAnonymousName(List<Comment> commentList, Long postId) {
@Override
public List<CommentDto.CommentResponseDto> getComment(Long postId) {

List<Comment> commentList = commentRepository.findByPostId(postId);
List<Comment> commentList = commentRepository.findAllByPostId(postId).stream().collect(Collectors.toList());

if (commentList.isEmpty()) {
throw new CommentHandler(ErrorStatus.COMMENTS_NOT_FOUND);
}

return commentList.stream().map(CommentConverter::toDto).collect(Collectors.toList());
}
List<CommentDto.CommentResponseDto> commentResponseList = commentList.stream()
.map(comment -> {
List<Comment> replyList = commentRepository.findByParentComment(comment);
comment.setReplies(replyList);
return CommentConverter.toCommentResponseDto(replyList, comment);
}).collect(Collectors.toList());

@Override
public List<CommentDto.CommentResponseDto> getReply(Long parentCommentId) {
Comment parentComment = commentRepository.findById(parentCommentId)
.orElseThrow(() -> new RuntimeException("Parent comment not found"));
List<Comment> replyList = commentRepository.findByParentComment(parentComment);
return replyList.stream().map(CommentConverter::toDto).collect(Collectors.toList());
return commentResponseList;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ public ApiResponse<List<CommentDto.CommentResponseDto>> getComment(@PathVariable
return ApiResponse.onSuccess(response);
}

@Operation(summary = "대댓글 조회 API", description = "대댓글 조회")
@GetMapping("/{parentCommentId}/reply")
public ResponseEntity<List<CommentDto.CommentResponseDto>> getReply(@PathVariable Long parentCommentId) {
List<CommentDto.CommentResponseDto> reply = commentService.getReply(parentCommentId);
return ResponseEntity.ok(reply);
}


@Operation(summary = "대댓글 작성 API", description = "대댓글 작성")
@PostMapping("/reply")
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/example/stepperbackend/web/dto/CommentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

public class CommentDto {

Expand All @@ -28,6 +29,7 @@ public static class CommentResponseDto {
private String memberName;
private String content;
private LocalDateTime dateTime;
private List<ReplyResponseDto> replyList;
}

@Getter
Expand All @@ -38,4 +40,12 @@ public static class ReplyRequestDto {
private boolean anonymous;
}

@Builder
@Getter
public static class ReplyResponseDto {
private Long postId;
private Long parentCommentId;
private String content;
private boolean anonymous;
}
}
Loading