public async Task StartQuiz(QuizServiceModel model, string username) { var user = await this.context.Users.FirstOrDefaultAsync(u => u.UserName == username); var quiz = await this.context.Quizzes.Include(q => q.QuizQuestions).FirstOrDefaultAsync(q => q.Id == model.Id); if (user == null || quiz == null) { return; } var results = new UserResult(); results.UserId = user.Id; results.User = user; results.QuizId = quiz.Id; results.Quiz = quiz; model.Result = results; for (int i = 0; i < quiz.QuizQuestions.Count; i++) { for (int a = 0; a < model.Answers.Count; a++) { if (quiz.QuizQuestions[i].Id == model.Answers[a].QuestionId) { if (model.Answers[a].Answer == quiz.QuizQuestions[i].CorrectAnswer) { model.Result.UsersCorrectAnswers++; } else { model.Result.UsersWrongAnswers++; } } i++; } } model.Result.PointsEarned = model.Result.UsersCorrectAnswers; user.TotalQuizPoints += model.Result.PointsEarned; await this.context.UserResults.AddAsync(model.Result); //if user meets the requirements of any achievement he`ll get the achievement after finishes the quiz await this.achievementService.GetRookieAchievement(user); await this.achievementService.GetHundrederAchievement(user); await this.achievementService.GetExcellentAchievement(user); await this.achievementService.GetMasterAchievement(user); await this.context.SaveChangesAsync(); await this.notificationService.CreateQuizNotification(quiz, user, model); }
public async Task CreateQuizNotification(Quiz quiz, QuizaldoUser user, QuizServiceModel model) { var notification = new Notification(); notification.Text = $"{user.UserName} has completed {quiz.Name} quiz with " + $"{model.Result.UsersCorrectAnswers} correct and " + $"{model.Result.UsersWrongAnswers} wrong answers."; notification.CreatedOn = DateTime.Now; notification.UserId = user.Id; notification.User = user; await this.context.Notifications.AddAsync(notification); await this.context.SaveChangesAsync(); }