public async Task <IActionResult> PostSolvedQuiz(UserSolvedQuiz solvedQuiz) { var result = await _solvingQuizzesService.CheckSolvedQuizAsync(solvedQuiz, UserId); if (result == null) { return(BadRequest()); } await _statisticsService.IncreaseSolveSessions(solvedQuiz.QuizId); return(Ok(result)); }
// Create/Update/Delete methods public async Task <SolvedQuiz> CreateSolvedQuiz(UserSolvedQuiz solvedQuiz, int correctCount, int badCount, long finishTime, List <SolvedQuestion> questions, string userId) { var statsQuiz = new SolvedQuiz { QuizId = solvedQuiz.QuizId, UserId = userId, CreationTime = solvedQuiz.CreatedTime, FinishTime = finishTime, SolveTime = solvedQuiz.SolvingTime, CorrectCount = correctCount, BadCount = badCount, Questions = questions }; await _solvedQuizRepository.Create(statsQuiz); var result = await Context.SaveChangesAsync() > 0; return(result ? statsQuiz : null); }
public async Task <object> CheckSolvedQuizAsync(UserSolvedQuiz solvedQuiz, string userId) { var finishTime = CurrentTime; var isPublic = await _quizzesRepository.IsPublicAsync(solvedQuiz.QuizId); if (!isPublic) { var haveReadAccess = await _quizzesRepository.HaveReadAccessToQuizAsync(userId, solvedQuiz.QuizId); if (!haveReadAccess) { return(null); } } var solvedQuestionIds = solvedQuiz.UserSolvedQuestions.Select(q => q.QuestionId); var questionsWithCorrectAnswers = await _questionsRepository .GetAllByQuizId(solvedQuiz.QuizId, solvedQuiz.CreatedTime, true) .Include(q => q.Versions) .Include(q => q.Answers) .ThenInclude(a => a.Versions) .Where(q => solvedQuestionIds.Contains(q.Id)) .Select(q => new { q.Id, q.Versions .Where(v => v.CreationTime <= solvedQuiz.CreatedTime) .OrderByDescending(v => v.CreationTime) .FirstOrDefault() .Value, Answers = q.Answers .Where(a => !a.IsDeleted) .Select(a => new { a.Id, Version = a.Versions .Where(v => v.CreationTime <= solvedQuiz.CreatedTime) .OrderByDescending(v => v.CreationTime) .FirstOrDefault() }) .Select(a => new { a.Id, a.Version.Value, a.Version.IsCorrect }) }) .ToListAsync(); if (questionsWithCorrectAnswers == null || questionsWithCorrectAnswers.Count == 0) { return(null); } var questionsWithCorrectAnswersAndUserAnswers = questionsWithCorrectAnswers .Select(q => new { q.Id, q.Value, q.Answers, UserSelectedAnswers = solvedQuiz.UserSolvedQuestions .First(sq => sq.QuestionId == q.Id) .SelectedAnswerIds ?? new List <long>() }); var questionsWithCorrectAnswersAndUserAnswersWithCorrectness = questionsWithCorrectAnswersAndUserAnswers .Select(q => new { q.Id, q.Value, Answers = q.Answers.Select(a => new { a.Id, a.Value, a.IsCorrect, Selected = q.UserSelectedAnswers.Contains(a.Id) }), AnsweredCorrectly = q.Answers .Where(a => a.IsCorrect) .Select(a => a.Id) .OrderBy(a => a) .SequenceEqual(q.UserSelectedAnswers.OrderBy(ua => ua)) }) .ToList(); var correctCount = questionsWithCorrectAnswersAndUserAnswersWithCorrectness .Count(q => q.AnsweredCorrectly); var badCount = questionsWithCorrectAnswersAndUserAnswersWithCorrectness .Count(q => !q.AnsweredCorrectly); var solvedQuestions = questionsWithCorrectAnswersAndUserAnswersWithCorrectness.Select(q => new SolvedQuestion { QuestionId = q.Id, AnsweredCorrectly = q.AnsweredCorrectly, Answers = q.Answers.Select(a => new SolvedAnswer { AnswerId = a.Id, IsSelected = a.Selected, IsCorrect = a.IsCorrect }).ToList() }).ToList(); await _statisticsService.CreateSolvedQuiz(solvedQuiz, correctCount, badCount, finishTime, solvedQuestions, userId); return(new { Questions = questionsWithCorrectAnswersAndUserAnswersWithCorrectness, SolvingTime = solvedQuiz.SolvingTime, MaxSolvingTime = solvedQuiz.MaxSolvingTime, CorrectCount = correctCount, BadCount = badCount }); }