public async Task <IActionResult> Put(int id, [FromBody] UpdatePollViewModel model) { if (ModelState.IsValid) { Poll poll = this.unitOfWork.PollsRepository.GetById(id); if (poll == null) { return(BadRequest()); } else { poll.Active = model.Active; poll.ActiveUntil = model.ActiveUntil; poll.Question = model.Question; this.unitOfWork.PollsRepository.Update(poll); await this.unitOfWork.Save(); return(NoContent()); } } else { return(UnprocessableEntity()); } }
public virtual PartialViewResult UpdatePoll(UpdatePollViewModel updatePollViewModel) { try { User.GetMembershipUser(MembershipService); // Fist need to check this user hasn't voted already and is trying to fudge the system if (LoggedOnReadOnlyUser != null && !_pollService.HasUserVotedAlready(updatePollViewModel.AnswerId, LoggedOnReadOnlyUser.Id)) { var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser?.Id); // Get the answer var pollAnswer = _pollService.GetPollAnswer(updatePollViewModel.AnswerId); // create a new vote var pollVote = new PollVote { PollAnswer = pollAnswer, User = loggedOnUser }; // Add it _pollService.Add(pollVote); // Update the context so the changes are reflected in the viewmodel below Context.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 == LoggedOnReadOnlyUser?.Id) > 0; var viewModel = new PollViewModel { Poll = poll, TotalVotesInPoll = votes.Count(), UserHasAlreadyVoted = alreadyVoted }; // Commit the transaction Context.SaveChanges(); return(PartialView("_Poll", viewModel)); } catch (Exception ex) { Context.RollBack(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("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 (!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")); } } }
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, LoggedOnReadOnlyUser.Id)) { var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser.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 == LoggedOnReadOnlyUser.Id) > 0); var viewModel = new PollViewModel { 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")); } } }