public async Task <TriviaTest> ConvertToTriviaAsync(Models.ViewModels.TestCreationModels.TestViewModel test) { var triviaTest = new TriviaTest(); triviaTest.Title = test.Title; triviaTest.Questions = new List <TriviaQuestion>(); await Task.Run(() => { foreach (var elem in test.Questions) { var question = new TriviaQuestion(); question.Title = elem.Title; question.Test = triviaTest; question.Options = new List <TriviaOption>(); foreach (var answer in elem.Options) { var option = new TriviaOption(answer); //option.Title = answer.Title; //option.IsCorrect = answer.IsCorrect; option.TriviaQuestion = question; question.Options.Add(option); } triviaTest.Questions.Add(question); } }); return(triviaTest); }
public ActionResult UpdateQuiz(TriviaTest testToUpdate) { if (testToUpdate != null && _triviaRepository.Get <TriviaTest>().Any(x => x.Id == testToUpdate.Id)) { if (ModelState.IsValid) { foreach (var question in testToUpdate.Questions) { foreach (var option in question.Options) { _triviaRepository.Update(option); } _triviaRepository.Update(question); } _triviaRepository.Update(testToUpdate); _triviaRepository.Commit(); return(Json(new { Success = true, Message = "Success" })); } } return(Json(new { Success = false, Message = "Error" })); }
public async Task <IHttpActionResult> Get(int id, int questionId = 0) { if (_triviaRepository.Get <TriviaTest>().Any(x => x.Id == id)) { _currentTest = _triviaRepository.Get <TriviaTest>().First(x => x.Id == id); } var isLast = (questionId == _currentTest.Questions.Count() - 1); var nextQuestion = new QuestionViewModel(); await Task.Run(() => { nextQuestion = _triviaService.GetQuestionAsync(_currentTest.Questions[questionId], isLast).Result; }); if (nextQuestion == null) { return(this.NotFound()); } return(this.Ok(nextQuestion)); }
public TestViewModel(TriviaTest test) { Id = test.Id; Title = test.Title; }