Beispiel #1
0
        public async Task <ActionResult <string> > Post(int id, [FromBody] PollVotePostViewModel vote)
        {
            var result = await _pollService.VotePollAsync(id, vote);

            if (result == null)
            {
                return(NotFound());
            }

            return(result);
        }
        public async Task <ActionResult <string> > VotePollAsync(int id, [FromBody] PollVotePostViewModel vote)
        {
            var poll = await _dataContext.Polls
                       .Include(x => x.Options)
                       .SingleOrDefaultAsync(x => x.poll_id == id && x.Options.Count(y => y.option_id == vote.option_id) == 1);

            if (poll == null)
            {
                return(null);
            }

            poll.Options.First(x => x.option_id == vote.option_id).option_votes += 1;

            _dataContext.Update(poll);
            await _dataContext.SaveChangesAsync();

            return("Registered Vote!");
        }