public void Create()
		{
			var now = DateTime.UtcNow;
			var generator = new RandomObjectGenerator();
			var userId = generator.Generate<int>();

			var poll = EntityCreator.Create<MVPoll>(_ =>
			{
				_.PollDeletedFlag = false;
				_.PollStartDate = now.AddDays(-2);
				_.PollEndDate = now.AddDays(2);
				_.PollMinAnswers = 2;
				_.PollMaxAnswers = 3;
			});

			poll.MVPollOptions = new List<MVPollOption> 
			{ 
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>()
			};

			var category = EntityCreator.Create<MVCategory>(_ => _.CategoryID = poll.PollCategoryID);

			var entities = new Mock<IEntities>(MockBehavior.Strict);
			entities.Setup(_ => _.MVPolls)
				.Returns(new InMemoryDbSet<MVPoll> { poll });
			entities.Setup(_ => _.MVPollSubmissions)
				.Returns(new InMemoryDbSet<MVPollSubmission>());
			entities.Setup(_ => _.MVCategories)
				.Returns(new InMemoryDbSet<MVCategory> { category });
			entities.Setup(_ => _.Dispose());

			var builder = new ContainerBuilder();
			builder.Register<IEntities>(_ => entities.Object);

			using (new ObjectActivator(builder.Build()).Bind(() => ApplicationContext.DataPortalActivator))
			{
				var criteria = new PollSubmissionCriteria(poll.PollID, userId);
				var submission = DataPortal.Create<PollSubmission>(criteria);

				Assert.AreEqual(poll.PollID, submission.PollID, submission.GetPropertyName(_ => _.PollID));
				Assert.AreEqual(poll.PollImageLink, submission.PollImageLink, submission.GetPropertyName(_ => _.PollImageLink));
				Assert.AreEqual(userId, submission.UserID, submission.GetPropertyName(_ => _.UserID));
				Assert.AreEqual(poll.PollQuestion, submission.PollQuestion, submission.GetPropertyName(_ => _.PollQuestion));
				Assert.AreEqual(category.CategoryName, submission.CategoryName, submission.GetPropertyName(_ => _.CategoryName));
				Assert.AreEqual(poll.PollDescription, submission.PollDescription, submission.GetPropertyName(_ => _.PollDescription));
				Assert.AreEqual(poll.PollMaxAnswers.Value, submission.PollMaxAnswers, submission.GetPropertyName(_ => _.PollMaxAnswers));
				Assert.AreEqual(poll.PollMinAnswers.Value, submission.PollMinAnswers, submission.GetPropertyName(_ => _.PollMinAnswers));
				Assert.AreEqual(4, submission.Responses.Count, submission.GetPropertyName(_ => _.Responses));

				submission.BrokenRulesCollection.AssertRuleCount(1);
				submission.BrokenRulesCollection.AssertRuleCount(PollSubmission.ResponsesProperty, 1);
				submission.BrokenRulesCollection.AssertBusinessRuleExists<MinimumAndMaximumPollSubmissionResponsesRule>(
					PollSubmission.ResponsesProperty, true);
			}

			entities.VerifyAll();
		}
Esempio n. 2
0
		public PollInfo Get(int pollID, int userID)
		{
			try
			{
				var authUserID = MyVoteAuthentication.GetCurrentUserID().Value;
				var criteria = new PollSubmissionCriteria(pollID, authUserID);
				var submission = this.PollSubmissionFactory.Fetch(criteria);
				var result = new PollInfo
				{
					PollID = submission.PollID,
					PollDescription = submission.PollDescription,
					MaxAnswers = submission.PollMaxAnswers,
					MinAnswers = submission.PollMinAnswers,
					Comment = submission.Comment,
					PollQuestion = submission.PollQuestion,
					PollSubmissionID = submission.PollSubmissionID,
					SubmissionDate = submission.SubmissionDate,
					UserID = submission.UserID,
					PollOptions = submission.Responses.Select(_ => new Models.PollResponseOption
					{
						IsOptionSelected = _.IsOptionSelected,
						OptionPosition = _.OptionPosition,
						OptionText = _.OptionText,
						PollOptionID = _.PollOptionID,
						PollResponseID = _.PollResponseID
					}).ToList()
				};
				return result;
			}
			catch (Exception ex)
			{
				throw ex.ToHttpResponseException(Request);
			}
		}
Esempio n. 3
0
 public PollInfo Get(int pollID, int userID)
 {
     try
     {
         var authUserID = MyVoteAuthentication.GetCurrentUserID().Value;
         var criteria   = new PollSubmissionCriteria(pollID, authUserID);
         var submission = this.PollSubmissionFactory.Fetch(criteria);
         var result     = new PollInfo
         {
             PollID           = submission.PollID,
             PollDescription  = submission.PollDescription,
             MaxAnswers       = submission.PollMaxAnswers,
             MinAnswers       = submission.PollMinAnswers,
             Comment          = submission.Comment,
             PollQuestion     = submission.PollQuestion,
             PollSubmissionID = submission.PollSubmissionID,
             SubmissionDate   = submission.SubmissionDate,
             UserID           = submission.UserID,
             PollOptions      = submission.Responses.Select(_ => new Models.PollResponseOption
             {
                 IsOptionSelected = _.IsOptionSelected,
                 OptionPosition   = _.OptionPosition,
                 OptionText       = _.OptionText,
                 PollOptionID     = _.PollOptionID,
                 PollResponseID   = _.PollResponseID
             }).ToList()
         };
         return(result);
     }
     catch (Exception ex)
     {
         throw ex.ToHttpResponseException(Request);
     }
 }
Esempio n. 4
0
        public void CreateWhenUserHasSubmittedAnswersBefore()
        {
            var generator = new RandomObjectGenerator();
            var userId    = generator.Generate <int>();
            var pollId    = generator.Generate <int>();

            var entity = EntityCreator.Create <MVPollSubmission>(_ =>
            {
                _.UserID = userId;
                _.PollID = pollId;
            });

            var entities = new Mock <IEntities>();

            entities.Setup(_ => _.MVPollSubmissions)
            .Returns(new InMemoryDbSet <MVPollSubmission> {
                entity
            });
            entities.Setup(_ => _.Dispose());

            var pollFactory = new Mock <IObjectFactory <IPoll> >();

            pollFactory.Setup(_ => _.Fetch(pollId)).Returns <int>(_ => DataPortal.Fetch <Poll>(_));

            var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >();

            pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>));

            var builder = new ContainerBuilder();

            builder.Register <IEntities>(_ => entities.Object);
            builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object);
            builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object);

            using (new ObjectActivator(builder.Build(), Mock.Of <ICallContext>())
                   .Bind(() => ApplicationContext.DataPortalActivator))
            {
                var criteria = new PollSubmissionCriteria(pollId, userId);
                new DataPortal <PollSubmission>().Create(criteria);
            }
        }
Esempio n. 5
0
		public void Put([FromBody]Models.PollResponse value)
		{
			IPollSubmission submission = null;

			try
			{
			    var authUserID = MyVoteAuthentication.GetCurrentUserID().Value;
				var criteria = new PollSubmissionCriteria(value.PollID, authUserID);
				submission = this.PollSubmissionFactory.Create(criteria);

				foreach (var item in value.ResponseItems)
				{
					var response = submission.Responses.Where(_ => _.PollOptionID == item.PollOptionID).Single();
					response.IsOptionSelected = item.IsOptionSelected;
				}

				submission.Save();
			}
			catch (ValidationException ex)
			{
				string brokenRules = string.Empty;
				if (submission != null)
				{
					brokenRules = submission.GetBrokenRules().ToString();
				}

				throw new HttpResponseException(
				  new HttpResponseMessage
				  {
					  StatusCode = HttpStatusCode.BadRequest,
					  ReasonPhrase = ex.Message.Replace(Environment.NewLine, " "),
					  Content = new StringContent(brokenRules),
					  RequestMessage = Request
				  });
			}
			catch (Exception ex)
			{
				throw ex.ToHttpResponseException(Request);
			}
		}
Esempio n. 6
0
        public void Put([FromBody] Models.PollResponse value)
        {
            IPollSubmission submission = null;

            try
            {
                var authUserID = MyVoteAuthentication.GetCurrentUserID().Value;
                var criteria   = new PollSubmissionCriteria(value.PollID, authUserID);
                submission = this.PollSubmissionFactory.Create(criteria);

                foreach (var item in value.ResponseItems)
                {
                    var response = submission.Responses.Where(_ => _.PollOptionID == item.PollOptionID).Single();
                    response.IsOptionSelected = item.IsOptionSelected;
                }

                submission.Save();
            }
            catch (ValidationException ex)
            {
                string brokenRules = string.Empty;
                if (submission != null)
                {
                    brokenRules = submission.GetBrokenRules().ToString();
                }

                throw new HttpResponseException(
                          new HttpResponseMessage
                {
                    StatusCode     = HttpStatusCode.BadRequest,
                    ReasonPhrase   = ex.Message.Replace(Environment.NewLine, " "),
                    Content        = new StringContent(brokenRules),
                    RequestMessage = Request
                });
            }
            catch (Exception ex)
            {
                throw ex.ToHttpResponseException(Request);
            }
        }
Esempio n. 7
0
        public void Insert()
        {
            var now       = DateTime.UtcNow;
            var generator = new RandomObjectGenerator();
            var userId    = generator.Generate <int>();

            var poll = EntityCreator.Create <MVPoll>(_ =>
            {
                _.PollDeletedFlag = false;
                _.PollStartDate   = now.AddDays(-2);
                _.PollEndDate     = now.AddDays(2);
                _.PollMinAnswers  = 2;
                _.PollMaxAnswers  = 3;
            });

            poll.MVPollOptions = new List <MVPollOption>
            {
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>()
            };

            var category = EntityCreator.Create <MVCategory>(_ => _.CategoryID = poll.PollCategoryID);

            var polls = new InMemoryDbSet <MVPoll> {
                poll
            };
            var submissions = new InMemoryDbSet <MVPollSubmission>();
            var responses   = new InMemoryDbSet <MVPollResponse>();

            var saveChangesCount = 0;
            var submissionId     = generator.Generate <int>();

            var entities = new Mock <IEntities>(MockBehavior.Strict);

            entities.Setup(_ => _.MVPolls).Returns(polls);
            entities.Setup(_ => _.MVPollSubmissions).Returns(submissions);
            entities.Setup(_ => _.MVPollResponses).Returns(responses);
            entities.Setup(_ => _.MVCategories)
            .Returns(new InMemoryDbSet <MVCategory> {
                category
            });
            entities.Setup(_ => _.SaveChanges()).Callback(() =>
            {
                if (saveChangesCount == 0)
                {
                    submissions.Local[0].PollSubmissionID = submissionId;
                }

                saveChangesCount++;
            }).Returns(1);
            entities.Setup(_ => _.Dispose());

            var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >();

            pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>());

            var pollOption = new Mock <IObjectFactory <IPollOption> >();

            pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption));

            var pollFactory = new Mock <IObjectFactory <IPoll> >();

            pollFactory.Setup(_ => _.Fetch(poll.PollID)).Returns <int>(_ => DataPortal.Fetch <Poll>(_));

            var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >();

            pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>));

            var pollSubmissionResponseFactory = new Mock <IObjectFactory <IPollSubmissionResponse> >();

            pollSubmissionResponseFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponse>(_[0] as IPollOption));

            var builder = new ContainerBuilder();

            builder.Register <IEntities>(_ => entities.Object);
            builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object);
            builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object);
            builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object);
            builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object);
            builder.Register <IObjectFactory <IPollSubmissionResponse> >(_ => pollSubmissionResponseFactory.Object);

            using (new ObjectActivator(builder.Build(), new ActivatorCallContext())
                   .Bind(() => ApplicationContext.DataPortalActivator))
            {
                var criteria   = new PollSubmissionCriteria(poll.PollID, userId);
                var submission = new DataPortal <PollSubmission>().Create(criteria);

                submission.Responses[1].IsOptionSelected = true;
                submission.Responses[3].IsOptionSelected = true;

                submission = submission.Save();

                Assert.AreEqual(1, submissions.Local.Count, nameof(submissions.Local.Count));
                Assert.AreEqual(submissionId, submission.PollSubmissionID, nameof(submission.PollSubmissionID));
                Assert.AreEqual(4, responses.Local.Count, nameof(responses.Local.Count));
                Assert.IsFalse(responses.Local[0].OptionSelected, nameof(MVPollResponse.OptionSelected));
                Assert.IsTrue(responses.Local[1].OptionSelected, nameof(MVPollResponse.OptionSelected));
                Assert.IsFalse(responses.Local[2].OptionSelected, nameof(MVPollResponse.OptionSelected));
                Assert.IsTrue(responses.Local[3].OptionSelected, nameof(MVPollResponse.OptionSelected));
            }

            entities.VerifyAll();
        }
Esempio n. 8
0
        public void Create()
        {
            var now       = DateTime.UtcNow;
            var generator = new RandomObjectGenerator();
            var userId    = generator.Generate <int>();

            var pollEntity = EntityCreator.Create <MVPoll>(_ =>
            {
                _.PollDeletedFlag = false;
                _.PollStartDate   = now.AddDays(-2);
                _.PollEndDate     = now.AddDays(2);
                _.PollMinAnswers  = 2;
                _.PollMaxAnswers  = 3;
            });

            pollEntity.MVPollOptions = new List <MVPollOption>
            {
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>(),
                EntityCreator.Create <MVPollOption>()
            };

            var category = EntityCreator.Create <MVCategory>(_ => _.CategoryID = pollEntity.PollCategoryID);

            Poll poll = null;

            var pollFactory = new Mock <IObjectFactory <IPoll> >(MockBehavior.Strict);

            pollFactory.Setup(_ => _.Fetch(pollEntity.PollID))
            .Returns <int>(_ =>
            {
                poll = DataPortal.Fetch <Poll>(_);
                return(poll);
            });

            var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >(MockBehavior.Strict);

            pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>()))
            .Callback <object[]>(_ => Assert.AreSame(poll.PollOptions, _[0]))
            .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>));

            var entities = new Mock <IEntities>(MockBehavior.Strict);

            entities.Setup(_ => _.MVPolls)
            .Returns(new InMemoryDbSet <MVPoll> {
                pollEntity
            });
            entities.Setup(_ => _.MVPollSubmissions)
            .Returns(new InMemoryDbSet <MVPollSubmission>());
            entities.Setup(_ => _.MVCategories)
            .Returns(new InMemoryDbSet <MVCategory> {
                category
            });
            entities.Setup(_ => _.Dispose());

            var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >();

            pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>());

            var pollOption = new Mock <IObjectFactory <IPollOption> >();

            pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption));

            var pollSubmissionResponseFactory = new Mock <IObjectFactory <IPollSubmissionResponse> >();

            pollSubmissionResponseFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>()))
            .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponse>(_[0] as IPollOption));

            var builder = new ContainerBuilder();

            builder.Register <IEntities>(_ => entities.Object);
            builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object);
            builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object);
            builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object);
            builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object);
            builder.Register <IObjectFactory <IPollSubmissionResponse> >(_ => pollSubmissionResponseFactory.Object);

            using (new ObjectActivator(builder.Build(), new ActivatorCallContext())
                   .Bind(() => ApplicationContext.DataPortalActivator))
            {
                var criteria   = new PollSubmissionCriteria(pollEntity.PollID, userId);
                var submission = DataPortal.Create <PollSubmission>(criteria);

                Assert.AreEqual(pollEntity.PollID, submission.PollID, nameof(submission.PollID));
                Assert.AreEqual(pollEntity.PollImageLink, submission.PollImageLink, nameof(submission.PollImageLink));
                Assert.AreEqual(userId, submission.UserID, nameof(submission.UserID));
                Assert.AreEqual(pollEntity.PollQuestion, submission.PollQuestion, nameof(submission.PollQuestion));
                Assert.AreEqual(category.CategoryName, submission.CategoryName, nameof(submission.CategoryName));
                Assert.AreEqual(pollEntity.PollDescription, submission.PollDescription, nameof(submission.PollDescription));
                Assert.AreEqual(pollEntity.PollMaxAnswers.Value, submission.PollMaxAnswers, nameof(submission.PollMaxAnswers));
                Assert.AreEqual(pollEntity.PollMinAnswers.Value, submission.PollMinAnswers, nameof(submission.PollMinAnswers));
                Assert.AreEqual(4, submission.Responses.Count, nameof(submission.Responses));

                submission.BrokenRulesCollection.AssertRuleCount(1);
                submission.BrokenRulesCollection.AssertRuleCount(PollSubmission.ResponsesProperty, 1);
                submission.BrokenRulesCollection.AssertBusinessRuleExists <MinimumAndMaximumPollSubmissionResponsesRule>(
                    PollSubmission.ResponsesProperty, true);
            }

            entities.VerifyAll();
            pollFactory.VerifyAll();
            pollSubmissionResponsesFactory.VerifyAll();
        }
		public void CreateWhenUserHasSubmittedAnswersBefore()
		{
			var generator = new RandomObjectGenerator();
			var userId = generator.Generate<int>();
			var pollId = generator.Generate<int>();

			var entity = EntityCreator.Create<MVPollSubmission>(_ =>
			{
				_.UserID = userId;
				_.PollID = pollId;
			});

			var entities = new Mock<IEntities>(MockBehavior.Strict);
			entities.Setup(_ => _.MVPollSubmissions)
				.Returns(new InMemoryDbSet<MVPollSubmission> { entity });
			entities.Setup(_ => _.Dispose());

			var builder = new ContainerBuilder();
			builder.Register<IEntities>(_ => entities.Object);

			using (new ObjectActivator(builder.Build()).Bind(() => ApplicationContext.DataPortalActivator))
			{
				var criteria = new PollSubmissionCriteria(pollId, userId);
				new DataPortal<PollSubmission>().Create(criteria);
			}

			entities.VerifyAll();
		}
		public void Insert()
		{
			var now = DateTime.UtcNow;
			var generator = new RandomObjectGenerator();
			var userId = generator.Generate<int>();

			var poll = EntityCreator.Create<MVPoll>(_ =>
			{
				_.PollDeletedFlag = false;
				_.PollStartDate = now.AddDays(-2);
				_.PollEndDate = now.AddDays(2);
				_.PollMinAnswers = 2;
				_.PollMaxAnswers = 3;
			});

			poll.MVPollOptions = new List<MVPollOption> 
			{ 
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>(),
				EntityCreator.Create<MVPollOption>()
			};

			var category = EntityCreator.Create<MVCategory>(_ => _.CategoryID = poll.PollCategoryID);

			var polls = new InMemoryDbSet<MVPoll> { poll };
			var submissions = new InMemoryDbSet<MVPollSubmission>();
			var responses = new InMemoryDbSet<MVPollResponse>();

			var saveChangesCount = 0;
			var submissionId = generator.Generate<int>();

			var entities = new Mock<IEntities>(MockBehavior.Strict);
			entities.Setup(_ => _.MVPolls).Returns(polls);
			entities.Setup(_ => _.MVPollSubmissions).Returns(submissions);
			entities.Setup(_ => _.MVPollResponses).Returns(responses);
			entities.Setup(_ => _.MVCategories)
				.Returns(new InMemoryDbSet<MVCategory> { category });
			entities.Setup(_ => _.SaveChanges()).Callback(() =>
				{
					if (saveChangesCount == 0)
					{
						submissions.Local[0].PollSubmissionID = submissionId;
					}

					saveChangesCount++;
				}).Returns(1);
			entities.Setup(_ => _.Dispose());

			var builder = new ContainerBuilder();
			builder.Register<IEntities>(_ => entities.Object);

			using (new ObjectActivator(builder.Build()).Bind(() => ApplicationContext.DataPortalActivator))
			{
				var criteria = new PollSubmissionCriteria(poll.PollID, userId);
				var submission = new DataPortal<PollSubmission>().Create(criteria);

				submission.Responses[1].IsOptionSelected = true;
				submission.Responses[3].IsOptionSelected = true;

				submission = submission.Save();

				Assert.AreEqual(1, submissions.Local.Count, submissions.GetPropertyName(_ => _.Local.Count));
				Assert.AreEqual(submissionId, submission.PollSubmissionID, submission.GetPropertyName(_ => _.PollSubmissionID));
				Assert.AreEqual(4, responses.Local.Count, responses.GetPropertyName(_ => _.Local.Count));
				Assert.IsFalse(responses.Local[0].OptionSelected, responses.GetPropertyName(_ => _.Local[0].OptionSelected));
				Assert.IsTrue(responses.Local[1].OptionSelected, responses.GetPropertyName(_ => _.Local[1].OptionSelected));
				Assert.IsFalse(responses.Local[2].OptionSelected, responses.GetPropertyName(_ => _.Local[2].OptionSelected));
				Assert.IsTrue(responses.Local[3].OptionSelected, responses.GetPropertyName(_ => _.Local[3].OptionSelected));
			}

			entities.VerifyAll();
		}