Ejemplo n.º 1
0
        public async Task <IActionResult> Index(string id)
        {
            var user = await this.userManager.GetUserAsync(HttpContext.User);

            StatusType check     = resultService.CheckForTakenTest(user.Id, id);
            TestDto    testDto   = null;
            var        startTime = DateTime.Now;

            if (check == StatusType.TestSubmitted)
            {
                return(RedirectToAction("Index", "Dashboard"));
            }

            if (check == StatusType.TestNotSubmitted)
            {
                testDto   = this.resultService.GetTestFromCategory(user.Id, id);
                startTime = (DateTime)resultService.GetUserTest(user.Id, testDto.Id).StartTime;
            }
            else
            {
                testDto = this.testService.GetRandomTestByCategory(id);
            }

            var questions = mapper.ProjectTo <QuestionViewModel>(testDto.Questions.AsQueryable()).ToList();

            var model = new IndexViewModel()
            {
                UserId       = user.Id,
                TestId       = testDto.Id,
                TestName     = testDto.TestName,
                Duration     = testDto.Duration,
                CategoryName = id,
                Questions    = questions,
                StartedOn    = startTime
            };

            if (check == StatusType.TestNotStarted)  // move to service
            {
                var resultDto = mapper.MapTo <UserTestDto>(model);
                resultDto.Id = Guid.NewGuid();
                resultService.AddResult(resultDto);
                this.saver.SaveChanges();
            }

            return(View(model));
        }