Example #1
0
 public DalTest GetById(int key)
 {
     Test test = context.Set<Test>().FirstOrDefault(t => t.Id == key);
     DalTest dalTest = new DalTest()
         {
             Id = test.Id,
             Name = test.Name,
             Questions = new List<DalQuestion>()
         };
     foreach (var question in test.Questions)
     {
         DalQuestion dalQuestion = new DalQuestion()
         {
             Id = question.Id,
             Text = question.Text,
             Options = new List<DalOption>()
         };
         foreach (var option in question.Options)
         {
             DalOption dalOption = new DalOption()
             {
                 Id = option.Id,
                 IsAnswer = option.IsAnswer,
                 QuestionId = option.QuestionId,
                 Text = option.Text
             };
             dalQuestion.Options.Add(dalOption);
         }
         dalTest.Questions.Add(dalQuestion);
     }
     return dalTest;
 }
        public IEnumerable<DalQuestion> GetAll()
        {
            List<Question> questions = context.Set<Question>().ToList();
            List<DalQuestion> dalQuestions = new List<DalQuestion>();
            foreach (var question in questions)
            {
                DalQuestion dalQuestion = new DalQuestion()
                {
                    Id = question.Id,
                    Text = question.Text,
                    Options = new List<DalOption>()

                };
                foreach (var option in question.Options)
                {
                    DalOption dalOption = new DalOption()
                    {
                        Id = option.Id,
                        IsAnswer = option.IsAnswer,
                        QuestionId = option.QuestionId,
                        Text = option.Text
                    };
                    dalQuestion.Options.Add(dalOption);
                }
                dalQuestions.Add(dalQuestion);
            }
            return dalQuestions;
        }
        public void Create(DalQuestion e)
        {
            Question question = new Question()
            {
                Text = e.Text,
                Options = new List<Option>()
            };
            foreach (DalOption dalOption in e.Options)
            {
                Option option = new Option()
                {
                    Question = question,
                    IsAnswer = dalOption.IsAnswer,
                    Text = dalOption.Text
                };
                question.Options.Add(option);
            }

            context.Set<Question>().Add(question);
            context.SaveChanges();
        }
 public void Delete(DalQuestion e)
 {
     throw new NotImplementedException();
 }
        public void Update(DalQuestion entity)
        {
            Question original = context.Set<Question>()
                                        .Include(x => x.Options)
                                        .Single(c => c.Id == entity.Id);

            context.Entry(original).CurrentValues.SetValues(entity);

            foreach (var dalOption in original.Options.ToList())
            {
                if (entity.Options.All(s => s.Id != dalOption.Id))
                    context.Set<Option>().Remove(dalOption);
            }

            foreach (Option dalOption in entity.ToOrm().Options)
            {
                var option = original.Options.SingleOrDefault(s => s.Id == dalOption.Id);
                if (option != null)
                {
                    context.Entry(option).CurrentValues.SetValues(dalOption);
                }
                else
                {
                    original.Options.Add(dalOption);
                }

            }

            context.SaveChanges();
        }