Example #1
0
        public async Task <Poll> CreatePoll(AddPollInputModel model, string id)
        {
            Poll newPoll = new Poll()
            {
                Content       = model.Content,
                ClubId        = id,
                IsMultichoice = model.IsMultichoice.ToLower() == "true" ? true
                : false,
                ExpiredDate = model.ExpiredDate,
            };

            List <string> options = model.Options.Split("~", StringSplitOptions.RemoveEmptyEntries)
                                    .ToList();

            foreach (var option in options)
            {
                Option newOption = new Option()
                {
                    VotesCount = 0,
                    Content    = option,
                    PollId     = newPoll.Id,
                };

                newPoll.Options.Add(newOption);
            }

            var result = await this.dbContext.Polls.AddAsync(newPoll);

            await this.dbContext.SaveChangesAsync();

            return(result.Entity);
        }
        public async Task <IActionResult> AddPoll(AddPollInputModel model)
        {
            if (ModelState.IsValid && model.ExpiredDate.Subtract(DateTime.UtcNow).Hours > 0)
            {
                await this.pollService.CreatePoll(model, model.ClubId);

                return(this.Redirect($"/Club/Polls/{model.ClubId}"));
            }

            return(this.Redirect($"/Club/Polls/{model.ClubId}?validation=true"));
        }
        public async Task <IActionResult> AddVote(AddPollInputModel model)
        {
            string userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (model.Votes?.Any() == true)
            {
                await this.pollService.AddVote(model.Votes, model.PollId, userId);

                return(this.Redirect($"/Club/Polls/{model.ClubId}"));
            }

            return(this.Redirect($"/Club/Polls/{model.ClubId}"));
        }