public ActionResult Create(Question question) { if (!ModelState.IsValid) { return View(question); } var user =MongoHelper.GetCollection<User>("users").AsQueryable().First(u => u.UserName == HttpContext.User.Identity.Name); var questions = MongoHelper.GetCollection<Question>("questions"); question.DateAsked = DateTime.Now.ToShortDateString(); question.UserId = user.UserId; var saveResult =questions.Save(question); if(!saveResult.Ok) { ModelState.AddModelError("", saveResult.ErrorMessage); return View(question); } var questionViewModel = MongoHelper.GetQuestionViewModel(question, HttpContext.User.Identity.Name); var jsonString = questionViewModel == null ? "{}" : questionViewModel.ToJson(); GlobalHost.ConnectionManager.GetHubContext<QuestionHub>().Clients.All.showQuestion(jsonString); return RedirectToAction("Create"); }
/// <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="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; } } }