public async Task <IActionResult> Post([FromBody] SaveQuizDTO saveQuizDTO) { try { var quiz = mapper.Map <Quiz>(saveQuizDTO); quizRepo.Add(quiz); await quizRepo.SaveChangesAsync(); var quizDTO = mapper.Map <QuizDTO>(quiz); return(CreatedAtAction(nameof(GetById), new { id = quiz.Id }, quizDTO)); } catch (Exception ex) { logger.LogError(ex.Message); return(BadRequest("Toevoegen mislukt")); } }
public async Task <IActionResult> NieuweQuiz([Bind("Onderwerp, MoeilijkheidsgraadId, Beschrijving, Vragen")] Quiz quiz) { try { quizRepo.Add(quiz); await quizRepo.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { logger.LogError(ex.Message); return(View("Error", new ErrorViewModel() { RequestId = HttpContext.TraceIdentifier })); } }
public async Task <ActionResult> Create(IFormCollection collection, Quiz quiz) { try { quiz.UserID = User.FindFirstValue(ClaimTypes.NameIdentifier); quiz.QuizID = Guid.NewGuid(); var created = await quizrepo.Add(quiz); if (created == null) { throw new Exception("Invalid Entry"); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Console.WriteLine("Create geeft error " + ex.Message); ModelState.AddModelError("", "Create actie is mislukt voor " + quiz.QuizName); return(View(quiz)); } }
public async Task <IActionResult> TryCreateQuiz() { try { Quiz @quiz = new Quiz { Label = Request.Form["Title"], DifficultyID = Guid.Parse(Request.Form["Difficulty"]), Description = Request.Form["Description"], ID = Guid.NewGuid() }; Quiz result = await quizRepo.Add(@quiz); if (result == null) { throw new Exception(); } ICollection <string> keys = Request.Form.Keys; List <string> questions = new List <string>(); List <string> answers = new List <string>(); List <string> radioButtons = new List <string>(); foreach (string key in keys) { Console.WriteLine(key); Console.WriteLine(Request.Form[key]); if (Regex.IsMatch(key, @"^q[1-9]+$")) { Console.WriteLine("AAAAAAAAAAHHHHHHHHHHHHHHHHH"); questions.Add(key); } else if (Regex.IsMatch(key, @"^q[1-9]+a[1-9]+$")) { answers.Add(key); } else if (Regex.IsMatch(key, @"^q[1-9]+Radio$")) { radioButtons.Add(key); } } Console.WriteLine(questions.Count); for (int i = 0; i < questions.Count; ++i) { Console.WriteLine("Going through questions"); string questionNumber = questions[i].Substring(1); Question @question = new Question() { ID = Guid.NewGuid(), Label = Request.Form[questions[i]], QuizID = result.ID }; Question questionResult = await questionRepo.Add(@question); if (questionResult == null) { throw new Exception(); } string correctAnswer = "99999"; for (int j = 0; j < radioButtons.Count; ++j) { if (Regex.IsMatch(radioButtons[j], @"^q" + questionNumber + @"Radio$")) { correctAnswer = Regex.Match(Request.Form[radioButtons[j]], @"[1-9]+", RegexOptions.RightToLeft).Value; Console.WriteLine(correctAnswer); } } for (int j = 0; j < answers.Count; ++j) { if (Regex.IsMatch(answers[j], @"^q" + questionNumber)) { bool isCorrectAnswer = (Regex.Match(answers[j], @"[1-9]+", RegexOptions.RightToLeft).Value == correctAnswer); Answer @answer = new Answer() { ID = Guid.NewGuid(), Label = Request.Form[answers[j]], CorrectAnswer = isCorrectAnswer, QuestionID = questionResult.ID }; Answer answerResult = await answerRepo.Add(@answer); if (answerResult == null) { throw new Exception(); } } } } return(RedirectToAction(nameof(Index))); } catch (Exception) { ModelState.AddModelError("", "Create is unable to save"); return(View("index")); } }