/// <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> /// Contains logic to prevent a user is voting him or herself, otherwise apply votes and show proper image. /// </summary> /// <param name="answerid"></param> /// <param name="isUpVote"></param> private void UpdateVote(string answerid, bool isUpVote) { var answer = MongoHelper.GetCollection<Answer>("answers").AsQueryable().FirstOrDefault(a => a.AnswerId == new ObjectId(answerid)); if (answer == null) { return; } var answerer = MongoHelper.GetCollection<User>("users").AsQueryable().FirstOrDefault(u => u.UserId == answer.UserId); if (answerer == null) { return; } VoteViewModel voteviewModel = new VoteViewModel(); if (answerer.UserName.Equals(HttpContext.Current.User.Identity.Name) || !HttpContext.Current.User.Identity.IsAuthenticated) { if (isUpVote) { Clients.Caller.updateAnswerUpVoteImage(answerid,voteviewModel.NoUpVotePath); } else { Clients.Caller.updateAnswerDownVoteImage(answerid,voteviewModel.NoDownVotePath); } int currVote = answer.Vote.UserVotes.Sum(uv => uv.Upvote); Clients.Caller.updateAnswerVote(answerid,currVote, answerer.UserName.Equals(HttpContext.Current.User.Identity.Name) ? "You cannot vote your own answer" : "You must be logged in to vote"); } else { UserVote userVote = answer.Vote.UserVotes.FirstOrDefault(uv => uv.User.UserName == HttpContext.Current.User.Identity.Name); if (userVote == null) { NewAnswerVoteByUser(answer, voteviewModel, isUpVote); } else { ReestablishUserVoteChanges(answer, voteviewModel, userVote, isUpVote); } MongoHelper.GetCollection<Answer>("answers").Save(answer); int currVote = answer.Vote.UserVotes.Sum(uv => uv.Upvote); Clients.All.updateAnswerVote(answer.AnswerId.ToString(),currVote, ""); } }
/// <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; } } }
/// <summary> /// Contains logic to prevent a user is voting him or herself, otherwise apply votes and show proper image. /// </summary> /// <param name="questionid"></param> /// <param name="isUpVote"></param> private void UpdateVote(string questionid, bool isUpVote) { var question = MongoHelper.GetCollection<Question>("questions").AsQueryable().FirstOrDefault(q => q.QuestionId == new ObjectId(questionid)); if (question == null) { return; } var asker = MongoHelper.GetCollection<User>("users").AsQueryable().FirstOrDefault(u => u.UserId == question.UserId); if (asker == null) { return; } VoteViewModel voteviewModel = new VoteViewModel(); if (asker.UserName.Equals(HttpContext.Current.User.Identity.Name) || !HttpContext.Current.User.Identity.IsAuthenticated) { if (isUpVote) { Clients.Caller.updateQuestionUpVoteImage(voteviewModel.NoUpVotePath); } else { Clients.Caller.updateQuestionDownVoteImage(voteviewModel.NoDownVotePath); } int currVote = question.Vote.UserVotes.Sum(uv => uv.Upvote); Clients.Caller.updateQuestionVote(currVote, asker.UserName.Equals(HttpContext.Current.User.Identity.Name) ? "You cannot vote your own question" : "You must be logged in to vote"); } else { UserVote userVote = question.Vote.UserVotes.FirstOrDefault(uv => uv.User.UserName == HttpContext.Current.User.Identity.Name); if (userVote == null) { NewQuestionVoteByUser(question, voteviewModel, isUpVote); } else { ReestablishUserVoteChanges(question, voteviewModel, userVote, isUpVote); } MongoHelper.GetCollection<Question>("questions").Save(question); int currVote = question.Vote.UserVotes.Sum(uv => uv.Upvote); Clients.All.updateQuestionVote(currVote, ""); } }