-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from ttoklip/Feat/15-question-comment-like
pr Feat/15 question comment like, likedByCurrentUser value
- Loading branch information
Showing
16 changed files
with
361 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/api/ttoklip/domain/question/like/entity/CommentLike.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,39 @@ | ||
package com.api.ttoklip.domain.question.like.entity; | ||
|
||
import com.api.ttoklip.domain.common.base.BaseTimeEntity; | ||
import com.api.ttoklip.domain.member.domain.Member; | ||
import com.api.ttoklip.domain.question.comment.domain.QuestionComment; | ||
import com.api.ttoklip.domain.question.post.domain.Question; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class CommentLike extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", updatable = false) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "question_id") | ||
private QuestionComment questionComment; | ||
|
||
public static CommentLike from(final QuestionComment questionComment) { | ||
return CommentLike.builder() | ||
.member(getCurrentMember()) | ||
.questionComment(questionComment) | ||
.build(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/api/ttoklip/domain/question/like/repository/CommentLikeRepository.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,15 @@ | ||
package com.api.ttoklip.domain.question.like.repository; | ||
|
||
import com.api.ttoklip.domain.question.like.entity.CommentLike; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
|
||
public interface CommentLikeRepository extends JpaRepository<CommentLike, Long>, CommentLikeRepositoryCustom { | ||
|
||
Optional<CommentLike> findByQuestionCommentIdAndMemberId(Long commentId, Long memberId); | ||
|
||
boolean existsByQuestionCommentIdAndMemberId(Long commentId, Long memberId); | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...ain/java/com/api/ttoklip/domain/question/like/repository/CommentLikeRepositoryCustom.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,5 @@ | ||
package com.api.ttoklip.domain.question.like.repository; | ||
|
||
public interface CommentLikeRepositoryCustom { | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/api/ttoklip/domain/question/like/service/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,55 @@ | ||
package com.api.ttoklip.domain.question.like.service; | ||
|
||
import com.api.ttoklip.domain.question.comment.domain.QuestionComment; | ||
import com.api.ttoklip.domain.question.like.entity.CommentLike; | ||
import com.api.ttoklip.domain.question.like.repository.CommentLikeRepository; | ||
import com.api.ttoklip.domain.question.post.service.QuestionCommonService; | ||
import com.api.ttoklip.global.exception.ApiException; | ||
import com.api.ttoklip.global.exception.ErrorType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class CommentLikeService { | ||
|
||
private final CommentLikeRepository commentLikeRepository; | ||
private final QuestionCommonService questionCommonService; | ||
|
||
// 좋아요 생성 | ||
public void registerLike(final Long commentId) { | ||
Long currentMemberId = getCurrentMember().getId(); | ||
boolean exists = commentLikeRepository.existsByQuestionCommentIdAndMemberId(commentId, currentMemberId); | ||
if (exists) { | ||
return; // 이미 스크랩이 존재하면 좋아요를 생성하지 않고 return | ||
} | ||
|
||
QuestionComment findQuestionComment = questionCommonService.getQuestionComment(commentId); | ||
CommentLike commentLike = CommentLike.from(findQuestionComment); | ||
commentLikeRepository.save(commentLike); | ||
} | ||
|
||
// 좋아요 취소 | ||
public void cancelLike(final Long commentId) { | ||
// commentId (댓글 ID) | ||
QuestionComment findQuestionComment = questionCommonService.getQuestionComment(commentId); | ||
Long findQuestionCommentId = findQuestionComment.getId(); | ||
Long currentMemberId = getCurrentMember().getId(); | ||
|
||
CommentLike commentLike = commentLikeRepository.findByQuestionCommentIdAndMemberId(findQuestionCommentId, currentMemberId) | ||
.orElseThrow(() -> new ApiException(ErrorType.LIKE_NOT_FOUND)); | ||
|
||
// 자격 검증: 이 단계에서는 findByQuestionCommentIdAndMemberId 결과가 존재하므로, 현재 사용자가 좋아요를 누른 것입니다. | ||
// 별도의 자격 검증 로직이 필요 없으며, 바로 삭제를 진행할 수 있습니다. | ||
commentLikeRepository.deleteById(commentLike.getId()); | ||
} | ||
|
||
public boolean existsByQuestionCommentIdAndMemberId(final Long commentId) { | ||
Long currentMemberId = getCurrentMember().getId(); | ||
return commentLikeRepository.existsByQuestionCommentIdAndMemberId(commentId, currentMemberId); | ||
} | ||
} |
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
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.