public async Task <Question> CreateNewQuestion(Question question, string testName, string option1, string option2, string option3, string option4)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                question.Test = context.Tests.Where(q => q.TestName == testName)
                                .FirstOrDefault();

                EntityEntry <Question> createdResult = await context.Set <Question>().AddAsync(question);

                await context.Set <QuestionOption>().AddAsync(new QuestionOption {
                    OptionText = option1, Question = question
                });

                await context.Set <QuestionOption>().AddAsync(new QuestionOption {
                    OptionText = option2, Question = question
                });

                await context.Set <QuestionOption>().AddAsync(new QuestionOption {
                    OptionText = option3, Question = question
                });

                await context.Set <QuestionOption>().AddAsync(new QuestionOption {
                    OptionText = option4, Question = question
                });

                await context.SaveChangesAsync();

                return(createdResult.Entity);
            }
        }
Ejemplo n.º 2
0
        public async Task ReadFunctionalityRetrievesQuestionOptionAndCorrespondingQuestionAndTest()
        {
            Test test;

            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                test = new Test()
                {
                    TestName = "Test1"
                };
                Question question = new Question {
                    Test = test, QuestionText = "TestQuestion1"
                };
                QuestionOption questionOption = new QuestionOption()
                {
                    OptionText = "TestOption1", Question = question
                };
                await context.Set <Test>().AddAsync(test);

                await context.Set <Question>().AddAsync(question);

                await context.Set <QuestionOption>().AddAsync(questionOption);

                await context.SaveChangesAsync();
            }

            Test retrievedTest = await testService.Get(test.Id);

            Question       retrievedQuestion       = retrievedTest.Questions.ToList()[0];
            QuestionOption retrievedQuestionOption = retrievedQuestion.QuestionOptions.ToList()[0];

            Assert.AreEqual("Test1", retrievedTest.TestName);
            Assert.AreEqual("TestQuestion1", retrievedQuestion.QuestionText);
            Assert.AreEqual("TestOption1", retrievedQuestionOption.OptionText);
        }
        public async Task <TestResult> CreatePersonalTestResult(int id, string testName, TestResult testResult)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                TestResult testResultCheck = context.TestResults.Where(t => t.Test.TestName == testName).FirstOrDefault(); //return already existing test result for that test
                if (testResultCheck == null)
                {
                    Account account = context.Accounts.Where(a => a.Id == id).FirstOrDefault();
                    Test    test    = context.Tests.Where(e => e.TestName == testName).FirstOrDefault();
                    testResult.Account = account;
                    testResult.Test    = test;
                    EntityEntry <TestResult> createdResult = await context.Set <TestResult>().AddAsync(testResult);

                    await context.SaveChangesAsync();

                    return(createdResult.Entity);
                }
                else
                {
                    if (testResultCheck.Mark < testResult.Mark)  //if user scores a higher mark
                    {
                        testResultCheck.Mark = testResult.Mark;
                        context.Set <TestResult>().Update(testResultCheck);
                        await context.SaveChangesAsync();
                    }
                    return(testResultCheck);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <T> AddAsync(T entity)
        {
            await context.Set <T>().AddAsync(entity);

            await context.SaveChangesAsync();

            return(entity);
        }
        public virtual async Task <bool> Delete(int id)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id);

                context.Set <T>().Remove(entity);
                await context.SaveChangesAsync();

                return(true);
            }
        }
Ejemplo n.º 6
0
 private void FuncToCall6(object context)
 {
     if (CreatedMainTopicTitle == null || CreatedSubTopicTitle == "")
     {
         MessageBox.Show("You must enter a valid name in order to delete a main topic.");
     }
     else
     {
         using (SelfAssessmentDbContext db = new SelfAssessmentDbContext())
         {
             List <SubTopic> subTopics = db.SubTopics
                                         .Include(s => s.MainTopic)
                                         .Where(m => m.MainTopic.Title == SelectedMainTopic.Title)
                                         .ToList();
             foreach (SubTopic subTopic in subTopics)
             {
                 db.SubTopics.Remove(subTopic);
             }
             MainTopic mainTopic = db.MainTopics.Where(m => m.Title == SelectedMainTopic.Title).FirstOrDefault();
             db.Set <MainTopic>().Remove(mainTopic);
             db.SaveChanges();
         }
         SelectedMainTopic     = null;
         MainTopics            = Context.MainTopics.ToList();
         CreatedMainTopicTitle = null;
         SubTopics             = null;
     }
 }
Ejemplo n.º 7
0
        public async Task CreatedAccountsAreDeletedWhenDeleteOperationCalled()
        {
            User user = new User()
            {
                Username       = "******",
                PasswordHashed = "test3",
                Email          = "*****@*****.**",
                DateJoined     = DateTime.Now
            };

            Account account = new Account()
            {
                User = user,
            };

            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                await context.Set <Account>().AddAsync(account);

                await context.SaveChangesAsync();

                await accountService.Delete(account.Id);

                Account retrievedAccount = context.Accounts.Where(a => a.User.Username == "test3").FirstOrDefault();
                Assert.IsNull(retrievedAccount);
            }
        }
        public virtual async Task <T> Get(int id)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id);

                return(entity);
            }
        }
        public virtual async Task <IEnumerable <T> > GetAll()
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                IEnumerable <T> entities = await context.Set <T>()
                                           .ToListAsync();

                return(entities);
            }
        }
        public virtual async Task <IEnumerable <T> > GetByParameter(IdentityObject parameter)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                IEnumerable <T> entities = await context.Set <T>().Where(t => t.Id == parameter.Id)
                                           .ToListAsync();

                return(entities);
            }
        }
        public virtual async Task <T> Update(int id, T entity)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                entity.Id = id;
                context.Set <T>().Update(entity);
                await context.SaveChangesAsync();

                return(entity);
            }
        }
        public virtual async Task <T> Create(T entity)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                EntityEntry <T> createdResult = await context.Set <T>().AddAsync(entity);

                await context.SaveChangesAsync();

                return(createdResult.Entity);
            }
        }
        public override async Task <Test> Get(int id)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                Test entity = await context.Set <Test>()
                              .Include(t => t.Questions)
                              .ThenInclude(q => q.QuestionOptions)
                              .FirstOrDefaultAsync((e) => e.Id == id);

                return(entity);
            }
        }
Ejemplo n.º 14
0
        public async Task <SubTopic> UpdateSubTopic(int subTopicId, string subTopicTitle, string subTopicIntro, string subTopicContent, string subTopicSummary)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                SubTopic retrievedSubTopic = context.SubTopics.Where(st => st.Id == subTopicId).FirstOrDefault();
                retrievedSubTopic.Title        = subTopicTitle;
                retrievedSubTopic.Introduction = subTopicIntro;
                retrievedSubTopic.Content      = subTopicContent;
                retrievedSubTopic.Summary      = subTopicSummary;

                context.Set <SubTopic>().Update(retrievedSubTopic);
                await context.SaveChangesAsync();

                return(retrievedSubTopic);
            }
        }
Ejemplo n.º 15
0
 private void FuncToCall5(object context)
 {
     if (CreatedMainTopicTitle == null || CreatedSubTopicTitle == "")
     {
         MessageBox.Show("You must enter a valid name in order to update a main topic.");
     }
     else
     {
         using (SelfAssessmentDbContext db = new SelfAssessmentDbContext())
         {
             MainTopic mainTopic = db.MainTopics.Where(m => m.Title == SelectedMainTopic.Title).FirstOrDefault();
             mainTopic.Title = CreatedMainTopicTitle;
             db.Set <MainTopic>().Update(mainTopic);
             db.SaveChanges();
         }
         MainTopics            = Context.MainTopics.ToList();
         CreatedMainTopicTitle = null;
     }
 }
        public async Task <Test> CreateNewTest(Test test, string seriesName)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                Test checkTest = context.Tests.Where(t => t.TestName == test.TestName).FirstOrDefault();
                if (checkTest == null)
                {
                    test.TestSeries = context.TestSeries
                                      .Where(q => q.TestSeriesName == seriesName)
                                      .FirstOrDefault();
                    EntityEntry <Test> createdResult = await context.Set <Test>().AddAsync(test);

                    await context.SaveChangesAsync();

                    return(createdResult.Entity);
                }
                return(null);
            }
        }
Ejemplo n.º 17
0
        public async Task <SubTopic> CreateNewSubTopic(string mainTopicTitle, string subTopicTitle, string subTopicIntro, string subTopicContent, string subTopicSummary)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                MainTopic mainTopic   = context.MainTopics.Where(m => m.Title == mainTopicTitle).FirstOrDefault();
                SubTopic  newSubTopic = new SubTopic()
                {
                    Title        = subTopicTitle,
                    Introduction = subTopicIntro,
                    Content      = subTopicContent,
                    Summary      = subTopicSummary,
                    MainTopic    = mainTopic
                };
                EntityEntry <SubTopic> createdResult = await context.Set <SubTopic>().AddAsync(newSubTopic);

                await context.SaveChangesAsync();

                return(createdResult.Entity);
            }
        }