예제 #1
0
        public async Task <IActionResult> Index()
        {
            var admin = await this.userManager.GetUserAsync(HttpContext.User);

            var adminId = admin.Id;

            //cache
            if (!cache.TryGetValue(adminId, out IEnumerable <TestDto> authorTests))
            {
                authorTests = adminService.GetTestsByAuthor(adminId).ToList();

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(5))
                                        .SetAbsoluteExpiration(TimeSpan.FromMinutes(20));

                cache.Set(adminId, authorTests, cacheEntryOptions);
            }

            if (!cache.TryGetValue("TestResults", out IEnumerable <UserTestDto> userResults))
            {
                userResults = adminService.GetUserResults().ToList();

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(5))
                                        .SetAbsoluteExpiration(TimeSpan.FromMinutes(20));;

                cache.Set("TestResults", userResults, cacheEntryOptions);
            }

            //var userResults = adminService.GetUserResults().ToList();
            //var authorTests = adminService.GetTestsByAuthor(adminId).ToList();

            // Model creating
            var userResultsList = new List <UserTestViewModel>();
            var authorTestsList = new List <TestViewModel>();

            // UserTestViewModels creating
            foreach (var userResult in userResults)
            {
                var currentModel = new UserTestViewModel()
                {
                    TestName      = userResult.Test.Title,
                    UserName      = userResult.User.UserName,
                    Category      = tests.GetCategoryNameByTestId(userResult.TestId),
                    RequestedTime = tests.GetTestRequestedTime(userResult.TestId),
                    ExecutionTime = (int)userResult.ExecutionTime.TotalMinutes,
                    Result        = (userResult.IsPassed) ? "Passed" : "Failed"
                };

                userResultsList.Add(currentModel);
            }

            // TestViewModels creating
            foreach (var authorTest in authorTests)
            {
                var currentModel = new TestViewModel()
                {
                    Id           = authorTest.Id.ToString(),
                    TestName     = authorTest.Title,
                    CategoryName = authorTest.Category.Name,
                    Status       = tests.GetStatusNameByTestId(authorTest.Id),
                    CreatedOn    = authorTest.CreatedOn
                };

                authorTestsList.Add(currentModel);
            }

            // IndexViewModel creating
            var model = new IndexViewModel()
            {
                AdminName   = admin.UserName,
                UserResults = userResultsList,
                Tests       = authorTestsList
            };

            return(View(model));
        }