예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        protected async override Task <CommentView> HandleInput(CommentCreateParams input)
        {
            using (var connection = database.GetConnection()) {
                IPostRepo    postRepo    = database.GetRepo <IPostRepo>(connection);
                ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection);
                IVoteRepo    voteRepo    = database.GetRepo <IVoteRepo>(connection);

                // Locate the post to ensure it actually exists.
                Post?post = await postRepo.FindById(input.PostId);

                if (post == null)
                {
                    throw new InvalidOperationException();
                }

                using (var transaction = connection.BeginTransaction()) {
                    Comment comment = new Comment()
                    {
                        User         = input.User,
                        PostId       = post.Id,
                        Body         = input.Body,
                        CreationDate = DateTime.UtcNow
                    };

                    // Set the parent comment if needed.
                    if (input.ParentId != 0)
                    {
                        comment.Parent = await commentRepo.FindById(input.ParentId);
                    }

                    // Update the comment count cache on the post.
                    post.CommentCount++;

                    comment.Upvotes++;
                    await commentRepo.Add(comment);

                    Vote upvote = new Vote()
                    {
                        User         = input.User,
                        ResourceId   = comment.Id,
                        ResourceType = VoteResourceType.Comment,
                        Direction    = VoteDirection.Up
                    };

                    await postRepo.Update(post);

                    await voteRepo.Add(upvote);

                    comment.Vote = upvote;
                    transaction.Commit();

                    return(commentMapper.Map(comment));
                }
            }
        }
예제 #3
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));
                }
            }
        }
예제 #4
0
        protected override async Task <PostView?> HandleInput(PostCreateParams input)
        {
            using (var connection = database.GetConnection()) {
                ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection);
                IPostRepo  postRepo  = database.GetRepo <IPostRepo>(connection);
                IVoteRepo  voteRepo  = database.GetRepo <IVoteRepo>(connection);

                Space?space = await spaceRepo.FindByName(input.Space);

                if (space == null)
                {
                    throw new InvalidOperationException($"No space with name ${input.Space} found.");
                }

                using (var transaction = connection.BeginTransaction()) {
                    Post post = new Post()
                    {
                        Type         = input.Type,
                        Title        = input.Title,
                        Body         = input.Body,
                        User         = input.User,
                        CreationDate = DateTime.UtcNow,
                        Space        = space
                    };

                    if (post.Type == PostType.Link && !System.Text.RegularExpressions.Regex.IsMatch(post.Body, Regex.UrlProtocol))
                    {
                        post.Body = $"http://{post.Body}";
                    }

                    // Not liking these count caches. Makes no sense?
                    post.Upvotes++;
                    await postRepo.Add(post);

                    Vote upvote = new Vote()
                    {
                        User         = input.User,
                        ResourceId   = post.Id,
                        ResourceType = VoteResourceType.Post,
                        Direction    = VoteDirection.Up
                    };

                    await voteRepo.Add(upvote);

                    post.Vote = upvote;

                    transaction.Commit();
                    return(postMapper.Map(post));
                }
            }
        }
예제 #5
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));
            }
        }
예제 #6
0
        protected async override Task <IEnumerable <CommentView> > HandleInput(FindByValueParams <int> input)
        {
            using (var connection = database.GetConnection()) {
                ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection);

                IEnumerable <Comment> comments = await commentRepo.FindByPost(input.Value);

                if (input.User != null)
                {
                    IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);

                    foreach (Comment c in comments)
                    {
                        await GetVotes(voteRepo, c, input.User);
                    }
                }

                return(comments.Select(c => commentMapper.Map(c)));
            }
        }
예제 #7
0
        protected async override Task <CommentView?> HandleInput(FindByValueParams <int> input)
        {
            using (var connection = database.GetConnection()) {
                ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection);

                Comment?c = await commentRepo.FindById(input.Value);

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

                if (input.User != null)
                {
                    IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);
                    await GetVotes(voteRepo, c, input.User);
                }

                return(commentMapper.Map(c));
            }
        }
예제 #8
0
        protected async override Task <PagedResultSet <CommentView> > HandleInput(FindByValueParams <string> input)
        {
            using (var connection = database.GetConnection()) {
                ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection);

                PagedResultSet <Comment> comments = await commentRepo.FindByUser(input.Value, input.Pagination?.PageNumber ?? 0, input.Pagination?.PageSize ?? Comment.PageSize);

                if (input.User != null)
                {
                    foreach (Comment c in comments)
                    {
                        IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection);
                        await GetVotes(voteRepo, c, input.User);
                    }
                }

                return(new PagedResultSet <CommentView>(
                           comments.Select(c => commentMapper.Map(c)),
                           comments.Pagination
                           ));
            }
        }
예제 #9
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));
            }
        }
예제 #10
0
 public VoteService(IEventBus bus, IVoteFactory factory, IVoteRepo repo)
 {
     this.bus     = bus;
     this.factory = factory;
     this.repo    = repo;
 }
예제 #11
0
 public ResultRoutine(IPollRepo pollRepo, IOptionRepo optionRepo, IVoteRepo voteRepo)
 {
     _pollRepo   = pollRepo;
     _optionRepo = optionRepo;
     _voteRepo   = voteRepo;
 }
예제 #12
0
 public VotesController(IVoteRepo voteRepo, IUserRepo userRepo)
 {
     _voteRepo = voteRepo;
     _userRepo = userRepo;
 }
예제 #13
0
 public PlayerService(IPlayerRepository playerRepo, IVoteRepo voteRepoet)
 {
     voteRepo = voteRepoet;
     repo     = playerRepo;
 }
예제 #14
0
 public PlayerController(IPlayerService playerService, IVoteRepo voteRepo)
 {
     service   = playerService;
     _voteRepo = voteRepo;
 }