Пример #1
0
        public async System.Threading.Tasks.Task UpdateTestAsync(int testId, TestModel testModel)
        {
            // update general test info
            var testEntity = _exercisesContext.CandidateTests.Single(t => t.Id == testId);

            testEntity.Description  = testModel.Description;
            testEntity.MaximumScore = testModel.MaximumScore;
            testEntity.Name         = testModel.Name;
            testEntity.TimeMinutes  = testModel.TimeMinutes;
            testEntity.Subject      = testModel.Subject;

            // delete test questions that do not exist in model
            foreach (var questionEntity in _exercisesContext.TestQuestions.Where(q => q.TestId == testId))
            {
                if (!testModel.Questions.Any(q => q.Id == questionEntity.Id))
                {
                    _exercisesContext.TestQuestions.Remove(questionEntity);
                }
            }

            foreach (var questionModel in testModel.Questions)
            {
                // update existent questions
                if (_exercisesContext.TestQuestions.Any(q => q.Id == questionModel.Id))
                {
                    _exercisesContext.TestQuestions.Single(q => q.Id == questionModel.Id).Text = questionModel.Text;
                    var variantEntities = _exercisesContext.TestAnswerVariants.Where(v => v.QuestionId == questionModel.Id);
                    foreach (var variantEntity in variantEntities)
                    {
                        var variantModel = questionModel.AnswerVariants.Single(v => v.Id == variantEntity.Id);
                        variantEntity.Text      = variantModel.Text;
                        variantEntity.IsCorrect = variantModel.IsCorrect;
                    }
                }

                // add new questions
                else
                {
                    var questionEntity = Mapper.Map <QuestionModel, QuestionEntity>(questionModel);
                    questionEntity.TestId = testId;
                    _exercisesContext.TestQuestions.Add(questionEntity);
                }
            }

            await _exercisesContext.SaveChangesAsync();
        }
Пример #2
0
 public async System.Threading.Tasks.Task AddTestAsync(TestModel test)
 {
     _exercisesContext.CandidateTests.Add(Mapper.Map <TestModel, TestEntity>(test));
     await _exercisesContext.SaveChangesAsync();
 }