@Test public void testListComments() throws Exception { final User user = UserServiceFactory.getUserService().getCurrentUser(); Comment comment1 = commentController.insertComment(postEntity.getWebsafeId(), "Test comment1", user); Comment comment2 = commentController.insertComment(postEntity.getWebsafeId(), "Test comment2", user); Comment comment3 = commentController.insertComment(postEntity.getWebsafeId(), "Test comment3", user); Comment comment4 = commentController.insertComment(postEntity.getWebsafeId(), "Test comment4", user); voteController.vote(comment1.getWebsafeId(), Vote.Direction.UP.getValue(), user); voteController.vote(comment2.getWebsafeId(), Vote.Direction.UP.getValue(), user); voteController.vote(comment3.getWebsafeId(), Vote.Direction.UP.getValue(), user); voteController.vote(comment4.getWebsafeId(), Vote.Direction.UP.getValue(), user); CollectionResponse<Comment> response = commentController.listComments(postEntity.getWebsafeId(), Optional.absent(), Optional.absent(), user); assertEquals(4, response.getItems().size()); for (Comment comment : response.getItems()) { assertEquals(2, comment.getShards().size()); assertEquals(1, comment.getVoteCount()); assertEquals(Vote.Direction.UP, comment.getDir()); } }
public CollectionResponse<Comment> listComments( String questionId, Optional<String> cursor, Optional<Integer> limit, User user) { final Key<Account> authKey = Key.create(user.getUserId()); final Key<PostEntity> questionKey = Key.create(questionId); Query<Comment> query = ofy().load() .group(Comment.ShardGroup.class) .type(Comment.class) .filter(Comment.FIELD_QUESTION_KEY + " =", questionKey) .order("-" + Comment.FIELD_CREATED); query = cursor.isPresent() ? query.startAt(Cursor.fromWebSafeString(cursor.get())) : query; query = query.limit(limit.or(DEFAULT_LIST_LIMIT)); final QueryResultIterator<Comment> qi = query.iterator(); List<Comment> comments = Lists.newArrayListWithCapacity(DEFAULT_LIST_LIMIT); while (qi.hasNext()) { Comment comment = qi.next(); if (!comment.isAccepted()) { comments.add(comment); } } if (!comments.isEmpty()) { comments = commentShardService.mergeShards(comments) .flatMap(comments1 -> voteService.checkCommentVote(comments1, authKey)) .blockingFirst(); } return CollectionResponse.<Comment>builder() .setItems(comments) .setNextPageToken(qi.getCursor().toWebSafeString()) .build(); }
CommentController extends Controller { public CollectionResponse<Comment> listComments( String questionId, Optional<String> cursor, Optional<Integer> limit, User user) { final Key<Account> authKey = Key.create(user.getUserId()); final Key<PostEntity> questionKey = Key.create(questionId); Query<Comment> query = ofy().load() .group(Comment.ShardGroup.class) .type(Comment.class) .filter(Comment.FIELD_QUESTION_KEY + " =", questionKey) .order("-" + Comment.FIELD_CREATED); query = cursor.isPresent() ? query.startAt(Cursor.fromWebSafeString(cursor.get())) : query; query = query.limit(limit.or(DEFAULT_LIST_LIMIT)); final QueryResultIterator<Comment> qi = query.iterator(); List<Comment> comments = Lists.newArrayListWithCapacity(DEFAULT_LIST_LIMIT); while (qi.hasNext()) { Comment comment = qi.next(); if (!comment.isAccepted()) { comments.add(comment); } } if (!comments.isEmpty()) { comments = commentShardService.mergeShards(comments) .flatMap(comments1 -> voteService.checkCommentVote(comments1, authKey)) .blockingFirst(); } return CollectionResponse.<Comment>builder() .setItems(comments) .setNextPageToken(qi.getCursor().toWebSafeString()) .build(); } }
CommentController extends Controller { public CollectionResponse<Comment> listComments( String questionId, Optional<String> cursor, Optional<Integer> limit, User user) { final Key<Account> authKey = Key.create(user.getUserId()); final Key<PostEntity> questionKey = Key.create(questionId); Query<Comment> query = ofy().load() .group(Comment.ShardGroup.class) .type(Comment.class) .filter(Comment.FIELD_QUESTION_KEY + " =", questionKey) .order("-" + Comment.FIELD_CREATED); query = cursor.isPresent() ? query.startAt(Cursor.fromWebSafeString(cursor.get())) : query; query = query.limit(limit.or(DEFAULT_LIST_LIMIT)); final QueryResultIterator<Comment> qi = query.iterator(); List<Comment> comments = Lists.newArrayListWithCapacity(DEFAULT_LIST_LIMIT); while (qi.hasNext()) { Comment comment = qi.next(); if (!comment.isAccepted()) { comments.add(comment); } } if (!comments.isEmpty()) { comments = commentShardService.mergeShards(comments) .flatMap(comments1 -> voteService.checkCommentVote(comments1, authKey)) .blockingFirst(); } return CollectionResponse.<Comment>builder() .setItems(comments) .setNextPageToken(qi.getCursor().toWebSafeString()) .build(); }  }
CommentController extends Controller { public CollectionResponse<Comment> listComments( String questionId, Optional<String> cursor, Optional<Integer> limit, User user) { final Key<Account> authKey = Key.create(user.getUserId()); final Key<PostEntity> questionKey = Key.create(questionId); Query<Comment> query = ofy().load() .group(Comment.ShardGroup.class) .type(Comment.class) .filter(Comment.FIELD_QUESTION_KEY + " =", questionKey) .order("-" + Comment.FIELD_CREATED); query = cursor.isPresent() ? query.startAt(Cursor.fromWebSafeString(cursor.get())) : query; query = query.limit(limit.or(DEFAULT_LIST_LIMIT)); final QueryResultIterator<Comment> qi = query.iterator(); List<Comment> comments = Lists.newArrayListWithCapacity(DEFAULT_LIST_LIMIT); while (qi.hasNext()) { Comment comment = qi.next(); if (!comment.isAccepted()) { comments.add(comment); } } if (!comments.isEmpty()) { comments = commentShardService.mergeShards(comments) .flatMap(comments1 -> voteService.checkCommentVote(comments1, authKey)) .blockingFirst(); } return CollectionResponse.<Comment>builder() .setItems(comments) .setNextPageToken(qi.getCursor().toWebSafeString()) .build(); }  Comment getComment(String commentId, User user); Comment insertComment(String postId, String content, User user); Comment updateComment(
      String postId,
      String commentId,
      Optional<String> content,
      Optional<Boolean> accepted,
      User user); void deleteComment(String postId, String commentId, User user); CollectionResponse<Comment> listComments(
      String questionId,
      Optional<String> cursor,
      Optional<Integer> limit,
      User user); }
CommentController extends Controller { public CollectionResponse<Comment> listComments( String questionId, Optional<String> cursor, Optional<Integer> limit, User user) { final Key<Account> authKey = Key.create(user.getUserId()); final Key<PostEntity> questionKey = Key.create(questionId); Query<Comment> query = ofy().load() .group(Comment.ShardGroup.class) .type(Comment.class) .filter(Comment.FIELD_QUESTION_KEY + " =", questionKey) .order("-" + Comment.FIELD_CREATED); query = cursor.isPresent() ? query.startAt(Cursor.fromWebSafeString(cursor.get())) : query; query = query.limit(limit.or(DEFAULT_LIST_LIMIT)); final QueryResultIterator<Comment> qi = query.iterator(); List<Comment> comments = Lists.newArrayListWithCapacity(DEFAULT_LIST_LIMIT); while (qi.hasNext()) { Comment comment = qi.next(); if (!comment.isAccepted()) { comments.add(comment); } } if (!comments.isEmpty()) { comments = commentShardService.mergeShards(comments) .flatMap(comments1 -> voteService.checkCommentVote(comments1, authKey)) .blockingFirst(); } return CollectionResponse.<Comment>builder() .setItems(comments) .setNextPageToken(qi.getCursor().toWebSafeString()) .build(); }  Comment getComment(String commentId, User user); Comment insertComment(String postId, String content, User user); Comment updateComment(
      String postId,
      String commentId,
      Optional<String> content,
      Optional<Boolean> accepted,
      User user); void deleteComment(String postId, String commentId, User user); CollectionResponse<Comment> listComments(
      String questionId,
      Optional<String> cursor,
      Optional<Integer> limit,
      User user);  }