diff --git a/backend/src/main/java/aimo/backend/domains/comment/controller/ParentCommentController.java b/backend/src/main/java/aimo/backend/domains/comment/controller/ParentCommentController.java index b1f3de7..c7a5710 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/controller/ParentCommentController.java +++ b/backend/src/main/java/aimo/backend/domains/comment/controller/ParentCommentController.java @@ -30,10 +30,11 @@ public class ParentCommentController { @PostMapping("/{postId}/comments") public ResponseEntity> saveParentComment( @Valid @PathVariable Long postId, - @Valid @RequestBody SaveParentCommentRequest request) { + @Valid @RequestBody SaveParentCommentRequest request + ) { Long memberId = MemberLoader.getMemberId(); - SaveParentCommentParameter parameter = - SaveParentCommentParameter.of(memberId, postId, request.content()); + + SaveParentCommentParameter parameter = SaveParentCommentParameter.of(memberId, postId, request.content()); parentCommentMemberService.saveParentComment(parameter); return ResponseEntity.ok(DataResponse.ok()); @@ -42,10 +43,15 @@ public ResponseEntity> saveParentComment( @PutMapping("comments/{commentId}") public ResponseEntity> updateParentComment( @Valid @PathVariable Long commentId, - @Valid @RequestBody UpdateParentCommentRequest request) { + @Valid @RequestBody UpdateParentCommentRequest request + ) { Long memberId = MemberLoader.getMemberId(); - UpdateParentCommentParameter parameter = - UpdateParentCommentParameter.of(memberId, commentId, request.content()); + + UpdateParentCommentParameter parameter = UpdateParentCommentParameter.of( + memberId, + commentId, + request.content()); + parentCommentMemberService.validateAndUpdateParentComment(parameter); return ResponseEntity.ok(DataResponse.ok()); @@ -56,7 +62,9 @@ public ResponseEntity> deleteParentComment( @Valid @PathVariable("commentId") Long commentId ) { Long memberId = MemberLoader.getMemberId(); + DeleteParentCommentParameter parameter = DeleteParentCommentParameter.of(memberId, commentId); + parentCommentMemberService.validateAndDeleteParentComment(parameter); return ResponseEntity.ok(DataResponse.ok()); diff --git a/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveParentCommentRequest.java b/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveParentCommentRequest.java index 6620bc9..b62dbfe 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveParentCommentRequest.java +++ b/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveParentCommentRequest.java @@ -7,7 +7,7 @@ public record SaveParentCommentRequest( String content ) { - public static SaveParentCommentRequest of(String content) { + public static SaveParentCommentRequest from(String content) { return new SaveParentCommentRequest(content); } } diff --git a/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateParentCommentRequest.java b/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateParentCommentRequest.java index b0c9e28..b9ec484 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateParentCommentRequest.java +++ b/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateParentCommentRequest.java @@ -7,7 +7,7 @@ public record UpdateParentCommentRequest( String content ) { - public static UpdateParentCommentRequest of(String content) { + public static UpdateParentCommentRequest from(String content) { return new UpdateParentCommentRequest(content); } } diff --git a/backend/src/main/java/aimo/backend/domains/comment/entity/ParentComment.java b/backend/src/main/java/aimo/backend/domains/comment/entity/ParentComment.java index e900c77..bb54e6b 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/entity/ParentComment.java +++ b/backend/src/main/java/aimo/backend/domains/comment/entity/ParentComment.java @@ -61,7 +61,6 @@ public Integer getLikesCount() { private ParentComment( String nickname, String content, - Boolean isDeleted, Member member, Post post, List childComments, @@ -69,7 +68,7 @@ private ParentComment( ) { this.nickname = nickname; this.content = content; - this.isDeleted = isDeleted; + this.isDeleted = false; this.member = member; this.post = post; this.childComments = childComments; @@ -84,18 +83,19 @@ public static ParentComment of( return ParentComment.builder() .nickname(member.getNickname()) .content(content) - .isDeleted(false) .member(member) .post(post) .build(); } + // 부모 댓글 이름 삭제 public void deleteParentCommentSoftly() { this.member = null; this.nickname = CommentConstants.UNKNOWN_MEMBER.getValue(); this.isDeleted = true; } + // 부모 댓글 이름, 내용 삭제 public void deleteParentCommentSoftlyWithContent() { this.member = null; this.nickname = CommentConstants.UNKNOWN_MEMBER.getValue(); @@ -103,10 +103,12 @@ public void deleteParentCommentSoftlyWithContent() { content = CommentConstants.DELETED_COMMENT.getValue(); } + // 부모 댓글 수정 public void updateContent(String content) { this.content = content; } + // 자식 댓글 삭제 public void deleteChildComment(Long childCommentId) { childComments.removeIf(childComment -> childComment.getId().equals(childCommentId)); } diff --git a/backend/src/main/java/aimo/backend/domains/comment/repository/ParentCommentRepository.java b/backend/src/main/java/aimo/backend/domains/comment/repository/ParentCommentRepository.java index 4b3d10a..bba20b7 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/repository/ParentCommentRepository.java +++ b/backend/src/main/java/aimo/backend/domains/comment/repository/ParentCommentRepository.java @@ -9,7 +9,7 @@ public interface ParentCommentRepository extends JpaRepository { - Boolean existsByIdAndMember(Long id, Member member); + Boolean existsByIdAndMember_Id(Long id, Long memberId); List findByMemberId(Long memberId); diff --git a/backend/src/main/java/aimo/backend/domains/comment/service/ParentCommentMemberService.java b/backend/src/main/java/aimo/backend/domains/comment/service/ParentCommentMemberService.java index 28fe3e2..5deb3d1 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/service/ParentCommentMemberService.java +++ b/backend/src/main/java/aimo/backend/domains/comment/service/ParentCommentMemberService.java @@ -13,6 +13,7 @@ import aimo.backend.domains.comment.repository.ParentCommentRepository; import aimo.backend.domains.member.entity.Member; import aimo.backend.domains.member.repository.MemberRepository; +import aimo.backend.domains.member.service.MemberService; import aimo.backend.domains.post.entity.Post; import aimo.backend.domains.post.service.PostService; import lombok.RequiredArgsConstructor; @@ -25,10 +26,11 @@ public class ParentCommentMemberService { private final PostService postService; private final ParentCommentRepository parentCommentRepository; private final MemberRepository memberRepository; + private final MemberService memberService; // 부모 댓글 권한 확인 - private void validateParentCommentAuthority(Member member, Long commentId) { - Boolean exists = parentCommentRepository.existsByIdAndMember(commentId, member); + private void validateParentCommentAuthority(Long memberId, Long commentId) { + Boolean exists = parentCommentRepository.existsByIdAndMember_Id(commentId, memberId); if (!exists) { throw ApiException.from(UNAUTHORIZED_PARENT_COMMENT); @@ -40,9 +42,11 @@ private void validateParentCommentAuthority(Member member, Long commentId) { public void saveParentComment(SaveParentCommentParameter parameter) { Long memberId = parameter.memberId(); Long postId = parameter.postId(); + Member member = memberRepository.findById(memberId) .orElseThrow(() -> ApiException.from(MEMBER_NOT_FOUND)); Post post = postService.findById(postId); + ParentComment parentComment = ParentComment.of(member, post, parameter.content()); parentCommentRepository.save(parentComment); @@ -51,11 +55,12 @@ public void saveParentComment(SaveParentCommentParameter parameter) { // 부모 댓글 수정 @Transactional(rollbackFor = ApiException.class) public void validateAndUpdateParentComment(UpdateParentCommentParameter parameter) { - Member member = memberRepository.findById(parameter.memberId()) - .orElseThrow(() -> ApiException.from(MEMBER_NOT_FOUND)); - validateParentCommentAuthority(member, parameter.parentCommentId()); + Long memberId = parameter.memberId(); + Long parentCommentId = parameter.parentCommentId(); + + validateParentCommentAuthority(memberId, parentCommentId); - ParentComment parentComment = parentCommentRepository.findById(parameter.parentCommentId()) + ParentComment parentComment = parentCommentRepository.findById(parentCommentId) .orElseThrow(() -> ApiException.from(PARENT_COMMENT_NOT_FOUND)); parentComment.updateContent(parameter.content()); @@ -64,10 +69,10 @@ public void validateAndUpdateParentComment(UpdateParentCommentParameter paramete // 부모 댓글 삭제 @Transactional(rollbackFor = ApiException.class) public void validateAndDeleteParentComment(DeleteParentCommentParameter parameter) { - Member member = memberRepository.findById(parameter.memberId()) - .orElseThrow(() -> ApiException.from(MEMBER_NOT_FOUND)); + Long memberId = parameter.memberId(); Long commentId = parameter.parentCommentId(); - validateParentCommentAuthority(member, commentId); + + validateParentCommentAuthority(memberId, commentId); parentCommentRepository.findById(commentId) .ifPresent((parentComment) -> { @@ -91,7 +96,6 @@ public void deleteIfParentCommentIsDeletedAndChildrenIsEmpty(ParentComment paren if (!parentComment.getIsDeleted()) { return; } - // 자식 댓글이 존재하면 return if(!parentComment.getChildComments().isEmpty()){ return;