/// <summary> /// Recursive helper to get the votes for all children. /// </summary> private async Task GetVotes(IVoteRepo voteRepo, Comment comment, User user) { comment.Vote = await voteRepo.FindByUserAndComment(user.Username, comment.Id); foreach (Comment child in comment.Children) { await GetVotes(voteRepo, child, user); } }
public async Task <Vote> VoteOnComment(VoteOnComment data, User user) { Vote?oldVote = await repo.FindByUserAndComment(user.Username, data.CommentId); if (oldVote != null) { await repo.Delete(oldVote); } Vote newVote = factory.CreateForComment(user, data.CommentId, data.VoteDirection); await repo.Add(newVote); await bus.Dispatch(new VoteOnCommentEvent(data.CommentId, newVote, oldVote)); return(newVote); }
protected override async Task <VoteView> HandleInput(VoteOnCommentParams input) { using (var connection = database.GetConnection()) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); IUserRepo userRepo = database.GetRepo <IUserRepo>(connection); using (var transaction = connection.BeginTransaction()) { Comment comment = (await commentRepo.FindById(input.CommentId)) !; Vote? oldVote = await voteRepo.FindByUserAndComment(input.User.Username, input.CommentId); // Wipe out the old one... if (oldVote != null) { comment.RemoveVote(oldVote.Direction); await voteRepo.Delete(oldVote); comment.User.CommentKarma -= (int)oldVote.Direction; } // Create the new vote, and update the comment's karma cache. Vote newVote = new Vote() { User = input.User, ResourceType = VoteResourceType.Comment, ResourceId = input.CommentId, Direction = input.Vote }; comment.AddVote(newVote.Direction); comment.User.CommentKarma += (int)newVote.Direction; await voteRepo.Add(newVote); await commentRepo.Update(comment); await userRepo.Update(comment.User); transaction.Commit(); return(voteViewMapper.Map(newVote)); } } }