public bool AddVote(PollVoteModel model)
        {
            var dbEvent = this.GetDbEventByCode(model.EventCode);

            if (dbEvent == null)
            {
                return(false);
            }

            if (!int.TryParse(model.Option, out int id))
            {
                return(false);
            }

            var pollAnswer = this.db.PollAnswers
                             .FirstOrDefault(pa => pa.Id == id && pa.PollId == model.PollId);

            if (pollAnswer == null)
            {
                return(false);
            }

            pollAnswer.Votes += 1;

            this.db.SaveChanges();

            return(true);
        }
Esempio n. 2
0
        public IActionResult Vote(PollVoteModel model)
        {
            if (this.ModelState.IsValid)
            {
                var voted = this.service.AddVote(model);

                if (voted)
                {
                    string pollId = model.PollId.ToString();

                    this.HttpContext.Session.SetPollId(pollId, VoteConstants.Voted);
                }
            }

            return(RedirectToAction("PollResults", new { id = model.PollId, code = model.EventCode }));
        }
Esempio n. 3
0
        public void WithValidModel_VoteAddedToSelectedAnswer()
        {
            var pollOptionId = 1;

            var votesBefore = this.db.PollAnswers.Find(pollOptionId).Votes;

            var model = new PollVoteModel()
            {
                PollId = 99, EventCode = "001", Option = pollOptionId.ToString()
            };

            this.service.AddVote(model);

            var votesAfter = this.db.PollAnswers.Find(pollOptionId).Votes;

            Assert.AreEqual(0, votesBefore);
            Assert.AreEqual(1, votesAfter);
        }