Skip to content

Commit

Permalink
Refactoring:� 좋아요 기능 분리, 쿼리 수정 (#179)
Browse files Browse the repository at this point in the history
* refactor: value 에서 content 로 변경

* refactor: 사용하지 않는 메서드 삭제

* refactor: dto 생성 트랜젝션안에서 생성하게 변경

* refactor: findByIdFetchJoinCommenter 쿼리변경

* style: 자동정렬

* refactor: comment 좋아요 기능 분리

* refactor: 사용하지 않는 CommentDeleteEvent 삭제

* test: 댓글 좋아요 테스트 분리
  • Loading branch information
kimJH47 authored Feb 19, 2024
1 parent d6341f3 commit aab8f7f
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 244 deletions.

This file was deleted.

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
import com.salmalteam.salmal.comment.dto.response.ReplyResponse;
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.entity.report.CommentReport;
import com.salmalteam.salmal.comment.entity.report.CommentReportRepository;
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.comment.exception.report.CommentReportException;
import com.salmalteam.salmal.comment.exception.report.CommentReportExceptionType;
import com.salmalteam.salmal.member.application.MemberService;
Expand All @@ -44,7 +40,6 @@ public class CommentService {
private final MemberService memberService;
private final CommentRepository commentRepository;
private final CommentReportRepository commentReportRepository;
private final CommentLikeRepository commentLikeRepository;
private final VoteRepository voteRepository;

@Transactional
Expand Down Expand Up @@ -81,11 +76,14 @@ public ReplayCommentDto replyComment(final Long memberId, final Long commentId,
final Comment comment = getCommentById(commentId); //대댓글을 작성한 댓글(대댓글 주인)
final Comment reply = Comment.ofReply(commentReplyCreateRequest.getContent(), comment, replyer); //대댓글
final Member commenterOwner = comment.getCommenter(); //댓글 주인
Vote vote = comment.getVote();
Long voteId = vote.getId();
String imageUrl = vote.getVoteImage().getImageUrl();
commentRepository.save(reply);
commentRepository.increaseReplyCount(commentId);

return ReplayCommentDto.createNotificationType(replyer, commenterOwner, comment, reply, comment.getVote());
}
return ReplayCommentDto.createNotificationType(replyer, commenterOwner, comment, reply, imageUrl, voteId);
}

@Transactional(readOnly = true)
public ReplyPageResponse searchReplies(final Long memberId, final Long commentId,
Expand Down Expand Up @@ -156,44 +154,6 @@ public List<CommentResponse> searchAllList(final Long voteId, final Long memberI
return commentRepository.searchAllList(voteId, memberId);
}

@Transactional
public void likeComment(final Long memberId, final Long commentId) {

final Member member = memberService.findMemberById(memberId);
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) {

final Member member = memberService.findMemberById(memberId);
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);
}
}

@Transactional
public void report(final Long memberId, final Long commentId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.salmalteam.salmal.comment.entity.Comment;
import com.salmalteam.salmal.member.entity.Member;
import com.salmalteam.salmal.vote.entity.Vote;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -20,17 +19,16 @@ public class ReplayCommentDto {
private final Long voteId;

public static ReplayCommentDto createNotificationType(Member replyer, Member commenterOwner, Comment comment,
Comment reply,
Vote vote) {
Comment reply, String imageUrl, Long voteId) {
return new ReplayCommentDto(
commenterOwner.getId(), //알림 타켓 ID
replyer.getId(), //대댓글 작성자
comment.getId(), //대댓글을 작성한 댓글 ID
replyer.getNickName().getValue(), //대댓글 작성자 ID
reply.getContent().getValue(), //대댓글 내용
replyer.getMemberImage().getImageUrl(), //대댓글 작성자 이미지
vote.getVoteImage().getImageUrl(), //대댓글 작성한 투표의 이미지
vote.getId()
imageUrl, //대댓글 작성한 투표의 이미지
voteId
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
package com.salmalteam.salmal.comment.entity;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

public interface CommentRepository extends Repository<Comment, Long>, CommentRepositoryCustom {
Comment save(Comment comment);
void delete(Comment comment);
@Query("select c from Comment c join fetch c.commenter join fetch c.vote where c.id =:id")
Optional<Comment> findById(@Param("id") Long id);
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom {

@Query("select c from Comment c join fetch c.commenter where c.id =:id")
Optional<Comment> findByIdFetchJoinCommenter(@Param("id") Long id);

boolean existsById(Long id);
List<Comment> findAllByCommenter_Id(Long commenterId);
List<Comment> findALlByCommenter_idAndCommentType(Long commenterId, CommentType commentType);
List<Comment> findAllByVote_Id(Long voteId);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(value = "update Comment c set c.replyCount = c.replyCount - 1 where c.id = :id")
void decreaseReplyCount(Long id);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("DELETE from Comment c where c.parentComment.id = :commentId")
void deleteAllRepliesByParentCommentId(@Param("commentId") Long commentId);
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("DELETE from Comment c where c.id in :commentIdsToDel")
void deleteAllCommentsByIdIn(@Param("commentIdsToDel") List<Long> commentIdsToDel);
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("DELETE from Comment c where c.parentComment.id in :commentIdsToDel")
void deleteAllRepliesByIdIn(@Param("commentIdsToDel") List<Long> commentIdsToDel);
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("DELETE from Comment c where c.commenter.id = :commenterId AND c.commentType = :commentType")
void deleteAllCommentsByCommenterId(@Param("commenterId") Long commenterId, @Param("commentType") CommentType commentType);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(value = "update Comment c set c.likeCount = c.likeCount + 1 where c.id = :id")
void increaseLikeCount(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Content {
private static final int MAX_LENGTH = 100;
private static final int MIN_LENGTH = 1;

@Column
@Column(name = "content")
private String value;

private Content(final String value){
Expand Down
Loading

0 comments on commit aab8f7f

Please sign in to comment.