public async Task <IActionResult> StartTest(int topicId, int testId) { string userEmail = User.Identity.Name; int userId = await _userManager.GetUserIdAsync(userEmail); DomainTest test = await _testManager.GetTestByIdAsync(testId); DomainUserTest userTest = await _testManager.GetUserTestAsync(userId, testId); List <int> testStages = (await _questionManager.GetTestStagesByTestIdAsync(testId)).ToList(); int stagesCount = testStages.Count; var userQuestionIds = new List <int>(); DomainQuestion currQuestion = null; List <UserAnswerModel> currQuestionUserAnswers = null; QuestionTypes currQuestionType; string currQuestionImageLocation = string.Empty; int currQuestionStage = 0; int secondsLeft = 0; DateTime startTime = default; ViewData["TopicId"] = topicId; if (userTest.Status == TestStatus.Finished) { return(RedirectToAction("ShowTestsInTopicToUser", "Test", new { @TopicId = topicId })); } else if (userTest.Status == TestStatus.NotStarted) { await _testManager.UpdateUserTestStatusAsync(userId, testId, TestStatus.NotFinished); userQuestionIds = await CreateUserAnswersAndGetQuestionIdsSortedByStageAsync(testId, userId); currQuestion = await _questionManager.GetQuestionByIdAsync(userQuestionIds[0]); secondsLeft = test.Minutes * _toSecondsConstant; startTime = DateTime.Now; await _testManager.UpdateUserTestStartTimeAsync(userId, testId, startTime); await _testManager.UpdateUserTopicStatus(userId, topicId, TopicStatus.NotFinished); } else if (userTest.Status == TestStatus.NotFinished) { secondsLeft = test.Minutes * _toSecondsConstant - Convert.ToInt32(Math.Abs((userTest.StartTime - DateTime.Now).TotalSeconds)); if (secondsLeft <= 0) { await FinishTest(userId, testId); await TryFinishTopicAndIfTopicIsFinishedSendEmail(userId, topicId); return(RedirectToAction("ShowTestsInTopicToUser", "Test", new { @TopicId = topicId })); } (await _questionManager.GetUserQuestionsByTestIdSortedByStageAsync(userId, testId)).ToList() .ForEach(question => userQuestionIds.Add(question.Id)); currQuestion = await _questionManager.GetQuestionByIdAsync(userQuestionIds[0]); } currQuestionUserAnswers = (await _answersManager.GetUserAnswersByQuestionIdAsync(userId, currQuestion.Id)) .Select(x => _mapper.Map <UserAnswerModel>(x)).ToList(); currQuestionStage = currQuestion.Stage; currQuestionType = currQuestion.QuestionType; if (!string.IsNullOrWhiteSpace(currQuestion.ImageFullName)) { currQuestionImageLocation = $"/{WebExtensions.ImagesFolderName}/" + Path.GetFileName(currQuestion.ImageFullName); } var startTest = new StartTestModel() { TopicId = topicId, UserId = userId, TestId = testId, StagesCount = stagesCount, StartTime = startTime, SecondsLeft = secondsLeft, UserQuestionIds = string.Join(",", userQuestionIds), CurrQuestionId = currQuestion.Id, CurrQuestionText = currQuestion.Text, CurrQuestionStage = currQuestion.Stage, CurrQuestionType = currQuestionType, CurrQuestionUserAnswers = currQuestionUserAnswers, CurrQuestionImageLocation = currQuestionImageLocation }; ViewData["SubmitButton_1"] = "Next"; ViewData["SubmitButton_2"] = string.Empty; if (stagesCount == 1) { ViewData["SubmitButton_1"] = "Finish"; } return(View(startTest)); }