コード例 #1
0
        //add vote to one answer in poll
        public void Vote(VotePollModelView votePollModelView, ApplicationDbContext db)
        {
            var pollAnswer = db.PollAnswers.First(x => x.Id == votePollModelView.IdAnswer);

            pollAnswer.Votes++;
            db.SaveChanges();
        }
コード例 #2
0
        public ActionResult PollVote(VotePollModelView votePollModelView)
        {
            var poll       = db.Polls.Find(votePollModelView.IdPoll);
            var pollAnswer = db.PollAnswers.Find(votePollModelView.IdAnswer);

            if (poll == null || pollAnswer == null)
            {
                return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest));
            }
            if (!IsPrivPollAuthorised(poll))
            {
                return(RedirectToAction("PrivatePollAuth", "PrivatePoll", new { id = votePollModelView.IdPoll }));
            }

            if (poll.UserChecking)   //checking if user is logged in
            {
                if (!User.Identity.IsAuthenticated)
                {
                    return(new HttpStatusCodeResult(System.Net.HttpStatusCode.Unauthorized));
                }
                else
                {
                    var user = appUserManager.FindById(User.Identity.GetUserId());
                    if (user.VotedPoll.Any(x => x.Poll == poll))
                    {
                        ModelState.AddModelError("UserAlreadyVote", "You have already voted in this poll.");
                        return(View(poll));//if user have already voted on this poll return error
                    }
                    else
                    {
                        user.VotedPoll.Add(pollAnswer);   //adding pollAnswer entity to list of Votedpoll of user
                    }
                }
            }
            //to do ip or cookie
            pollManager.Vote(votePollModelView, db);
            return(RedirectToAction("PollResult", new { id = poll.Id }));
        }