From fb5284967b5e7e21c5436ae1c3e750c63c8d1205 Mon Sep 17 00:00:00 2001 From: "BOOK-U3FJG82JE7\\USER" Date: Tue, 12 Nov 2024 22:32:08 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20ChildComment=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/common/config/DataInitConfig.java | 2 -- .../controller/ChildCommentController.java | 3 +- .../dto/request/SaveChildCommentRequest.java | 4 +-- .../request/UpdateChildCommentRequest.java | 13 +++++++ .../domains/comment/entity/ChildComment.java | 11 +----- .../repository/ChildCommentRepository.java | 3 +- .../comment/service/ChildCommentService.java | 36 ++++++++++--------- 7 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateChildCommentRequest.java diff --git a/backend/src/main/java/aimo/backend/common/config/DataInitConfig.java b/backend/src/main/java/aimo/backend/common/config/DataInitConfig.java index f579760..02f76dd 100644 --- a/backend/src/main/java/aimo/backend/common/config/DataInitConfig.java +++ b/backend/src/main/java/aimo/backend/common/config/DataInitConfig.java @@ -119,7 +119,6 @@ public void initializeData(MemberRepository memberRepo, PrivatePostRepository pr ParentComment.builder() .nickname("ParentComment" + i) .content("This is a parent comment " + i) - .isDeleted(false) .member(member) .post(post) .build(); @@ -135,7 +134,6 @@ public void initializeData(MemberRepository memberRepo, PrivatePostRepository pr .content("This is a child comment " + i) .member(member) .nickname(member.getNickname()) - .isDeleted(false) .parentComment(parentComment) .post(postRepo.getReferenceById(parentComment.getPost().getId())) .build(); diff --git a/backend/src/main/java/aimo/backend/domains/comment/controller/ChildCommentController.java b/backend/src/main/java/aimo/backend/domains/comment/controller/ChildCommentController.java index c577d2a..10a0651 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/controller/ChildCommentController.java +++ b/backend/src/main/java/aimo/backend/domains/comment/controller/ChildCommentController.java @@ -14,6 +14,7 @@ import aimo.backend.domains.comment.dto.request.SaveChildCommentRequest; import aimo.backend.domains.comment.dto.parameter.ValidAndDeleteParentCommentParameter; import aimo.backend.domains.comment.dto.parameter.ValidAndUpdateChildCommentParameter; +import aimo.backend.domains.comment.dto.request.UpdateChildCommentRequest; import aimo.backend.domains.comment.service.ChildCommentService; import aimo.backend.common.util.memberLoader.MemberLoader; import jakarta.validation.Valid; @@ -45,7 +46,7 @@ public ResponseEntity> saveChildComment( @PutMapping("comments/child/{childCommentId}") public ResponseEntity> updateChildComment( @PathVariable Long childCommentId, - @Valid @RequestBody SaveChildCommentRequest request + @Valid @RequestBody UpdateChildCommentRequest request ) { Long memberId = MemberLoader.getMemberId(); diff --git a/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveChildCommentRequest.java b/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveChildCommentRequest.java index 36d56b9..4dd6a1f 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveChildCommentRequest.java +++ b/backend/src/main/java/aimo/backend/domains/comment/dto/request/SaveChildCommentRequest.java @@ -3,11 +3,11 @@ import jakarta.validation.constraints.Size; public record SaveChildCommentRequest( - @Size(max = 1500, message = "댓글은 최대 500자까지 입력할 수 있습니다.") + @Size(max = 500, message = "댓글은 최대 500자까지 입력할 수 있습니다.") String content ) { - public static SaveChildCommentRequest of(String content) { + public static SaveChildCommentRequest from(String content) { return new SaveChildCommentRequest(content); } } diff --git a/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateChildCommentRequest.java b/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateChildCommentRequest.java new file mode 100644 index 0000000..32e7cef --- /dev/null +++ b/backend/src/main/java/aimo/backend/domains/comment/dto/request/UpdateChildCommentRequest.java @@ -0,0 +1,13 @@ +package aimo.backend.domains.comment.dto.request; + +import jakarta.validation.constraints.Size; + +public record UpdateChildCommentRequest( + @Size(max = 500, message = "댓글은 최대 500자까지 입력할 수 있습니다.") + String content +) { + + public static UpdateChildCommentRequest from(String content) { + return new UpdateChildCommentRequest(content); + } +} diff --git a/backend/src/main/java/aimo/backend/domains/comment/entity/ChildComment.java b/backend/src/main/java/aimo/backend/domains/comment/entity/ChildComment.java index baf0d33..05707ba 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/entity/ChildComment.java +++ b/backend/src/main/java/aimo/backend/domains/comment/entity/ChildComment.java @@ -66,7 +66,6 @@ private ChildComment( String nickname, String content, ParentComment parentComment, - Boolean isDeleted, Member member, Post post, List childCommentLikes @@ -74,7 +73,7 @@ private ChildComment( this.nickname = nickname; this.content = content; this.parentComment = parentComment; - this.isDeleted = isDeleted; + this.isDeleted = false; this.member = member; this.post = post; this.childCommentLikes = childCommentLikes; @@ -90,7 +89,6 @@ public static ChildComment of( .nickname(member.getNickname()) .content(content) .parentComment(parentComment) - .isDeleted(false) .member(member) .post(post) .build(); @@ -106,13 +104,6 @@ public void deleteChildCommentSoftly() { this.isDeleted = true; } - public void deleteChildCommentSoftlyWithContent() { - this.member = null; - this.nickname = CommentConstants.DELETED_MEMBER.getValue(); - this.isDeleted = true; - content = CommentConstants.DELETED_COMMENT.getValue(); - } - public void updateChildComment(String content) { this.content = content; } diff --git a/backend/src/main/java/aimo/backend/domains/comment/repository/ChildCommentRepository.java b/backend/src/main/java/aimo/backend/domains/comment/repository/ChildCommentRepository.java index 0a93aaf..d43a561 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/repository/ChildCommentRepository.java +++ b/backend/src/main/java/aimo/backend/domains/comment/repository/ChildCommentRepository.java @@ -5,11 +5,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import aimo.backend.domains.comment.entity.ChildComment; -import aimo.backend.domains.member.entity.Member; public interface ChildCommentRepository 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/ChildCommentService.java b/backend/src/main/java/aimo/backend/domains/comment/service/ChildCommentService.java index 6571bde..7be4515 100644 --- a/backend/src/main/java/aimo/backend/domains/comment/service/ChildCommentService.java +++ b/backend/src/main/java/aimo/backend/domains/comment/service/ChildCommentService.java @@ -29,8 +29,8 @@ public class ChildCommentService { private final MemberService memberService; //자식 댓글 권한 확인 - private void validateChildCommentAuthority(Member member, Long childCommentId) throws ApiException { - Boolean exists = childCommentRepository.existsByIdAndMember(childCommentId, member); + private void validateChildCommentAuthority(Long memberId, Long childCommentId) throws ApiException { + Boolean exists = childCommentRepository.existsByIdAndMember_Id(childCommentId, memberId); if (!exists) { throw ApiException.from(UNAUTHORIZED_CHILD_COMMENT); @@ -39,14 +39,15 @@ private void validateChildCommentAuthority(Member member, Long childCommentId) t //자식 댓글 저장 @Transactional(rollbackFor = ApiException.class) - public void saveChildComment(SaveChildCommentParameter saveChildCommentParameter) { - Long postId = saveChildCommentParameter.postId(); - Long parentCommentId = saveChildCommentParameter.parentCommentId(); - String content = saveChildCommentParameter.content(); - Member member = memberService.findMemberById(saveChildCommentParameter.memberId()); - Post post = postService.findById(postId); + public void saveChildComment(SaveChildCommentParameter parameter) { + Long postId = parameter.postId(); + Long parentCommentId = parameter.parentCommentId(); + String content = parameter.content(); + Member member = memberService.findMemberById(parameter.memberId()); + Post post = postService.findById(postId); ParentComment parentComment = parentCommentMemberService.findById(parentCommentId); + ChildComment childComment = ChildComment.of(content, member, parentComment, post); childCommentRepository.save(childComment); } @@ -54,12 +55,15 @@ public void saveChildComment(SaveChildCommentParameter saveChildCommentParameter //자식 댓글 수정 @Transactional(rollbackFor = ApiException.class) public void validateAndUpdateChildComment( - ValidAndUpdateChildCommentParameter validAndUpdateChildCommentParameter) { - Member member = memberService.findMemberById(validAndUpdateChildCommentParameter.memberId()); - Long childCommentId = validAndUpdateChildCommentParameter.childCommentId(); - String content = validAndUpdateChildCommentParameter.content(); + ValidAndUpdateChildCommentParameter parameter + ) { + Long memberId = parameter.memberId(); + Long childCommentId = parameter.childCommentId(); + String content = parameter.content(); + + // 자식 댓글 권한 확인 + validateChildCommentAuthority(memberId, childCommentId); - validateChildCommentAuthority(member, childCommentId); ChildComment childComment = childCommentRepository.findById(childCommentId) .orElseThrow(() -> ApiException.from(UNAUTHORIZED_CHILD_COMMENT)); @@ -72,17 +76,17 @@ public void validateAndDeleteChildComment( ValidAndDeleteParentCommentParameter parameter ) { Long childCommentId = parameter.childCommentId(); - - Member member = memberService.findMemberById(parameter.memberId()); + Long memberId = parameter.memberId(); // 자식 댓글 권한 확인 - validateChildCommentAuthority(member, childCommentId); + validateChildCommentAuthority(memberId, childCommentId); ParentComment parentComment = childCommentRepository.findById(childCommentId) .orElseThrow(() -> ApiException.from(CHILD_COMMENT_NOT_FOUND)) .getParentComment(); // 자식 댓글 삭제 + parentComment.deleteChildComment(childCommentId); childCommentRepository.deleteById(childCommentId); // 자식 댓글이 없으며 부모 댓글이 삭제된 상태면 삭제