public ActionResult DeleteConfirmed(Guid id) { Data.Quiz quiz = db.Quizzes.Find(id); db.Quizzes.Remove(quiz); db.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <int> CreateAsync(string userId) { var rng = new Random(); var quiz = new Data.Quiz { Finished = DateTime.Now }; await this.db.Quizzes.AddAsync(quiz); var questions = this.db.Questions.ToArray(); var questionsForCurrentQuiz = new List <Question>(); for (int i = 0; i < 5; i++) { var question = questions[rng.Next(questions.Count())]; while (questionsForCurrentQuiz.Any(q => q.Id == question.Id)) { question = questions[rng.Next(questions.Count())]; } questionsForCurrentQuiz.Add(question); await this.db.QuestionQuizzes.AddAsync(new QuestionQuiz { Question = question, Quiz = quiz }); } await this.db.SaveChangesAsync(); return(quiz.Id); }
public ActionResult Edit(Data.Quiz quiz) { if (ModelState.IsValid) { db.Entry(quiz).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(quiz)); }
public ActionResult Create([Bind(Include = "Id,Name,Description")] Data.Quiz quiz) { if (ModelState.IsValid) { quiz.Id = Guid.NewGuid(); db.Quizzes.Add(quiz); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(quiz)); }
// GET: Admin/Quiz/Edit/5 public ActionResult Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Data.Quiz quiz = db.Quizzes.Find(id); if (quiz == null) { return(HttpNotFound()); } return(View(quiz)); }
public async Task <bool> CreateQuiz(Models.NewQuizModel newQuiz) { var quiz = new Data.Quiz { Title = newQuiz.Title, Questions = new List <Data.Question>() }; foreach (var question in newQuiz.NewQuestions) { var questionToAdd = new Data.Question { QuestionString = question.Question, CorrectAnswer = new Answer { AnswerText = question.CorrectAnswer }, WrongAnswers = new WrongAnswers() { Answers = new List <Answer>() } }; foreach (var wrongAnswer in question.WrongAnswers) { var wrongAnswerToAdd = new Answer { AnswerText = wrongAnswer }; questionToAdd.WrongAnswers.Answers.Add(wrongAnswerToAdd); } ; quiz.Questions.Add(questionToAdd); } ; try { await _quizManagerContext.Quizzes.AddAsync(quiz); await _quizManagerContext.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public ActionResult Questions(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Data.Quiz quiz = db.Quizzes.Find(id); if (quiz == null) { return(HttpNotFound()); } var questions = new List <Question>(); foreach (var quizSection in quiz.Sections) { questions.AddRange(quizSection.Questions.Select(quizQuestion => quizQuestion.Question)); } return(View(questions)); }