public ActionResult Show(int?id) { if (id == null) { return(HttpNotFound()); } var poll = _context.Polls.SingleOrDefault(p => p.Id == id); if (poll == null) { return(HttpNotFound()); } var options = _context.Options.Where(o => o.PollId == poll.Id).ToList(); var viewModel = new ShowPollViewModel() { Poll = poll, NewOption = new Option(), VoteOptions = options }; return(View(viewModel)); }
public ActionResult PutOption(ShowPollViewModel viewModel) { if (viewModel.NewOption.Name == null) { return(RedirectToAction("Show", "Polls", new { id = viewModel.Poll.Id })); } var newOption = viewModel.NewOption; newOption.PollId = viewModel.Poll.Id; _context.Options.Add(newOption); _context.SaveChanges(); return(RedirectToAction("Show", "Polls", new { id = viewModel.Poll.Id })); }
public ActionResult Voted(ShowPollViewModel viewModel) { var optionInDb = _context.Options.SingleOrDefault(o => o.Id == viewModel.VoteId); optionInDb.Votes++; _context.SaveChanges(); var newViewModel = new VotedViewModel() { Poll = viewModel.Poll, VotedFor = optionInDb.Name, }; return(View(newViewModel)); }
public PartialViewResult UpdatePoll(UpdatePollViewModel updatePollViewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { // Fist need to check this user hasn't voted already and is trying to fudge the system if (!ServiceFactory.PollService.HasUserVotedAlready(updatePollViewModel.AnswerId, CurrentMember.Id)) { // Get the answer var pollAnswer = ServiceFactory.PollService.GetAnswer(updatePollViewModel.AnswerId); // create a new vote var pollVote = new PollVote { PollAnswer = pollAnswer, Member = CurrentMember, MemberId = CurrentMember.Id }; // Add it ServiceFactory.PollService.Add(pollVote); // Update the context so the changes are reflected in the viewmodel below unitOfWork.SaveChanges(); } // Create the view model and get ready return the poll partial view var poll = ServiceFactory.PollService.Get(updatePollViewModel.PollId); var votes = poll.PollAnswers.SelectMany(x => x.PollVotes).ToList(); var alreadyVoted = (votes.Count(x => x.MemberId == CurrentMember.Id) > 0); var viewModel = new ShowPollViewModel { Poll = poll, TotalVotesInPoll = votes.Count(), UserHasAlreadyVoted = alreadyVoted }; // Commit the transaction unitOfWork.Commit(); return(PartialView(PathHelper.GetThemePartialViewPath("Poll"), viewModel)); } catch (Exception ex) { unitOfWork.Rollback(); LogError(ex); throw new Exception(Lang("Errors.GenericMessage")); } } }
public PartialViewResult UpdatePoll(UpdatePollViewModel updatePollViewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { // Fist need to check this user hasn't voted already and is trying to fudge the system if (!_pollVoteService.HasUserVotedAlready(updatePollViewModel.AnswerId, LoggedOnUser.Id)) { // Get the answer var pollAnswer = _pollAnswerService.Get(updatePollViewModel.AnswerId); // create a new vote var pollVote = new PollVote { PollAnswer = pollAnswer, User = LoggedOnUser }; // Add it _pollVoteService.Add(pollVote); // Update the context so the changes are reflected in the viewmodel below unitOfWork.SaveChanges(); } // Create the view model and get ready return the poll partial view var poll = _pollService.Get(updatePollViewModel.PollId); var votes = poll.PollAnswers.SelectMany(x => x.PollVotes).ToList(); var alreadyVoted = (votes.Count(x => x.User.Id == LoggedOnUser.Id) > 0); var viewModel = new ShowPollViewModel { Poll = poll, TotalVotesInPoll = votes.Count(), UserHasAlreadyVoted = alreadyVoted }; // Commit the transaction unitOfWork.Commit(); return(PartialView("_Poll", viewModel)); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } }