@Test public void testVoteComment_DefaultToUp() throws Exception { final User user = UserServiceFactory.getUserService().getCurrentUser(); final Key<Account> accountKey = Key.create(user.getUserId()); Comment comment = commentController.insertComment(postEntity.getWebsafeId(), "Test comment", user); voteController.vote(comment.getWebsafeId(), Vote.Direction.DEFAULT.getValue(), user); CommentShardService shardService = CommentShardService.create(); VoteService voteService = VoteService.create(); comment = shardService .mergeShards(comment) .flatMap(comment1 -> voteService.checkCommentVote(comment1, accountKey)) .blockingSingle(); assertEquals(Vote.Direction.DEFAULT, comment.getDir()); assertEquals(0L, comment.getVoteCount()); voteController.vote(comment.getWebsafeId(), Vote.Direction.UP.getValue(), user); comment = shardService .mergeShards(comment) .flatMap(comment1 -> voteService.checkCommentVote(comment1, accountKey)) .blockingSingle(); assertEquals(Vote.Direction.UP, comment.getDir()); assertEquals(1L, comment.getVoteCount()); }
public void vote(String votableId, int dir, User user) { Vote.Direction direction = Vote.parse(dir); final Key<Votable> votableKey = Key.create(votableId); final Key<Account> accountKey = Key.create(user.getUserId()); Shardable.Shard shard = getShard(votableKey); Vote dbVote = getVote(votableKey, accountKey); if (dbVote == null) { Vote vote = Vote .builder() .id(votableKey.toWebSafeString()) .parent(accountKey) .votableKey(votableKey) .dir(dir) .build(); switch (direction) { case UP: shard.increaseVotesBy(1L); break; case DOWN: shard.decreaseVotesBy(1L); break; } ofy().transact(() -> ofy().save().entities(vote, shard).now()); } else { switch (Vote.parse(dbVote.getDir())) { case DEFAULT: if (direction == Vote.Direction.UP) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(1L); } break; case UP: if (direction == Vote.Direction.DEFAULT) { shard.decreaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(2L); } break; case DOWN: if (direction == Vote.Direction.DEFAULT) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.UP) { shard.increaseVotesBy(2L); } break; } ofy().transact(() -> ofy().save().entities(dbVote.withDir(dir), shard).now()); } }
VoteController extends Controller { public void vote(String votableId, int dir, User user) { Vote.Direction direction = Vote.parse(dir); final Key<Votable> votableKey = Key.create(votableId); final Key<Account> accountKey = Key.create(user.getUserId()); Shardable.Shard shard = getShard(votableKey); Vote dbVote = getVote(votableKey, accountKey); if (dbVote == null) { Vote vote = Vote .builder() .id(votableKey.toWebSafeString()) .parent(accountKey) .votableKey(votableKey) .dir(dir) .build(); switch (direction) { case UP: shard.increaseVotesBy(1L); break; case DOWN: shard.decreaseVotesBy(1L); break; } ofy().transact(() -> ofy().save().entities(vote, shard).now()); } else { switch (Vote.parse(dbVote.getDir())) { case DEFAULT: if (direction == Vote.Direction.UP) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(1L); } break; case UP: if (direction == Vote.Direction.DEFAULT) { shard.decreaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(2L); } break; case DOWN: if (direction == Vote.Direction.DEFAULT) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.UP) { shard.increaseVotesBy(2L); } break; } ofy().transact(() -> ofy().save().entities(dbVote.withDir(dir), shard).now()); } } }
VoteController extends Controller { public void vote(String votableId, int dir, User user) { Vote.Direction direction = Vote.parse(dir); final Key<Votable> votableKey = Key.create(votableId); final Key<Account> accountKey = Key.create(user.getUserId()); Shardable.Shard shard = getShard(votableKey); Vote dbVote = getVote(votableKey, accountKey); if (dbVote == null) { Vote vote = Vote .builder() .id(votableKey.toWebSafeString()) .parent(accountKey) .votableKey(votableKey) .dir(dir) .build(); switch (direction) { case UP: shard.increaseVotesBy(1L); break; case DOWN: shard.decreaseVotesBy(1L); break; } ofy().transact(() -> ofy().save().entities(vote, shard).now()); } else { switch (Vote.parse(dbVote.getDir())) { case DEFAULT: if (direction == Vote.Direction.UP) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(1L); } break; case UP: if (direction == Vote.Direction.DEFAULT) { shard.decreaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(2L); } break; case DOWN: if (direction == Vote.Direction.DEFAULT) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.UP) { shard.increaseVotesBy(2L); } break; } ofy().transact(() -> ofy().save().entities(dbVote.withDir(dir), shard).now()); } }  }
VoteController extends Controller { public void vote(String votableId, int dir, User user) { Vote.Direction direction = Vote.parse(dir); final Key<Votable> votableKey = Key.create(votableId); final Key<Account> accountKey = Key.create(user.getUserId()); Shardable.Shard shard = getShard(votableKey); Vote dbVote = getVote(votableKey, accountKey); if (dbVote == null) { Vote vote = Vote .builder() .id(votableKey.toWebSafeString()) .parent(accountKey) .votableKey(votableKey) .dir(dir) .build(); switch (direction) { case UP: shard.increaseVotesBy(1L); break; case DOWN: shard.decreaseVotesBy(1L); break; } ofy().transact(() -> ofy().save().entities(vote, shard).now()); } else { switch (Vote.parse(dbVote.getDir())) { case DEFAULT: if (direction == Vote.Direction.UP) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(1L); } break; case UP: if (direction == Vote.Direction.DEFAULT) { shard.decreaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(2L); } break; case DOWN: if (direction == Vote.Direction.DEFAULT) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.UP) { shard.increaseVotesBy(2L); } break; } ofy().transact(() -> ofy().save().entities(dbVote.withDir(dir), shard).now()); } }  void vote(String votableId, int dir, User user); CollectionResponse<Account> listVoters(@Nonnull String postId, Optional<Integer> limit,
      Optional<String> cursor); }
VoteController extends Controller { public void vote(String votableId, int dir, User user) { Vote.Direction direction = Vote.parse(dir); final Key<Votable> votableKey = Key.create(votableId); final Key<Account> accountKey = Key.create(user.getUserId()); Shardable.Shard shard = getShard(votableKey); Vote dbVote = getVote(votableKey, accountKey); if (dbVote == null) { Vote vote = Vote .builder() .id(votableKey.toWebSafeString()) .parent(accountKey) .votableKey(votableKey) .dir(dir) .build(); switch (direction) { case UP: shard.increaseVotesBy(1L); break; case DOWN: shard.decreaseVotesBy(1L); break; } ofy().transact(() -> ofy().save().entities(vote, shard).now()); } else { switch (Vote.parse(dbVote.getDir())) { case DEFAULT: if (direction == Vote.Direction.UP) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(1L); } break; case UP: if (direction == Vote.Direction.DEFAULT) { shard.decreaseVotesBy(1L); } else if (direction == Vote.Direction.DOWN) { shard.decreaseVotesBy(2L); } break; case DOWN: if (direction == Vote.Direction.DEFAULT) { shard.increaseVotesBy(1L); } else if (direction == Vote.Direction.UP) { shard.increaseVotesBy(2L); } break; } ofy().transact(() -> ofy().save().entities(dbVote.withDir(dir), shard).now()); } }  void vote(String votableId, int dir, User user); CollectionResponse<Account> listVoters(@Nonnull String postId, Optional<Integer> limit,
      Optional<String> cursor);  }