예제 #1
0
        public async Task <IActionResult> QuizStart([FromForm] QuizStartViewModel model)
        {
            var requiredQuiz = await _dbContext.Quizzes
                               .Where(e => e.Id == model.QuizId)
                               .FirstOrDefaultAsync()
                               .ConfigureAwait(false);

            if (requiredQuiz == null)
            {
                return(StatusCode(404, $"Тест с заданным идентификатором ({model.QuizId}) не найден."));
            }

            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            if (currentUser == null)
            {
                ModelState.AddModelError("UnknownUser", "Невозможно определить пользователя в этой сессии");
                return(View(model));
            }

            var generatedQuiz = new GeneratedQuizDto
            {
                Tag            = Guid.NewGuid().ToString().Replace("-", string.Empty),
                StartTime      = DateTime.Now,
                SourceQuiz     = requiredQuiz,
                RespondentUser = currentUser,
                UserAnswers    = new List <UserAnswerDto>()
            };

            await _dbContext.UserQuizzes.AddAsync(generatedQuiz).ConfigureAwait(false);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(RedirectToAction("QuizProcess", "Testing", new { token = generatedQuiz.Tag }));
        }
예제 #2
0
        public async Task <IActionResult> QuizStart()
        {
            var currentUser = await _userManager.GetUserAsync(HttpContext.User).ConfigureAwait(false);

            if (currentUser == null)
            {
                return(StatusCode(500, "Невозможно определить пользователя в этой сессии."));
            }

            var model     = new QuizStartViewModel();
            var userRoles = await _userManager.GetRolesAsync(currentUser).ConfigureAwait(false);

            model.UserName = currentUser.Name;
            model.UserRole = string.Join(" / ", userRoles);

            var startedQuizzesCount = await _dbContext.UserQuizzes
                                      .CountAsync(e => !e.IsEnded && e.RespondentUser.Id == currentUser.Id).ConfigureAwait(false);

            if (startedQuizzesCount > 0)
            {
                var startedQuizzes = await _dbContext.UserQuizzes
                                     .Include(e => e.RespondentUser)
                                     .Include(e => e.SourceQuiz)
                                     .Where(e => !e.IsEnded && e.RespondentUser.Id == currentUser.Id)
                                     .ToListAsync().ConfigureAwait(false);

                await UpdateQuizStatuses().ConfigureAwait(false);

                foreach (var quiz in startedQuizzes)
                {
                    var quizModel = new QuizDescriptionViewModel();
                    quizModel.QuizTag = quiz.Tag;
                    quizModel.Title   = quiz.SourceQuiz.Title;

                    model.StartedQuizzes.Add(quizModel);
                }
            }

            var allQuizzes = await _dbContext.Quizzes.ToListAsync().ConfigureAwait(false);

            foreach (var quiz in allQuizzes)
            {
                var quizModel = new QuizDescriptionViewModel();
                quizModel.QuizId = quiz.Id;
                quizModel.Title  = quiz.Title;

                model.AvailableQuizzes.Add(quizModel);
            }

            return(View(model));
        }