@Test public void upVoteArticle_not_allow_wrong_previous_state() throws Exception { service.voteArticle(UP, articleId, voter, EMPTY, 100); try { service.voteArticle(UP, articleId, voter, EMPTY, 100); fail("DuplicateKeyException expected"); } catch (DuplicateKeyException expected) { } }
@Override public void voteArticle(VoteState newState, FlakeId articleId, Authorization authorization, VoteState previousState, long previousCount) { Preconditions.checkArgument(newState != VoteState.DOWN); Article cachedArticle = articleDao.loadArticleWithCache(articleId); ZoneInfo zoneInfo = checkVoteAuthority(cachedArticle.getZone(), authorization); voteDao.voteArticle(newState, articleId, authorization.authenticatedId(), previousState, previousCount, Instant.now()); int upVoteDelta = newState.upVoteDeltaFrom(previousState); int downVoteDelta = newState.downVoteDeltaFrom(previousState); articleDao.changeTotalVote(articleId, upVoteDelta, downVoteDelta); if (zoneInfo.canContributeVoteStats() && !authorization.authenticatedId() .equals(cachedArticle.getAuthorId())) { accountDao.changeTotalVotedArticle(cachedArticle.getAuthorId(), upVoteDelta, downVoteDelta); honorRollDao.updateRotateVoteStats(HonorRollVoter.createByVote(cachedArticle, upVoteDelta, downVoteDelta)); } }
VoteServiceImpl implements VoteService { @Override public void voteArticle(VoteState newState, FlakeId articleId, Authorization authorization, VoteState previousState, long previousCount) { Preconditions.checkArgument(newState != VoteState.DOWN); Article cachedArticle = articleDao.loadArticleWithCache(articleId); ZoneInfo zoneInfo = checkVoteAuthority(cachedArticle.getZone(), authorization); voteDao.voteArticle(newState, articleId, authorization.authenticatedId(), previousState, previousCount, Instant.now()); int upVoteDelta = newState.upVoteDeltaFrom(previousState); int downVoteDelta = newState.downVoteDeltaFrom(previousState); articleDao.changeTotalVote(articleId, upVoteDelta, downVoteDelta); if (zoneInfo.canContributeVoteStats() && !authorization.authenticatedId() .equals(cachedArticle.getAuthorId())) { accountDao.changeTotalVotedArticle(cachedArticle.getAuthorId(), upVoteDelta, downVoteDelta); honorRollDao.updateRotateVoteStats(HonorRollVoter.createByVote(cachedArticle, upVoteDelta, downVoteDelta)); } } }
VoteServiceImpl implements VoteService { @Override public void voteArticle(VoteState newState, FlakeId articleId, Authorization authorization, VoteState previousState, long previousCount) { Preconditions.checkArgument(newState != VoteState.DOWN); Article cachedArticle = articleDao.loadArticleWithCache(articleId); ZoneInfo zoneInfo = checkVoteAuthority(cachedArticle.getZone(), authorization); voteDao.voteArticle(newState, articleId, authorization.authenticatedId(), previousState, previousCount, Instant.now()); int upVoteDelta = newState.upVoteDeltaFrom(previousState); int downVoteDelta = newState.downVoteDeltaFrom(previousState); articleDao.changeTotalVote(articleId, upVoteDelta, downVoteDelta); if (zoneInfo.canContributeVoteStats() && !authorization.authenticatedId() .equals(cachedArticle.getAuthorId())) { accountDao.changeTotalVotedArticle(cachedArticle.getAuthorId(), upVoteDelta, downVoteDelta); honorRollDao.updateRotateVoteStats(HonorRollVoter.createByVote(cachedArticle, upVoteDelta, downVoteDelta)); } }  }
VoteServiceImpl implements VoteService { @Override public void voteArticle(VoteState newState, FlakeId articleId, Authorization authorization, VoteState previousState, long previousCount) { Preconditions.checkArgument(newState != VoteState.DOWN); Article cachedArticle = articleDao.loadArticleWithCache(articleId); ZoneInfo zoneInfo = checkVoteAuthority(cachedArticle.getZone(), authorization); voteDao.voteArticle(newState, articleId, authorization.authenticatedId(), previousState, previousCount, Instant.now()); int upVoteDelta = newState.upVoteDeltaFrom(previousState); int downVoteDelta = newState.downVoteDeltaFrom(previousState); articleDao.changeTotalVote(articleId, upVoteDelta, downVoteDelta); if (zoneInfo.canContributeVoteStats() && !authorization.authenticatedId() .equals(cachedArticle.getAuthorId())) { accountDao.changeTotalVotedArticle(cachedArticle.getAuthorId(), upVoteDelta, downVoteDelta); honorRollDao.updateRotateVoteStats(HonorRollVoter.createByVote(cachedArticle, upVoteDelta, downVoteDelta)); } }  @Override List<DebateVoter> listDebateVoters(Authorization voter, FlakeId articleId); @Override void voteArticle(VoteState newState,
      FlakeId articleId,
      Authorization authorization,
      VoteState previousState,
      long previousCount); @Override void voteDebate(VoteState newState,
      FlakeId debateId,
      Authorization voter,
      VoteState previousState,
      long previousCount); @Override List<ArticleVoter> listArticleVoters(Authorization voter, List<FlakeId> articleIds); @Override List<DebateVoter> listDebateVotersByIds(Authorization voter, List<FlakeId> debateIds); @Override List<Article> listUpVotedArticles(Authorization voter, @Nullable FlakeId startArticleId); }
VoteServiceImpl implements VoteService { @Override public void voteArticle(VoteState newState, FlakeId articleId, Authorization authorization, VoteState previousState, long previousCount) { Preconditions.checkArgument(newState != VoteState.DOWN); Article cachedArticle = articleDao.loadArticleWithCache(articleId); ZoneInfo zoneInfo = checkVoteAuthority(cachedArticle.getZone(), authorization); voteDao.voteArticle(newState, articleId, authorization.authenticatedId(), previousState, previousCount, Instant.now()); int upVoteDelta = newState.upVoteDeltaFrom(previousState); int downVoteDelta = newState.downVoteDeltaFrom(previousState); articleDao.changeTotalVote(articleId, upVoteDelta, downVoteDelta); if (zoneInfo.canContributeVoteStats() && !authorization.authenticatedId() .equals(cachedArticle.getAuthorId())) { accountDao.changeTotalVotedArticle(cachedArticle.getAuthorId(), upVoteDelta, downVoteDelta); honorRollDao.updateRotateVoteStats(HonorRollVoter.createByVote(cachedArticle, upVoteDelta, downVoteDelta)); } }  @Override List<DebateVoter> listDebateVoters(Authorization voter, FlakeId articleId); @Override void voteArticle(VoteState newState,
      FlakeId articleId,
      Authorization authorization,
      VoteState previousState,
      long previousCount); @Override void voteDebate(VoteState newState,
      FlakeId debateId,
      Authorization voter,
      VoteState previousState,
      long previousCount); @Override List<ArticleVoter> listArticleVoters(Authorization voter, List<FlakeId> articleIds); @Override List<DebateVoter> listDebateVotersByIds(Authorization voter, List<FlakeId> debateIds); @Override List<Article> listUpVotedArticles(Authorization voter, @Nullable FlakeId startArticleId);  }