Skip to content

Commit

Permalink
Merge pull request #105 from DEPthes/refactor/#104-commint-logic
Browse files Browse the repository at this point in the history
[REFACTOR]: 댓글 관련 로직 수정
  • Loading branch information
HuniHuns authored Aug 18, 2024
2 parents 28e4540 + 03ffe26 commit 9d0192e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,65 @@ public class CommentService {
public SuccessResponse<List<CommentListRes>> getCommentList(Long postId){
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당하는 아이디의 게시글이 없습니다: " + postId));
List<Comment> commentList = commentRepository.findByPost(post);

List<Comment> commentList = commentRepository.findByPostAndParentCommentIsNull(post);
List<Comment> replyList = commentRepository.findByPostAndParentCommentIsNotNull(post);

List<CommentListRes> commentDetails = new ArrayList<>();

for(Comment comment : commentList) {
Avatar avatar;
if (comment.isUseNickname()) {
avatar = Avatar.builder()
.avatarFace(null)
.avatarBody(null)
.avatarEyes(null)
.avatarNose(null)
.avatarMouth(null)
.build();
} else {
Member member = comment.getMember();
avatar = Avatar.builder()
.avatarFace(member.getAvatarFace())
.avatarBody(member.getAvatarBody())
.avatarEyes(member.getAvatarEyes())
.avatarNose(member.getAvatarNose())
.avatarMouth(member.getAvatarMouth())
.build();
}
Avatar avatar = commentAvatar(comment);

CommentListRes commentListRes = CommentListRes.builder()
.commentId(comment.getId())
.avatar(avatar)
.nickname(comment.getNickname())
.createdDate(comment.getCreatedDate().toLocalDate())
.content(comment.getContent())
.replyList(new ArrayList<>()) // 대댓글 리스트 초기화
.build();

commentDetails.add(commentListRes);
}

if(comment.getParentComment() == null){
CommentListRes commentListRes = CommentListRes.builder()
.commentId(comment.getId())
.avatar(avatar)
.nickname(comment.getNickname())
.createdDate(comment.getCreatedDate().toLocalDate())
.content(comment.getContent())
.replyList(new ArrayList<>()) // 대댓글 리스트 초기화
.build();
for(Comment reply : replyList) {
Avatar avatar = commentAvatar(reply);

commentDetails.add(commentListRes);
} else {
ReplyListRes replyListRes = ReplyListRes.builder()
.commentId(comment.getId())
.parentCommentId(comment.getParentComment().getId())
.avatar(avatar)
.nickname(comment.getNickname())
.createdDate(comment.getCreatedDate().toLocalDate())
.content(comment.getContent())
.build();
ReplyListRes replyListRes = ReplyListRes.builder()
.commentId(reply.getId())
.parentCommentId(reply.getParentComment().getId())
.avatar(avatar)
.nickname(reply.getNickname())
.createdDate(reply.getCreatedDate().toLocalDate())
.content(reply.getContent())
.build();

commentDetails.stream()
.filter(parentComment -> parentComment.getCommentId().equals(comment.getParentComment().getId()))
.findFirst()
.ifPresent(parentComment -> parentComment.getReplyList().add(replyListRes));
}
commentDetails.stream()
.filter(parentComment -> parentComment.getCommentId().equals(reply.getParentComment().getId()))
.findFirst()
.ifPresent(parentComment -> parentComment.getReplyList().add(replyListRes));
}

return SuccessResponse.of(commentDetails);
}

private Avatar commentAvatar(Comment comment){
if(!comment.isUseNickname()) {
Member member = comment.getMember();
return Avatar.builder()
.avatarFace(member.getAvatarFace())
.avatarBody(member.getAvatarBody())
.avatarEyes(member.getAvatarEyes())
.avatarNose(member.getAvatarNose())
.avatarMouth(member.getAvatarMouth())
.build();
}
return Avatar.builder()
.avatarFace(null)
.avatarBody(null)
.avatarEyes(null)
.avatarNose(null)
.avatarMouth(null)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByPost(Post post);
// 댓글 목록 조히
List<Comment> findByPostAndParentCommentIsNull(Post post);

void deleteByPost(Post post);
// 대댓글 목록 조회
List<Comment> findByPostAndParentCommentIsNotNull(Post post);

// 대댓글 삭제
void deleteByPostAndParentCommentIsNotNull(Post post);

// 댓글 삭제
void deleteByPostAndParentCommentIsNull(Post post);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mvp.deplog.domain.comment.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
Expand All @@ -13,11 +14,13 @@ public class CreateCommentReq {
private Long postId;

@Schema(type = "String", example = "**댓글 내용**", description = "댓글 내용입니다.")
@NotBlank
private String content;

@Schema(type = "String", example = "닉네임", description = "닉네임입니다.")
@NotBlank
private String nickname;

@Schema(type = "boolean", example = "true", description = "닉네임 사용 체크박스 체크 여부입니다. 회원이 체크 안한 경우, 비회원의 경우 모두 true입니다. 회원이 누른 경우만 falseh입니다.")
@Schema(type = "boolean", example = "true", description = "닉네임 사용 체크박스 체크 여부입니다. 회원이 체크 안한 경우, 비회원의 경우 모두 true입니다. 회원이 누른 경우만 false입니다.")
private boolean useNicknameChecked;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import mvp.deplog.domain.comment.dto.request.CreateCommentReq;
import mvp.deplog.domain.comment.dto.response.CommentListRes;
import mvp.deplog.global.common.Message;
Expand Down Expand Up @@ -40,7 +41,7 @@ public interface CommentApi {
@PostMapping
ResponseEntity<SuccessResponse<Message>> createComment(
@Parameter(description = "Access Token을 입력해주세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails,
@Parameter(description = "Schemas의 CommentReq를 참고해주세요.", required = true) @RequestBody CreateCommentReq createCommentReq
@Parameter(description = "Schemas의 CommentReq를 참고해주세요.", required = true) @Valid @RequestBody CreateCommentReq createCommentReq
);

@Operation(summary = "댓글 목록 조회 API", description = "해당 게시글을 댓글 목록을 조회합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mvp.deplog.domain.comment.presentation;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import mvp.deplog.domain.comment.application.CommentService;
import mvp.deplog.domain.comment.application.CreateCommentService;
Expand All @@ -26,7 +27,8 @@ public class CommentController implements CommentApi {

@Override
@PostMapping
public ResponseEntity<SuccessResponse<Message>> createComment(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody CreateCommentReq createCommentReq) {
public ResponseEntity<SuccessResponse<Message>> createComment(@AuthenticationPrincipal UserDetailsImpl userDetails,
@Valid @RequestBody CreateCommentReq createCommentReq) {
CreateCommentService createCommentService = createCommentServiceFactory.find(userDetails, createCommentReq.getParentCommentId());
return ResponseEntity
.status(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ public SuccessResponse<Message> deletePost(Long memberId, Long postId) {
throw new UnauthorizedException("본인이 작성한 게시글이 아니므로 삭제할 수 없습니다.");
}

// 게시글 내 대댓글 삭제
commentRepository.deleteByPostAndParentCommentIsNotNull(post);

// 게시글 내 댓글 삭제
commentRepository.deleteByPost(post);
commentRepository.deleteByPostAndParentCommentIsNull(post);

// 게시글 내 태그 삭제
taggingRepository.deleteByPost(post);
Expand Down

0 comments on commit 9d0192e

Please sign in to comment.