Exemplo n.º 1
0
        private void MarkPostUpOrDown(Post post, MembershipUser postWriter, MembershipUser voter, PostType postType)
        {
            // Check this user is not the post owner
            if (voter.Id != postWriter.Id)
            {
                // Not the same person, now check they haven't voted on this post before
                if (post.Votes.All(x => x.User.Id != LoggedOnUser.Id))
                {
                    // Points to add or subtract to a user
                    var usersPoints = (postType == PostType.Negative) ?
                                      (-SettingsService.GetSettings().PointsDeductedNagativeVote) : (SettingsService.GetSettings().PointsAddedPostiveVote);

                    // Update the users points who wrote the post
                    _membershipUserPointsService.Add(new MembershipUserPoints {
                        Points = usersPoints, User = postWriter
                    });

                    // Update the post with the new vote of the voter
                    var vote = new Vote
                    {
                        Post   = post,
                        User   = voter,
                        Amount = (postType == PostType.Negative) ? (-1) : (1),
                        VotedByMembershipUser = LoggedOnUser,
                        DateVoted             = DateTime.Now
                    };
                    _voteService.Add(vote);

                    // Update the post with the new points amount
                    var newPointTotal = (postType == PostType.Negative) ? (post.VoteCount - 1) : (post.VoteCount + 1);
                    post.VoteCount = newPointTotal;
                }
            }
        }
Exemplo n.º 2
0
        private async Task <bool> MarkPostUpOrDown(Post post, MembershipUser postWriter, MembershipUser voter, PostType postType,
                                                   MembershipUser loggedOnReadOnlyUser)
        {
            var settings = SettingsService.GetSettings();

            // Check this user is not the post owner
            if (voter.Id != postWriter.Id)
            {
                // Not the same person, now check they haven't voted on this post before
                var votes = post.Votes.Where(x => x.VotedByMembershipUser.Id == loggedOnReadOnlyUser.Id).ToList();
                if (votes.Any())
                {
                    // Already voted, so delete the vote and remove the points
                    var votesToDelete = new List <Vote>();
                    votesToDelete.AddRange(votes);
                    foreach (var vote in votesToDelete)
                    {
                        _voteService.Delete(vote);
                    }

                    // Update the post with the new points amount
                    var newPointTotal = postType == PostType.Negative ? post.VoteCount + 1 : post.VoteCount - 1;
                    post.VoteCount = newPointTotal;
                }
                else
                {
                    // Points to add or subtract to a user
                    var usersPoints = postType == PostType.Negative
                        ? -settings.PointsDeductedNagativeVote
                        : settings.PointsAddedPostiveVote;

                    // Update the post with the new vote of the voter
                    var vote = new Vote
                    {
                        Post   = post,
                        User   = postWriter,
                        Amount = postType == PostType.Negative ? -1 : 1,
                        VotedByMembershipUser = voter,
                        DateVoted             = DateTime.UtcNow
                    };
                    _voteService.Add(vote);

                    // Update the users points who wrote the post
                    await _membershipUserPointsService.Add(new MembershipUserPoints
                    {
                        Points      = usersPoints,
                        User        = postWriter,
                        PointsFor   = PointsFor.Vote,
                        PointsForId = vote.Id
                    });

                    // Update the post with the new points amount
                    var newPointTotal = postType == PostType.Negative ? post.VoteCount - 1 : post.VoteCount + 1;
                    post.VoteCount = newPointTotal;
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Vote([Bind("Choice")] Vote vote)
        {
            // Try to save vote
            voteService.Add(vote);

            // Always redirect to Index
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> VoteOnPost(string id, [Required] string voteType)
        {
            var userId = User.FindFirst(ClaimTypes.Name)?.Value;

            // Check if the request sent a valid vote
            if (!Enum.IsDefined(typeof(VoteType), voteType))
            {
                return(BadRequest(new { message = "Invalid vote" }));
            }
            // Create var with enum value
            Enum.TryParse(voteType, out VoteType vote);

            var post = _postService.Get(id);

            if (post == null)
            {
                return(NotFound(new { message = "Post was not found" }));
            }

            // Used to determine if a vote should be cancelled or reversed if it exists
            var foundVote = await _voteService.FindByUserAndResponseId(userId, post.Id);

            if (foundVote == null)
            {
                // No vote has been registered, so create one and add to tally
                post = _postService.VoteOnPost(post, vote);
                await _voteService.Add(new Vote
                {
                    UserVote   = vote,
                    UserId     = userId,
                    ResponseId = post.Id
                });
            }
            else if (foundVote.UserVote == vote)
            {
                // Clear vote if same vote was selected

                // Set to opposite vote to negate user's vote
                var negatingVote = vote == VoteType.Up ? VoteType.Down : VoteType.Up;

                post = _postService.VoteOnPost(post, negatingVote);
                await _voteService.Remove(foundVote.Id);
            }
            else
            {
                // Can be implied at this point that the user changed their vote
                // to the opposite

                // Since they chose the opposite vote, offset is set to two
                post = _postService.VoteOnPost(post, vote, 2);

                // Set vote document to the opposite vote
                foundVote.UserVote = vote;
                await _voteService.Update(foundVote);
            }

            return(Ok(new VoteResponseDTO
            {
                UserVote = Enum.GetName(typeof(VoteType), vote),
                NumVotes = post.NumVotes
            }));
        }