コード例 #1
0
 public void EditQuiz(int id, Quiz quizNew)
 {
     using (var db = new QuizDbContext())
     {
         foreach (var item in quizNew.Questions)
         {
             if (item.QuestionId == 0)
             {
                 db.QuestionTable.Add(item);
             }
             else
             {
                 foreach (var answer in item.Answers)
                 {
                     if (answer.AnswerId == 0)
                     {
                         db.AnswerTable.Add(answer);
                     }
                     else
                     {
                         db.Entry(answer).State = EntityState.Modified;
                     }
                 }
                 db.Entry(item).State = EntityState.Modified;
             }
         }
         db.QuizTable.Attach(quizNew);
         db.Entry(quizNew).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
コード例 #2
0
 public Answer GetAnswerById(int id)
 {
     using(var db = new QuizDbContext())
     {
         return db.AnswerTable.Find(id);
     }
 }
コード例 #3
0
 public void AddQuiz(Quiz newQuiz)
 {
     using (var db = new QuizDbContext())
     {
         db.QuizTable.Add(newQuiz);
         db.SaveChanges();
     }
 }
コード例 #4
0
 public void DeleteContent(int id)
 {
     using (var db = new QuizDbContext())
     {
         var quizToDelete = db.QuizTable.Find(id);
         db.QuizTable.Remove(quizToDelete);
         db.SaveChanges();
     }
 }
コード例 #5
0
        public List<Quiz> GetQuizAll()
        {
            using (var db = new QuizDbContext())
            {
                var quizList = from quiz in db.QuizTable
                               select quiz;

                return quizList.ToList();
            }

        }
コード例 #6
0
        public QuestionViewModel GetQuestions(int id)
        {
            using (var db = new QuizDbContext())
            {
                var qList = db.QuestionTable
                    .Where(question => question.QuizId == id)
                    .Include(question => question.Answers)
                    .ToList();

                return new QuestionViewModel(qList[_randy.Next(0, qList.Count)]);
            }
        }
コード例 #7
0
 public Quiz GetQuizById(int id)
 {
     using (var db = new QuizDbContext())
     {
         var quiz = db.QuizTable.Find(id);
         db.Entry(quiz).Collection(q => q.Questions).Load();
         foreach (var item in quiz.Questions)
         {
             var question = db.QuestionTable.Find(item.QuestionId);
             db.Entry(question).Collection(quest => quest.Answers).Load();
         }
         return quiz;
     }
 }
コード例 #8
0
        public void LoadQuizSample()
        {
            Quiz newQuiz = new Quiz()
            {
                Title     = "Objects and Classes",
                Topic     = "Javascript",
                Active    = false,
                Questions = new List <Question>
                {
                    new Question
                    {
                        QuestionId = 1,
                        QuizId     = 1,
                        Content    = "What is your name?",
                        Answers    = new List <Answer>
                        {
                            new Answer {
                                AnswerId   = 1,
                                QuestionId = 1,
                                Content    = "Wilmer",
                                IsCorrect  = false
                            },
                            new Answer
                            {
                                AnswerId   = 2,
                                QuestionId = 1,
                                Content    = "Bob",
                                IsCorrect  = false
                            },
                            new Answer
                            {
                                AnswerId   = 3,
                                QuestionId = 1,
                                Content    = "Ricky",
                                IsCorrect  = false
                            },
                            new Answer
                            {
                                AnswerId   = 4,
                                QuestionId = 1,
                                Content    = "Cristian",
                                IsCorrect  = true
                            }
                        }
                    },

                    new Question
                    {
                        QuestionId = 2,
                        QuizId     = 1,
                        Content    = "What state are we located in?",
                        Answers    = new List <Answer>
                        {
                            new Answer {
                                AnswerId   = 1,
                                QuestionId = 2,
                                Content    = "Florida",
                                IsCorrect  = false
                            },
                            new Answer
                            {
                                AnswerId   = 2,
                                QuestionId = 2,
                                Content    = "Kansas",
                                IsCorrect  = false
                            },
                            new Answer
                            {
                                AnswerId   = 3,
                                QuestionId = 2,
                                Content    = "Georgia",
                                IsCorrect  = true
                            },
                            new Answer
                            {
                                AnswerId   = 4,
                                QuestionId = 2,
                                Content    = "Texas",
                                IsCorrect  = false
                            }
                        }
                    }
                }
            };

            AddQuiz(newQuiz);

            using (var db = new QuizDbContext())
            {
                //Quiz quizOne = new Quiz
                //{
                //    Title = "Random Test Questions",
                //    Topic = "Testing",
                //    Questions = new List<Question>
                //    {
                //        new Question {
                //        Content = "What area are we located in?",
                //        Answers = new List<Answer> {
                //        new Answer { Content = "Norcross" , IsCorrect = false},
                //        new Answer { Content = "Dalton" , IsCorrect = false },
                //        new Answer { Content = "Lilburn", IsCorrect = true },
                //        new Answer { Content = "Kenessaw", IsCorrect = false }
                //        }
                //        }
                //    }
                //};

                //db.QuizTable.Add(quizOne);
                //db.SaveChanges();

                var quizList = from quiz in db.QuizTable
                               select quiz;

                foreach (var item in quizList)
                {
                    AddQuiz(item);
                }
            }
        }