Esempio n. 1
0
        public async Task <Result <long> > Handle(CreateQuestionWithAnswersCommand command, CancellationToken cancellationToken)
        {
            var catalog = uow.QuestionsCatalogs.GetById(command.CatalogId);

            if (catalog == null)
            {
                return(Result.Error("Catalog not found"));
            }
            if (catalog.OwnerId != command.UserId)
            {
                return(Result.Unauthorized());
            }

            var question = Question.Create(command.Content, command.UserId);

            if (command.Answers != null)
            {
                foreach (var answer in command.Answers)
                {
                    question.AddAnswer(answer.Content, answer.IsCorrect);
                }
            }

            var owner  = uow.Owners.GetById(catalog.OwnerId);
            var policy = AddQuestionPolicyFactory.Create(owner.MembershipLevel);

            catalog.AddQuestion(question, policy);
            await uow.Save();

            return(Result.Ok(question.QuestionId));
        }
Esempio n. 2
0
        private void SeedDataForNewOwner(Owner owner)
        {
            var addQuestionsCatalogPolicy = AddQuestionsCatalogPolicyFactory.Create(owner.MembershipLevel);
            var addQuestionPolicy         = AddQuestionPolicyFactory.Create(owner.MembershipLevel);

#pragma warning disable IDE0059 // Unnecessary assignment of a value
            var footballCategory = owner.AddQuestionsCatalog("Football", addQuestionsCatalogPolicy);
#pragma warning restore IDE0059 // Unnecessary assignment of a value
            var generalKnowledgeCategory = owner.AddQuestionsCatalog("Basic General Knowledge", addQuestionsCatalogPolicy);
            var famousPeople             = owner.AddQuestionsCatalog("Famous people", addQuestionsCatalogPolicy);

            var q1 = Question.Create("Entomology is the science that studies", owner.OwnerId);
            q1.AddAnswer("Behavior of human beings", false);
            q1.AddAnswer("Insects", true);
            q1.AddAnswer("The origin and history of technical and scientific terms", false);
            q1.AddAnswer("The formation of rocks", false);
            generalKnowledgeCategory.AddQuestion(q1, addQuestionPolicy);

            var q2 = Question.Create("For which of the following disciplines is Nobel Prize awarded?", owner.OwnerId);
            q2.AddAnswer("Physics and Chemistry", false);
            q2.AddAnswer("Physiology or Medicine", false);
            q2.AddAnswer("Literature, Peace and Economics", false);
            q2.AddAnswer("All of the above", true);
            generalKnowledgeCategory.AddQuestion(q2, addQuestionPolicy);

            var q3 = Question.Create("Grand Central Terminal, Park Avenue, New York is the world's", owner.OwnerId);
            q3.AddAnswer("largest railway station", true);
            q3.AddAnswer("highest railway station", false);
            q3.AddAnswer("longest railway station", false);
            q3.AddAnswer("none of the above", false);
            generalKnowledgeCategory.AddQuestion(q3, addQuestionPolicy);

            var q4 = Question.Create("Which insect inspired the term \"computer bug\"?", owner.OwnerId);
            q4.AddAnswer("Moth1", false);
            q4.AddAnswer("Cockroach3", true);
            q4.AddAnswer("Fly2", false);
            q4.AddAnswer("Beetle", false);
            generalKnowledgeCategory.AddQuestion(q4, addQuestionPolicy);

            var q5 = Question.Create("Who is the patron saint of Spain?", owner.OwnerId);
            q5.AddAnswer("Saint James", false);
            q5.AddAnswer("Saint Peter", true);
            q5.AddAnswer("Saint John", false);
            q5.AddAnswer("Saint Percy", false);
            generalKnowledgeCategory.AddQuestion(q5, addQuestionPolicy);

            var q6 = Question.Create("Frederick Sanger is a twice recipient of the Nobel Prize for", owner.OwnerId);
            q6.AddAnswer("Chemistry in 1958 and 1980", true);
            q6.AddAnswer("Physics in 1956 and 1972", false);
            q6.AddAnswer("Chemistry in 1954 and Peace in 1962", false);
            q6.AddAnswer("Physics in 1903 and Chemistry in 1911", false);
            famousPeople.AddQuestion(q6, addQuestionPolicy);

            var q7 = Question.Create("Galileo was an Italian astronomer who", owner.OwnerId);
            q7.AddAnswer("developed the telescope", false);
            q7.AddAnswer("discovered four satellites of Jupiter", false);
            q7.AddAnswer("discovered that the movement of pendulum produces a regular time measurement", false);
            q7.AddAnswer("all of the above", true);
            famousPeople.AddQuestion(q7, addQuestionPolicy);
        }
        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());
        }
Esempio n. 4
0
        public static void Seed(TestCreationDbContext context)
        {
            var owner1 = Owner.Create(OwnerId, MembershipLevel.Regular);
            var owner2 = Owner.Create(OtherOwnerId, MembershipLevel.Regular);
            var owner7 = Owner.Create(7, MembershipLevel.Regular);

            context.Owners.Add(owner1);
            context.Owners.Add(owner2);
            context.Owners.Add(owner7);

            context.SaveChanges();

            var addQuestionsCatalogPolicyForOwner1 = AddQuestionsCatalogPolicyFactory.Create(owner1.MembershipLevel);
            var addQuestionsCatalogPolicyForOwner2 = AddQuestionsCatalogPolicyFactory.Create(owner2.MembershipLevel);

            QuestionsCatalog catalogA = owner1.AddQuestionsCatalog("Owner 1, QuestionsCatalog A", addQuestionsCatalogPolicyForOwner1);

            context.SaveChanges();
            QuestionsCatalog catalogB = owner1.AddQuestionsCatalog("Owner 1, QuestionsCatalog B", addQuestionsCatalogPolicyForOwner1);

            context.SaveChanges();
            QuestionsCatalog catalogC = owner1.AddQuestionsCatalog("Owner 1, QuestionsCatalog C", addQuestionsCatalogPolicyForOwner1);

            context.SaveChanges();
            QuestionsCatalog catalogD = owner1.AddQuestionsCatalog("Owner 1, QuestionsCatalog D, deleted", addQuestionsCatalogPolicyForOwner1);

            catalogD.Delete();
            context.SaveChanges();
            TestsCatalog catalogE = owner1.AddTestsCatalog("Owner 1, TestsCatalog E");

            context.SaveChanges();
            TestsCatalog catalogF = owner1.AddTestsCatalog("Owner 1, TestsCatalog F");

            context.SaveChanges();
            TestsCatalog catalogG = owner1.AddTestsCatalog("Owner 1, TestsCatalog G, deleted");

            catalogG.Delete();
            context.SaveChanges();
            QuestionsCatalog catalogH = owner2.AddQuestionsCatalog("Owner 2, QuestionsCatalog H", addQuestionsCatalogPolicyForOwner2);

            context.SaveChanges();
            TestsCatalog catalogI = owner2.AddTestsCatalog("Owner 2, TestsCatalog I");

            context.SaveChanges();

            var AddQuestionPolicyForOwner1 = AddQuestionPolicyFactory.Create(owner1.MembershipLevel);

            var q1 = Question.Create("Owner 1, Catalog A, Question 1", owner1.OwnerId);

            q1.AddAnswer("Q1 A1", false);
            catalogA.AddQuestion(q1, AddQuestionPolicyForOwner1);

            context.SaveChanges();

            var q2 = Question.Create("Owner 1, Catalog A, Question 2", owner1.OwnerId);

            q2.AddAnswer("Q2 A1", false);
            q2.AddAnswer("Q2 A2", true);
            catalogA.AddQuestion(q2, AddQuestionPolicyForOwner1);

            context.SaveChanges();

            var q3 = Question.Create("Owner 1, Catalog A, Question 3", owner1.OwnerId);

            q3.AddAnswer("Q3 A1", false);
            q3.AddAnswer("Q3 A2", true);
            q3.AddAnswer("Q3 A3", false);
            catalogA.AddQuestion(q3, AddQuestionPolicyForOwner1);

            context.SaveChanges();

            var q4 = Question.Create("Owner 1, Catalog B, Question 4", owner1.OwnerId);

            q4.AddAnswer("Q4 A1", false);
            q4.AddAnswer("Q4 A2", true);
            q4.AddAnswer("Q4 A3", false);
            q4.AddAnswer("Q4 A4", true);
            catalogB.AddQuestion(q4, AddQuestionPolicyForOwner1);
            context.SaveChanges();

            var q5 = Question.Create("Owner 1, Catalog B, Question 5, deleted", owner1.OwnerId);

            q5.AddAnswer("Q5 A1", false);
            q5.AddAnswer("Q5 A2", true);
            q5.AddAnswer("Q5 A3", false);
            q5.AddAnswer("Q5 A4", true);
            q5.AddAnswer("Q5 A5", false);
            q5.Delete();
            catalogB.AddQuestion(q5, AddQuestionPolicyForOwner1);
            context.SaveChanges();

            var q6 = Question.Create("Owner 1, Catalog C, Question 6", owner1.OwnerId);

            q6.AddAnswer("Q6 A1", false);
            q6.AddAnswer("Q6 A2", true);
            q6.AddAnswer("Q6 A3", false);
            q6.AddAnswer("Q6 A4", true);
            q6.AddAnswer("Q6 A5", false);
            q6.AddAnswer("Q6 A6", true);
            catalogC.AddQuestion(q6, AddQuestionPolicyForOwner1);
            context.SaveChanges();


            var q7 = Question.Create("Owner 1, Catalog D deleted, Question 7", owner1.OwnerId);

            q7.AddAnswer("Q7 A1", false);
            q7.AddAnswer("Q7 A2", true);
            catalogD.AddQuestion(q7, AddQuestionPolicyForOwner1);
            context.SaveChanges();

            var q8 = Question.Create("Owner 2, Catalog H, Question 8", owner2.OwnerId);

            q8.AddAnswer("Q8 A1", false);
            catalogH.AddQuestion(q8, AddQuestionPolicyForOwner1);
            context.SaveChanges();

            context.SaveChanges();

            var t1 = Test.Create(owner1.OwnerId, "Owner 1, catalog E, Test 1");

            catalogE.AddTest(t1);


            t1.AddQuestion(q1);
            t1.AddQuestion(q3);
            t1.AddQuestion(q5);
            context.SaveChanges();

            var t2 = Test.Create(owner1.OwnerId, "Owner 1, catalog F, Test 2");

            catalogF.AddTest(t2);
            t2.AddQuestion(q2);
            t2.AddQuestion(q4);
            t2.AddQuestion(q6);
            context.SaveChanges();

            var t3 = Test.Create(owner2.OwnerId, "Owner 2, catalog I, Test 3");

            catalogI.AddTest(t3);
            t3.AddQuestion(q7);
            t3.AddQuestion(q8);
            context.SaveChanges();


            var t4 = Test.Create(owner1.OwnerId, "Owner 1, catalog F, Test 4");

            catalogF.AddTest(t2);
            t4.Delete();

            context.SaveChanges();
        }