public ActionResult Save(NewPollViewModel newPoll)
        {
            string currentUserId = User.Identity.GetUserId();

            var currentUserInfo = _context.Users.SingleOrDefault(u => u.Id == currentUserId);

            newPoll.Poll.UserIdentity = currentUserId;

            newPoll.Poll.PostedBy = currentUserInfo.Name;

            newPoll.Poll.PostDate = DateTime.Today;

            _context.Polls.Add(newPoll.Poll);

            _context.SaveChanges();



            newPoll.OptionOne.PostId    = _context.Polls.Max(lastPoll => lastPoll.Id);
            newPoll.OptionOne.VoteCount = 1;
            newPoll.OptionOne.Status    = true;
            newPoll.OptionOne.CheckedBy.Add(currentUserInfo.Name);

            _context.Options.Add(newPoll.OptionOne);

            _context.SaveChanges();

            return(RedirectToAction("Details", "Poll", newPoll.Poll));
        }
        public async Task <IActionResult> Post([FromBody] NewPollViewModel model)
        {
            var userId = this.userManager.GetUserId(User);

            if (ModelState.IsValid)
            {
                Poll newPoll = new Poll
                {
                    Question        = model.Question,
                    Active          = true,
                    ActiveUntil     = model.ActiveUntil,
                    MultipleAnswers = model.MultipleAnswers,
                    UserId          = userId
                };
                this.unitOfWork.PollsRepository.Add(newPoll);
                foreach (var answer in model.Answers)
                {
                    PollAnswer newAnswer = new PollAnswer
                    {
                        Name   = answer,
                        PollId = newPoll.PollId,
                    };
                    this.unitOfWork.PollAnswersRepository.Add(newAnswer);
                }
                await this.unitOfWork.Save();

                return(CreatedAtAction("GetPoll", new { id = newPoll.PollId }));
            }
            else
            {
                return(BadRequest());
            }
        }
示例#3
0
        public IActionResult AddNewPoll([FromBody] NewPollViewModel newPoll)
        {
            string userId = Request.Headers[Constants.UserToken];
            User   user;

            _memoryCache.TryGetValue(userId, out user);
            if (user == null)
            {
                return(Unauthorized(Messages.UserNotFoundError));
            }

            Poll p = _mapper.Map <Poll>(newPoll);

            p.CreatedDate = DateTime.UtcNow;
            p.CreatedBy   = user.Userid;

            _dBContext.Poll.Add(p);
            _dBContext.SaveChanges();

            List <PollOptions> lstPollOptions = new List <PollOptions>();

            int order = 0;

            foreach (var item in newPoll.options)
            {
                PollOptions options = new PollOptions();
                options.PollId      = p.PollId;
                options.OptionText  = item;
                options.StatusId    = p.StatusId;
                options.CreatedBy   = user.Userid;
                options.CreatedDate = DateTime.UtcNow;
                options.OrderId     = order;
                lstPollOptions.Add(options);
                order++;
            }

            _dBContext.PollOptions.AddRange(lstPollOptions);
            _dBContext.SaveChanges();

            PollViewModel pCache = _mapper.Map <PollViewModel>(p);

            pCache.PollOptions = lstPollOptions;

            _memoryCache.Set($"dbpoll_id_{p.PollId}", p);
            _memoryCache.Set($"poll_guid_{p.PollGuid}", pCache);
            _memoryCache.Set($"poll_id_{p.PollId}", pCache);
            _memoryCache.Remove($"poll_userpoll_userid_{user.UserGuid}");

            DashboardMetricsViewModel dashboardMetricsViewModel;

            _memoryCache.TryGetValue($"dashboard_{user.UserGuid}", out dashboardMetricsViewModel);
            if (dashboardMetricsViewModel != null)
            {
                dashboardMetricsViewModel.polls += 1;
                _memoryCache.Set($"dashboard_{user.UserGuid}", dashboardMetricsViewModel);
            }

            return(GetPoll(p.PollId));
        }
        // GET: Poll / NewPoll

        public ActionResult NewPoll()
        {
            var newPoll = new NewPollViewModel
            {
                Poll      = new Poll(),
                OptionOne = new Option()
            };

            return(View(newPoll));
        }
示例#5
0
        public IActionResult AddNewPoll([FromBody] NewPollViewModel newPoll)
        {
            string userId        = Request.Headers[Constants.UserToken];
            string decyrptstring = Security.Decrypt(userId);

            if (decyrptstring == null)
            {
                return(BadRequest());
            }

            User user = _dBContext.User.Where(x => x.UserGuid == decyrptstring).FirstOrDefault();

            if (user == null)
            {
                return(BadRequest(Messages.UserNotFoundError));
            }

            Poll p = _mapper.Map <Poll>(newPoll);

            p.CreatedDate = DateTime.UtcNow;
            p.CreatedBy   = user.Userid;

            _dBContext.Poll.Add(p);
            _dBContext.SaveChanges();

            List <PollOptions> lstPollOptions = new List <PollOptions>();

            int order = 0;

            foreach (var item in newPoll.options)
            {
                PollOptions options = new PollOptions();
                options.PollId      = p.PollId;
                options.OptionText  = item;
                options.StatusId    = p.StatusId;
                options.CreatedBy   = user.Userid;
                options.CreatedDate = DateTime.UtcNow;
                options.OrderId     = order;
                lstPollOptions.Add(options);
                order++;
            }

            _dBContext.PollOptions.AddRange(lstPollOptions);
            _dBContext.SaveChanges();
            return(GetPoll(p.PollId));
        }
示例#6
0
        public async Task <IActionResult> NewPoll(ulong guildId, [FromServices] IDiscordGuildService guildService)
        {
            // All channels for guild.
            var channels = await guildService.GetChannels(guildId);

            // Text channels for guild.
            var textChannels = channels.Where(x => x.Type == (int)ChannelType.Text).ToList();

            var viewModel = new NewPollViewModel
            {
                Channels     = new SelectList(textChannels, "Id", "Name"),
                TotalOptions = ModuleConstants.TotalOptions,
                GuildId      = guildId.ToString()
            };

            return(View(viewModel));
        }
示例#7
0
        public async Task <IActionResult> NewPoll([FromServices] IDiscordGuildService guildService, [FromServices] IMessageService messageService,
                                                  ulong guildId, NewPollViewModel viewModel, List <string> options)
        {
            // All channels for guild.
            var channels = await guildService.GetChannels(guildId);

            // Text channels for guild.
            var textChannels = channels.Where(x => x.Type == (int)ChannelType.Text).ToList();

            // Authorization - make sure channel belongs to the guild (prevents exploitation by providing a channel that isn't theirs, if they're authorized).
            // Note - this should be extracted into a seperate web filter.
            if (textChannels.FirstOrDefault(x => x.Id == viewModel.ChannelId) == null)
            {
                ModelState.AddModelError("All", "You are not authorized to perform this action.");

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");

                return(View(viewModel));
            }

            var validOptions = options.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

            // Make sure user supplied valid no of options
            if (validOptions.Count < 2 || validOptions.Count > ModuleConstants.TotalOptions)
            {
                ModelState.AddModelError("All", $"Please provide between 2 and {ModuleConstants.TotalOptions} valid options.");

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");

                return(View(viewModel));
            }

            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var poll = new StringBuilder();

            // Add poll title.
            poll.Append(":small_blue_diamond: " + viewModel.Title + Environment.NewLine);

            var discordNumbers = MessageService.DiscordNumberEmotes;

            // Add options to poll.
            for (var i = 0; i < validOptions.Count; i++)
            {
                poll.Append($"{discordNumbers[i + 1]} {validOptions[i]}{Environment.NewLine}");
            }

            // Post message and get message details.
            var message = await messageService.Post(viewModel.ChannelId, poll.ToString());

            // Create poll settings entry for guild in db, if doesn't exist.
            if (await _entityServicePollSettings.Find(guildId) == null)
            {
                await _entityServicePollSettings.Create(new PollSettings()
                {
                    GuildId = guildId
                });
            }

            // Save poll to database
            await _entityServicePoll.Create(new Poll()
            {
                ChannelId = viewModel.ChannelId,
                MessageId = message.Id,
                PollTitle = viewModel.Title,
                GuildId   = guildId
            });

            // Add reactions to message to act as voting buttons.
            for (var i = 1; i < validOptions.Count + 1; i++)
            {
                await messageService.AddReaction(message, new Emoji(discordNumbers[i]));
            }

            return(RedirectToAction("Index"));
        }