/// <summary> /// Check if a user is making a new vote /// </summary> /// <param name="answer"></param> /// <param name="voteviewModel"></param> /// <param name="isUpVote"></param> private void NewAnswerVoteByUser(Answer answer, VoteViewModel voteviewModel, bool isUpVote) { UserVote userVote = new UserVote(); userVote.User = MongoHelper.GetCollection<User>("users").AsQueryable().FirstOrDefault( u => u.UserName == HttpContext.Current.User.Identity.Name); userVote.Upvote += (isUpVote) ? AnswerUpvote : AnswerDownVote; answer.Vote.UserVotes.Add(userVote); if (isUpVote) { userVote.SelectedUpVotePath = voteviewModel.SelectedUpVotePath = voteviewModel.UpVotePath; Clients.Caller.updateAnswerUpVoteImage(answer.AnswerId.ToString(),voteviewModel.SelectedUpVotePath); } else { userVote.SelectedDownVotePath = voteviewModel.SelectedDownVotePath = voteviewModel.DownVotePath; Clients.Caller.updateAnswerDownVoteImage(answer.AnswerId.ToString(),voteviewModel.SelectedDownVotePath); } }
/// <summary> /// Check if a user is making a new vote /// </summary> /// <param name="question"></param> /// <param name="voteviewModel"></param> /// <param name="isUpVote"></param> private void NewQuestionVoteByUser(Question question, VoteViewModel voteviewModel, bool isUpVote) { UserVote userVote = new UserVote(); userVote.User = MongoHelper.GetCollection<User>("users").AsQueryable().FirstOrDefault( u => u.UserName == HttpContext.Current.User.Identity.Name); userVote.Upvote += (isUpVote) ? QuestionUpvote : QuestionDownVote; question.Vote.UserVotes.Add(userVote); if (isUpVote) { userVote.SelectedUpVotePath = voteviewModel.SelectedUpVotePath = voteviewModel.UpVotePath; Clients.Caller.updateQuestionUpVoteImage(voteviewModel.SelectedUpVotePath); } else { userVote.SelectedDownVotePath = voteviewModel.SelectedDownVotePath = voteviewModel.DownVotePath; Clients.Caller.updateQuestionDownVoteImage(voteviewModel.SelectedDownVotePath); } }
/// <summary> /// User is making a change to an existing vote /// </summary> /// <param name="answer"></param> /// <param name="voteviewModel"></param> /// <param name="userVote"></param> /// <param name="isUpVote"></param> private void ReestablishUserVoteChanges(Answer answer, VoteViewModel voteviewModel, UserVote userVote, bool isUpVote) { if (isUpVote) { userVote.SelectedDownVotePath = ""; if (userVote.Upvote == AnswerUpvote) { Clients.Caller.updateAnswerUpVoteImage(answer.AnswerId.ToString(),voteviewModel.NoUpVotePath); userVote.SelectedUpVotePath = ""; userVote.Upvote = 0; answer.Vote.UserVotes.Remove(userVote); } else { Clients.Caller.updateAnswerUpVoteImage(answer.AnswerId.ToString(),voteviewModel.UpVotePath); userVote.SelectedUpVotePath = voteviewModel.UpVotePath; userVote.Upvote = AnswerUpvote; } } else { userVote.SelectedUpVotePath = ""; if (userVote.Upvote == AnswerDownVote) { Clients.Caller.updateAnswerDownVoteImage(answer.AnswerId.ToString(),voteviewModel.NoDownVotePath); userVote.SelectedDownVotePath = ""; userVote.Upvote = 0; answer.Vote.UserVotes.Remove(userVote); } else { Clients.Caller.updateAnswerDownVoteImage(answer.AnswerId.ToString(),voteviewModel.DownVotePath); userVote.SelectedDownVotePath = voteviewModel.DownVotePath; userVote.Upvote = AnswerDownVote; } } }
/// <summary> /// User is making a change to an existing vote /// </summary> /// <param name="question"></param> /// <param name="voteviewModel"></param> /// <param name="userVote"></param> /// <param name="isUpVote"></param> private void ReestablishUserVoteChanges(Question question, VoteViewModel voteviewModel, UserVote userVote, bool isUpVote) { if (isUpVote) { userVote.SelectedDownVotePath = ""; if (userVote.Upvote == QuestionUpvote) { Clients.Caller.updateQuestionUpVoteImage(voteviewModel.NoUpVotePath); userVote.SelectedUpVotePath = ""; userVote.Upvote = 0; question.Vote.UserVotes.Remove(userVote); } else { Clients.Caller.updateQuestionUpVoteImage(voteviewModel.UpVotePath); userVote.SelectedUpVotePath = voteviewModel.UpVotePath; userVote.Upvote = QuestionUpvote; } } else { userVote.SelectedUpVotePath = ""; if (userVote.Upvote == QuestionDownVote) { Clients.Caller.updateQuestionDownVoteImage(voteviewModel.NoDownVotePath); userVote.SelectedDownVotePath = ""; userVote.Upvote = 0; question.Vote.UserVotes.Remove(userVote); } else { Clients.Caller.updateQuestionDownVoteImage(voteviewModel.DownVotePath); userVote.SelectedDownVotePath = voteviewModel.DownVotePath; userVote.Upvote = QuestionDownVote; } } }