Example #1
0
        public ActionResult NewAnswer(AnswerModel answerModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Enter a valid answer.");
                return View("AnswerBox", answerModel);
            }

            var question =
                MongoHelper.GetCollection<Question>("questions").AsQueryable().FirstOrDefault(
                    q => q.QuestionId == new ObjectId(answerModel.QuestionId));

            //if the question id was tampered with
            if (question == null)
            {
                ModelState.AddModelError("", "Invalid question id");
                return View("AnswerBox", answerModel);
            }

            var user =
                MongoHelper.GetCollection<User>("users").AsQueryable().First(
                    u => u.UserName == HttpContext.User.Identity.Name);

            Answer newAnswer = new Answer();
            newAnswer.Body = answerModel.Answer;
            newAnswer.UserId = user.UserId;
            newAnswer.DateAnswered = DateTime.Now.ToShortDateString();
            newAnswer.QuestionId = question.QuestionId;
            var saveResult = MongoHelper.GetCollection<Question>("answers").Save(newAnswer);

            if(!saveResult.Ok)
            {
                ModelState.AddModelError("",saveResult.ErrorMessage);
                return View("AnswerBox", answerModel.QuestionId);
            }

            GlobalHost.ConnectionManager.GetHubContext<AnswerHub>().Clients.All.showAnswer(new AnswerViewModel
                                                                                                   {
                                                                                                       Answer = newAnswer,
                                                                                                       Answerer = user,
                                                                                                       VoteViewModel =new VoteViewModel()
                                                                                                   });
                return RedirectToAction("AnswerBox", new {questionid = answerModel.QuestionId});
        }
Example #2
0
        /// <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);
            }
        }
Example #3
0
        /// <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;
                }
            }
        }
Example #4
0
 /// <summary>
 /// Publishes a new answer to the client
 /// </summary>
 /// <param name="answer"></param>
 public void ShowAnswer(Answer answer)
 {
     Clients.Caller.showAnswer(answer.ToJson());
 }