Ejemplo n.º 1
0
        //[ValidateInput(false)]
        public virtual IActionResult Vote(string pollAnswerId, string pollId)
        {
            var poll = _pollService.GetPollById(pollId); //pollAnswer.Poll;

            if (!poll.Published)
            {
                return(Json(new
                {
                    error = "Poll is not available",
                }));
            }

            var pollAnswer = poll.PollAnswers.FirstOrDefault(x => x.Id == pollAnswerId);

            if (pollAnswer == null)
            {
                return(Json(new
                {
                    error = "No poll answer found with the specified id",
                }));
            }


            if (_workContext.CurrentCustomer.IsGuest() && !poll.AllowGuestsToVote)
            {
                return(Json(new
                {
                    error = _localizationService.GetResource("Polls.OnlyRegisteredUsersVote"),
                }));
            }

            bool alreadyVoted = _pollService.AlreadyVoted(poll.Id, _workContext.CurrentCustomer.Id);

            if (!alreadyVoted)
            {
                //vote
                pollAnswer.PollVotingRecords.Add(new PollVotingRecord
                {
                    PollId       = pollId,
                    PollAnswerId = pollAnswer.Id,
                    CustomerId   = _workContext.CurrentCustomer.Id,
                    CreatedOnUtc = DateTime.UtcNow
                });
                //update totals
                pollAnswer.NumberOfVotes = pollAnswer.PollVotingRecords.Count;
                _pollService.UpdatePoll(poll);

                if (!_workContext.CurrentCustomer.IsHasPoolVoting)
                {
                    _workContext.CurrentCustomer.IsHasPoolVoting = true;
                    EngineContextExperimental.Current.Resolve <ICustomerService>().UpdateHasPoolVoting(_workContext.CurrentCustomer.Id);
                }
            }

            return(Json(new
            {
                html = this.RenderPartialViewToString("_Poll", _pollWebService.PreparePollModel(poll, true)),
            }));
        }
Ejemplo n.º 2
0
        public virtual /*Task<*/ IViewComponentResult PollBlock(string systemKeyword)
        {
            if (String.IsNullOrWhiteSpace(systemKeyword))
            {
                return(Content(""));
            }

            var cacheKey = string.Format(ModelCacheEventConsumer.POLL_BY_SYSTEMNAME__MODEL_KEY, systemKeyword, _storeContext.CurrentStore.Id);



            //just workaround i need
            Poll poll        = _pollService.GetPollBySystemKeyword(systemKeyword, _storeContext.CurrentStore.Id);
            var  cachedModel = _pollWebService.PreparePollModel(poll, false);



            //var cachedModel = _cacheManager.Get(cacheKey, () =>
            //{
            //    Poll poll = _pollService.GetPollBySystemKeyword(systemKeyword, _storeContext.CurrentStore.Id);
            //    //ACL (access control list)
            //    if (!_aclService.Authorize(poll))
            //        return new PollModel { Id = "" };
            //    if (poll == null ||
            //        !poll.Published ||
            //        (poll.StartDateUtc.HasValue && poll.StartDateUtc.Value > DateTime.UtcNow) ||
            //        (poll.EndDateUtc.HasValue && poll.EndDateUtc.Value < DateTime.UtcNow))
            //        //we do not cache nulls. that's why let's return an empty record (ID = 0)
            //        return new PollModel { Id = "" };
            //    return _pollWebService.PreparePollModel(poll, false);
            //});

            if (cachedModel == null || cachedModel.Id == "")
            {
                return(Content(""));
            }

            //"AlreadyVoted" property of "PollModel" object depends on the current customer. Let's update it.
            //But first we need to clone the cached model (the updated one should not be cached)
            var model = (PollModel)cachedModel.Clone();

            model.AlreadyVoted = _pollService.AlreadyVoted(model.Id, _workContext.CurrentCustomer.Id);

            return(View(nameof(this.PollBlock), model));
        }