Skip to content

Commit

Permalink
refactor: ParentComment 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
yooooonshine committed Nov 13, 2024
1 parent fb52849 commit 4147326
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public class ParentCommentController {
@PostMapping("/{postId}/comments")
public ResponseEntity<DataResponse<Void>> 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());
Expand All @@ -42,10 +43,15 @@ public ResponseEntity<DataResponse<Void>> saveParentComment(
@PutMapping("comments/{commentId}")
public ResponseEntity<DataResponse<Void>> 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());
Expand All @@ -56,7 +62,9 @@ public ResponseEntity<DataResponse<Void>> deleteParentComment(
@Valid @PathVariable("commentId") Long commentId
) {
Long memberId = MemberLoader.getMemberId();

DeleteParentCommentParameter parameter = DeleteParentCommentParameter.of(memberId, commentId);

parentCommentMemberService.validateAndDeleteParentComment(parameter);

return ResponseEntity.ok(DataResponse.ok());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ public Integer getLikesCount() {
private ParentComment(
String nickname,
String content,
Boolean isDeleted,
Member member,
Post post,
List<ChildComment> childComments,
List<ParentCommentLike> parentCommentLikes
) {
this.nickname = nickname;
this.content = content;
this.isDeleted = isDeleted;
this.isDeleted = false;
this.member = member;
this.post = post;
this.childComments = childComments;
Expand All @@ -84,29 +83,32 @@ 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();
this.isDeleted = true;
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface ParentCommentRepository extends JpaRepository<ParentComment, Long> {

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

List<ParentComment> findByMemberId(Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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) -> {
Expand All @@ -91,7 +96,6 @@ public void deleteIfParentCommentIsDeletedAndChildrenIsEmpty(ParentComment paren
if (!parentComment.getIsDeleted()) {
return;
}

// 자식 댓글이 존재하면 return
if(!parentComment.getChildComments().isEmpty()){
return;
Expand Down

0 comments on commit 4147326

Please sign in to comment.