Esempio n. 1
0
        public void Can_clone()
        {	    
            //create
            var model1 = new PollModel
            {
                Id = 1,
                Name = "Name 1",
                AlreadyVoted = true,
                TotalVotes = 2,
            };
            model1.Answers.Add(new PollAnswerModel
                               {
                                   Id = 3, 
                                   Name = "answer 1", 
                                   NumberOfVotes = 4, 
                                   PercentOfTotalVotes = 5
                               });

            //clone
            var model2 = (PollModel)model1.Clone();
            model2.Id.ShouldEqual(1);
            model2.Name.ShouldEqual("Name 1");
            model2.AlreadyVoted.ShouldEqual(true);
            model2.TotalVotes.ShouldEqual(2);
            model2.Answers.ShouldNotBeNull();
            model2.Answers.Count.ShouldEqual(1);
            model2.Answers[0].Id.ShouldEqual(3);
            model2.Answers[0].Name.ShouldEqual("answer 1");
            model2.Answers[0].NumberOfVotes.ShouldEqual(4);
            model2.Answers[0].PercentOfTotalVotes.ShouldEqual(5);
        }
        protected PollModel PreparePollModel(Poll poll, bool setAlreadyVotedProperty)
        {
            var model = new PollModel()
            {
                Id = poll.Id,
                AlreadyVoted = setAlreadyVotedProperty && _pollService.AlreadyVoted(poll.Id, _workContext.CurrentCustomer.Id),
                Name = poll.Name
            };
            var answers = poll.PollAnswers.OrderBy(x => x.DisplayOrder);
            foreach (var answer in answers)
                model.TotalVotes += answer.NumberOfVotes;
            foreach (var pa in answers)
            {
                model.Answers.Add(new PollAnswerModel()
                {
                    Id = pa.Id,
                    Name = pa.Name,
                    NumberOfVotes = pa.NumberOfVotes,
                    PercentOfTotalVotes = model.TotalVotes > 0 ? ((Convert.ToDouble(pa.NumberOfVotes) / Convert.ToDouble(model.TotalVotes)) * Convert.ToDouble(100)) : 0,
                });
            }

            return model;
        }