public bool CheckQuiz(QuizBm bind, string userId) { Quiz quiz = Context.Quizzes.Find(bind.QuizId); if (quiz == null) { throw new ArgumentNullException("No such quiz!"); } try { for (int i = 0; i < quiz.Questions.Count; i++) { if (bind.Questions[i].Answer != quiz.Questions[i].Answer) { return(false); } } Player player = this.Context.Players.FirstOrDefault(p => p.AppUser.Id == userId); player.Points += Constants.WinPointsPerQuiz; this.Context.Quizzes.Find(bind.QuizId).PlayersPlayed.Add(player); Context.SaveChanges(); } catch (Exception e) { throw new Exception("Something went wrong!"); } return(true); }
public ActionResult Quiz([Bind(Include = "Questions,QuizId")] QuizBm bind) { string userId = User.Identity.GetUserId(); if (ModelState.IsValid) { ViewBag.InPost = true; ViewBag.AreCorrect = this.service.CheckQuiz(bind, userId); } return(View(this.service.GetQuizViewModel(bind.QuizId, userId))); }