In my application, there are post, comment, vote entities.
I injected post service, comment service to verify the data to vote.
VoteService
@Transactional
public boolean votePost(Member currentMember, Long postId)
{
Post post = postService.getPostById(postId);
Optional<Vote> optionalVote = voteRepository.findByMemberAndPost(currentMember, post);
if(optionalVote.isPresent())
{
voteRepository.delete(optionalVote.get());
return false;
}
else
{
voteRepository.save(new Vote(currentMember, post));
return true;
}
}
and also I need vote service in post service and comment service to check the logged-in user’s vote status.
VoteService
public boolean checkIfMemberVotedPost(Long memberId, Long postId)
{
return voteRepository.existsVoteByMemberIdAndPostId(memberId, postId);
}
public boolean checkIfMemberVotedComment(Long memberId, Long commentId)
{
return voteRepository.existsVoteByMemberIdAndCommentId(memberId, commentId);
}
But in this case, bean circular dependency error will occur.
I would like to know a better design or approach.
I know that using @Lazy can handle this problem but is this a valid solution instead of redesigning the application?