Exemple #1
0
 public void MoveQuestionToCatalog_ErrorPathThrows(long catalogId, string expectedErrorMessage)
 {
     using (var context = CreateTestCreationDbContext())
     {
         var      uow       = TestUtils.CreateTestCreationUoW(context);
         Question question  = uow.Questions.GetByIdWithAnswers(ValidQuestion1Id);
         var      exception = Assert.ThrowsException <DomainException>(() => QuestionMover.MoveQuestionToCatalog(question, catalogId, uow.QuestionsCatalogs, null));
         Assert.AreEqual(expectedErrorMessage, exception.Message);
     }
 }
Exemple #2
0
        public void MoveQuestionToCatalog_HappyPathIsSuccessful(long questionId, long catalogId)
        {
            const int maxNumberOfQuestionsInCatalog = 3;
            Mock <IAddQuestionPolicy> policyMock    = new Mock <IAddQuestionPolicy>();

            policyMock.Setup(x => x.CanAddQuestion(It.IsAny <int>())).Returns <int>(x => x < maxNumberOfQuestionsInCatalog);

            using (var context = CreateTestCreationDbContext())
            {
                var      uow      = TestUtils.CreateTestCreationUoW(context);
                Question question = uow.Questions.GetByIdWithAnswers(questionId);

                QuestionMover.MoveQuestionToCatalog(question, catalogId, uow.QuestionsCatalogs, policyMock.Object);
                uow.Save();
            }

            using (var context = CreateTestCreationDbContext())
            {
                Question question = context.Questions.Find(questionId);
                Assert.AreEqual(catalogId, question.CatalogId);
            }
        }
        public async Task <Result> Handle(UpdateQuestionWithAnswersCommand command, CancellationToken cancellationToken)
        {
            Question question = uow.Questions.GetByIdWithAnswers(command.QuestionId);

            if (question == null)
            {
                return(Result.NotFound());
            }
            if (question.OwnerId != command.UserId) // todo : check catalog instead
            {
                return(Result.Unauthorized());
            }
            if (command.ConcurrencyToken.HasValue)
            {
                if (question.ConcurrencyToken != command.ConcurrencyToken.Value)
                {
                    return(Result.Conflict());
                }
            }

            question.Content = command.Content;

            if (question.CatalogId != command.CatalogId)
            {
                var owner  = uow.Owners.GetById(question.OwnerId);
                var policy = AddQuestionPolicyFactory.Create(owner.MembershipLevel);
                QuestionMover.MoveQuestionToCatalog(question, command.CatalogId, uow.QuestionsCatalogs, policy);
            }

            question.Answers.MergeWith(command.Answers, x => x.AnswerId, y => y.AnswerId,
                                       onAdd: x => question.AddAnswer(x.Content, x.IsCorrect),
                                       onUpdate: (x, y) => { x.Content = y.Content; x.IsCorrect = y.IsCorrect; },
                                       onDelete: y => question.DeleteAnswer(y));

            await uow.Save();

            return(Result.Ok());
        }