-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring:� 좋아요 기능 분리, 쿼리 수정 (#179)
* refactor: value 에서 content 로 변경 * refactor: 사용하지 않는 메서드 삭제 * refactor: dto 생성 트랜젝션안에서 생성하게 변경 * refactor: findByIdFetchJoinCommenter 쿼리변경 * style: 자동정렬 * refactor: comment 좋아요 기능 분리 * refactor: 사용하지 않는 CommentDeleteEvent 삭제 * test: 댓글 좋아요 테스트 분리
- Loading branch information
Showing
11 changed files
with
297 additions
and
244 deletions.
There are no files selected for viewing
17 changes: 0 additions & 17 deletions
17
src/main/java/com/salmalteam/salmal/comment/application/CommentDeleteEvent.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
src/main/java/com/salmalteam/salmal/comment/application/CommentLikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.salmalteam.salmal.comment.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.salmalteam.salmal.comment.entity.Comment; | ||
import com.salmalteam.salmal.comment.entity.CommentRepository; | ||
import com.salmalteam.salmal.comment.entity.like.CommentLike; | ||
import com.salmalteam.salmal.comment.entity.like.CommentLikeRepository; | ||
import com.salmalteam.salmal.comment.exception.CommentException; | ||
import com.salmalteam.salmal.comment.exception.CommentExceptionType; | ||
import com.salmalteam.salmal.comment.exception.like.CommentLikeException; | ||
import com.salmalteam.salmal.comment.exception.like.CommentLikeExceptionType; | ||
import com.salmalteam.salmal.member.entity.Member; | ||
import com.salmalteam.salmal.member.entity.MemberRepository; | ||
import com.salmalteam.salmal.member.exception.MemberException; | ||
import com.salmalteam.salmal.member.exception.MemberExceptionType; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommentLikeService { | ||
|
||
private final CommentLikeRepository commentLikeRepository; | ||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Transactional | ||
public void likeComment(final Long memberId, final Long commentId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND)); | ||
|
||
final Comment comment = getCommentById(commentId); | ||
|
||
validateCommentAlreadyLiked(comment, member); | ||
final CommentLike commentLike = CommentLike.of(comment, member); | ||
commentLikeRepository.save(commentLike); | ||
commentRepository.increaseLikeCount(commentId); | ||
|
||
} | ||
|
||
private void validateCommentAlreadyLiked(final Comment comment, final Member member) { | ||
if (commentLikeRepository.existsByCommentAndLiker(comment, member)) { | ||
throw new CommentLikeException(CommentLikeExceptionType.DUPLICATED_LIKE); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void unLikeComment(final Long memberId, final Long commentId) { | ||
|
||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND)); | ||
|
||
final Comment comment = getCommentById(commentId); | ||
validateCommentNotLiked(comment, member); | ||
|
||
commentLikeRepository.deleteByCommentAndLiker(comment, member); | ||
commentRepository.decreaseLikeCount(commentId); | ||
} | ||
|
||
private void validateCommentNotLiked(final Comment comment, final Member member) { | ||
if (!commentLikeRepository.existsByCommentAndLiker(comment, member)) { | ||
throw new CommentLikeException(CommentLikeExceptionType.NOT_FOUND); | ||
} | ||
} | ||
|
||
|
||
private Comment getCommentById(final Long commentId) { | ||
return commentRepository.findById(commentId) | ||
.orElseThrow(() -> new CommentException(CommentExceptionType.NOT_FOUND)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 9 additions & 19 deletions
28
src/main/java/com/salmalteam/salmal/comment/entity/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.