예제 #1
0
        protected override async Task <VoteView> HandleInput(VoteOnPostParams input)
        {
            using (var connection = database.GetConnection()) {
                IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);
                IPostRepo postRepo = database.GetRepo <IPostRepo>(connection);
                IUserRepo userRepo = database.GetRepo <IUserRepo>(connection);

                using (var transaction = connection.BeginTransaction()) {
                    Post post    = (await postRepo.FindById(input.PostId)) !;
                    Vote?oldVote = await voteRepo.FindByUserAndPost(input.User.Username, input.PostId);


                    // Wipe out the old one...
                    if (oldVote != null)
                    {
                        post.RemoveVote(oldVote.Direction);
                        await voteRepo.Delete(oldVote);

                        if (post.Type != PostType.Text)
                        {
                            post.User.PostKarma -= (int)oldVote.Direction;
                        }
                    }

                    // Create the new vote, and update the comment's karma cache.
                    Vote newVote = new Vote()
                    {
                        User         = input.User,
                        ResourceType = VoteResourceType.Post,
                        ResourceId   = input.PostId,
                        Direction    = input.Vote
                    };

                    post.AddVote(newVote.Direction);

                    if (post.Type != PostType.Text)
                    {
                        post.User.PostKarma += (int)newVote.Direction;
                    }

                    await voteRepo.Add(newVote);

                    await postRepo.Update(post);

                    await userRepo.Update(post.User);

                    transaction.Commit();
                    return(voteViewMapper.Map(newVote));
                }
            }
        }
예제 #2
0
        public async Task <Vote> VoteOnPost(VoteOnPost data, User user)
        {
            Vote?oldVote = await repo.FindByUserAndPost(user.Username, data.PostId);

            if (oldVote != null)
            {
                await repo.Delete(oldVote);
            }

            Vote newVote = factory.CreateForPost(user, data.PostId, data.VoteDirection);
            await repo.Add(newVote);

            await bus.Dispatch(new VoteOnPostEvent(data.PostId, newVote, oldVote));

            return(newVote);
        }
예제 #3
0
        protected override async Task <PagedResultSet <PostView> > HandleInput(FindByValueParams <string> input)
        {
            using (var connection = database.GetConnection()) {
                IPostRepo postRepo = database.GetRepo <IPostRepo>(connection);
                IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);

                PagedResultSet <Post> posts = await postRepo.FindByUser(input.Value, input.Pagination?.PageNumber ?? 0, input.Pagination?.PageSize ?? Post.PageSize);

                if (input.User != null)
                {
                    foreach (Post p in posts)
                    {
                        p.Vote = await voteRepo.FindByUserAndPost(input.User.Username, p.Id);
                    }
                }

                return(new PagedResultSet <PostView>(posts.Items.Select(p => postMapper.Map(p)), posts.Pagination));
            }
        }
예제 #4
0
        protected async override Task <PostView?> HandleInput(FindByValueParams <int> input)
        {
            using (var connection = database.GetConnection()) {
                IPostRepo postRepo = database.GetRepo <IPostRepo>(connection);
                IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);

                Post?post = await postRepo.FindById(input.Value);

                if (post == null)
                {
                    return(null);
                }

                //Pull in the vote if needed.
                if (input.User != null)
                {
                    post.Vote = await voteRepo.FindByUserAndPost(input.User.Username, input.Value);
                }

                return(postMapper.Map(post));
            }
        }