public PollStats MountStats(List <BsonDocument> pollStatsBson)
        {
            var pollStats = new PollStats();

            foreach (var pollStat in pollStatsBson)
            {
                var views = pollStat.AsBsonDocument.Contains("views") ? (long)pollStat.GetValue("views") : 0;
                pollStats.Views = views;

                var options = pollStat.GetValue("options").AsBsonArray.ToList();

                foreach (var option in options)
                {
                    var pollVotesStructure = new PollVotes()
                    {
                        OptionId = option["option_id"].AsInt64,
                        Qty      = option.AsBsonDocument.Contains("votes") ? option["votes"]["qty"].AsInt64 : 0
                    };

                    pollStats.Votes.Add(pollVotesStructure);
                }
            }

            return(pollStats);
        }
Beispiel #2
0
        public IActionResult VotePoll([FromBody] PollVoteRequestViewModel newPoll)
        {
            Poll poll;;

            if (_memoryCache.TryGetValue("dbpoll_id_" + newPoll.pollId, out poll))
            {
                return(Ok(poll));
            }
            else
            {
                poll = _dBContext.Poll.Where(x => x.PollId == newPoll.pollId).FirstOrDefault();
            }

            if (poll == null)
            {
                return(BadRequest(Messages.PollNotFoundError));
            }
            if (poll.Enddate <= DateTime.Now.Date.AddDays(1).AddSeconds(-1))
            {
                return(BadRequest(Messages.PollEnded));
            }

            IPLocation userLocationDetails = LocationHelper.GetIpAndLocation(_httpContextAccessor);

            if (poll.Duplicate == 0)
            {
                var voted = _dBContext.PollVotes.Where(x => x.IpAddress == userLocationDetails.IP && x.UserLocation == userLocationDetails.Region).Any();
                if (voted)
                {
                    return(BadRequest(Messages.PollVoted));
                }
            }

            List <PollVotes> lstPollVotes = new List <PollVotes>();

            foreach (var item in newPoll.options)
            {
                PollVotes pollVote = new PollVotes();
                pollVote.PollId       = poll.PollId;
                pollVote.OptionId     = Int32.Parse(item);
                pollVote.IpAddress    = userLocationDetails.IP;
                pollVote.UserLocation = userLocationDetails.Region;
                pollVote.CreatedDate  = DateTime.UtcNow;
                lstPollVotes.Add(pollVote);
            }
            _dBContext.PollVotes.AddRange(lstPollVotes);
            _dBContext.SaveChanges();
            return(Ok(true));
        }
        public async Task <Poll> VoteAsync(long optionId)
        {
            IPollVotesRepository   pollVotesRepository   = new PollVotesRepository(_database);
            IPollOptionsRepository pollOptionsRepository = new PollOptionsRepository(_database);

            PollVotes pollVotesUpdate = new PollVotes();

            pollVotesUpdate.Qty = await pollVotesRepository.IsVotedAsync(optionId) ? await pollOptionsRepository.QuantityByOptionIdAsync(optionId) + 1 : 1;

            var filter = Builders <Poll> .Filter.Eq("options.option_id", optionId);

            var update = Builders <Poll> .Update.Set("options.$.votes", pollVotesUpdate);

            var documentBefore = await _database.GetCollection <Poll>("Poll").FindOneAndUpdateAsync(filter, update, new FindOneAndUpdateOptions <Poll> {
                ReturnDocument = ReturnDocument.Before
            });

            return(documentBefore);
        }
        public virtual bool DeleteOption(int optionID, string crc32Hash)
        {
            if (PollVotes.Count <= 1)
            {
                return(false);
            }
            string currentKey = PollVotes.ElementAt(optionID).Key;

            if (currentKey.HashCRC32() == crc32Hash)
            {
                PollVotes.Remove(currentKey);
                dBHandler.AddToQueue(this, true);
            }
            else
            {
                throw new FormatException("Hashes for poll vote do not match");
            }
            return(true);
        }
            public void Insert(Guid VoteId, Guid UserId, Guid PollAnswerId, DateTime CreationDate, Guid PollId)
            {
                PollVotes item = new PollVotes();

                item.VoteId = VoteId;

                item.UserId = UserId;

                item.PollAnswerId = PollAnswerId;

                item.CreationDate = CreationDate;

                item.PollId = PollId;

                item.Save(UserName);
            }
 public virtual string GetOptionText(int optionID)
 {
     return(PollVotes.ElementAt(optionID).Key.RemoveAppendingText());
 }