Skip to content

Commit

Permalink
refactor: ChildComment 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
yooooonshine committed Nov 12, 2024
1 parent 9cdc7c1 commit fb52849
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ResponseEntity<DataResponse<Void>> saveChildComment(
@PutMapping("comments/child/{childCommentId}")
public ResponseEntity<DataResponse<Void>> updateChildComment(
@PathVariable Long childCommentId,
@Valid @RequestBody SaveChildCommentRequest request
@Valid @RequestBody UpdateChildCommentRequest request
) {
Long memberId = MemberLoader.getMemberId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ private ChildComment(
String nickname,
String content,
ParentComment parentComment,
Boolean isDeleted,
Member member,
Post post,
List<ChildCommentLike> childCommentLikes
) {
this.nickname = nickname;
this.content = content;
this.parentComment = parentComment;
this.isDeleted = isDeleted;
this.isDeleted = false;
this.member = member;
this.post = post;
this.childCommentLikes = childCommentLikes;
Expand All @@ -90,7 +89,6 @@ public static ChildComment of(
.nickname(member.getNickname())
.content(content)
.parentComment(parentComment)
.isDeleted(false)
.member(member)
.post(post)
.build();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChildComment, Long> {

Boolean existsByIdAndMember(Long id, Member member);
Boolean existsByIdAndMember_Id(Long id, Long memberId);

List<ChildComment> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,27 +39,31 @@ 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);
}

//자식 댓글 수정
@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));

Expand All @@ -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);

// 자식 댓글이 없으며 부모 댓글이 삭제된 상태면 삭제
Expand Down

0 comments on commit fb52849

Please sign in to comment.