コード例 #1
0
        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);
                }
            }
        }
コード例 #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);
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        public async Task <T> AddAsync(T entity)
        {
            await context.Set <T>().AddAsync(entity);

            await context.SaveChangesAsync();

            return(entity);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
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);
            }
        }
コード例 #10
0
        public override async Task <bool> Delete(int testId)
        {
            using (SelfAssessmentDbContext context = new SelfAssessmentDbContext())
            {
                List <QuestionOption> questionOptionsToDelete = await context.QuestionOptions
                                                                .Include(q => q.Question)
                                                                .ThenInclude(q => q.Test)
                                                                .Where(q => q.Question.Test.Id == testId)
                                                                .ToListAsync();

                foreach (var questionOption in questionOptionsToDelete)
                {
                    context.QuestionOptions.Remove(questionOption);
                }

                List <Question> questionsToDelete = await context.Questions
                                                    .Include(q => q.Test)
                                                    .Where(q => q.Test.Id == testId)
                                                    .ToListAsync();

                foreach (var question in questionsToDelete)
                {
                    context.Questions.Remove(question);
                }

                List <TestResult> resultsToDelete = await context.TestResults
                                                    .Include(q => q.Test)
                                                    .Where(q => q.Test.Id == testId)
                                                    .ToListAsync();

                foreach (var testResult in resultsToDelete)
                {
                    context.TestResults.Remove(testResult);
                }
                Test testToDelete = await context.Tests
                                    .Where(t => t.Id == testId)
                                    .FirstOrDefaultAsync();

                context.Tests.Remove(testToDelete);
                await context.SaveChangesAsync();
            }
            return(true);
        }
コード例 #11
0
        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);
            }
        }
コード例 #12
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);
            }
        }