Beispiel #1
0
        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);
        }
Beispiel #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));
                }
            }
        }
Beispiel #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));
                }
            }
        }
Beispiel #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));
                }
            }
        }