Ejemplo n.º 1
0
 public Vote Add(Vote item)
 {
     ContextPerRequest.Db.Vote.Add(item);
     return item;
 }
Ejemplo n.º 2
0
 public void Delete(Vote item)
 {
     ContextPerRequest.Db.Vote.Remove(item);
 }
        private string MarkPostUpOrDown(Post post, Member postWriter, Member 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.MemberId != CurrentMember.Id))
                {

                    // Points to add or subtract to a user
                    var usersPoints = (postType == PostType.Negative) ?
                                        (-Settings.PointsDeductedForNegativeVote) : (Settings.PointsAddedForPositiveVote);

                    // Update the users points who wrote the post
                    ServiceFactory.MemberPointsService.Add(new MemberPoints
                    {
                        Points = usersPoints, 
                        Member = postWriter, 
                        MemberId = postWriter.Id,
                        RelatedPostId = post.Id
                    });

                    // Update the post with the new vote of the voter
                    var vote = new Vote
                    {
                        Post = post,
                        Member = voter,
                        MemberId = voter.Id,
                        Amount = (postType == PostType.Negative) ? (-1) : (1),
                        VotedByMember = CurrentMember,
                        DateVoted = DateTime.Now
                    };
                    ServiceFactory.VoteService.Add(vote);

                    // Update the post with the new points amount
                    var allVotes = post.Votes.ToList();
                    var allVoteCount = allVotes.Sum(x => x.Amount);
                    //var newPointTotal = (postType == PostType.Negative) ? (post.VoteCount - 1) : (post.VoteCount + 1);
                    post.VoteCount = allVoteCount;
                    var postTypeVoteCount = 0;
                    if (postType == PostType.Positive)
                    {
                        postTypeVoteCount = allVotes.Count(x => x.Amount > 0);
                    }
                    else
                    {
                        postTypeVoteCount =  allVotes.Count(x => x.Amount < 0);   
                    }
                    return string.Concat(postTypeVoteCount, ",", allVoteCount);
                }
            }
            return "0";
        }