コード例 #1
0
        public async Task <IActionResult> Statistics()
        {
            string currentUserId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var testResultQuery  = _testResults.GetAll().Where(x => x.UserId == currentUserId);
            var totalScoresQuery = _totalScores.GetAll().Where(x => x.UserId == currentUserId);

            int idLangLearn  = (int)HttpContext.Session.GetInt32("idLangLearn");
            int idLangNative = (int)HttpContext.Session.GetInt32("idLangNative");

            List <DTO> NativeLearnLang = await _languages.GetAll()
                                         .Join(
                _languageTranslations.GetAll().Where(s => s.LangId == idLangNative),
                lang => lang.Id,
                langTrans => langTrans.LangId,
                (lang, langTrans) => new DTO
            {
                Id             = langTrans.NativeLangId,
                WordNativeLang = langTrans.Translation,
            }
                )
                                         .Join(
                _totalScores.GetAll(),
                lang => lang.Id,
                total => total.LangId,
                (lang, total) => new DTO
            {
                Id             = lang.Id,
                WordNativeLang = lang.WordNativeLang,
                Total          = total.Total,
                UserId         = total.UserId
            }
                )
                                         .Where(x => x.UserId == currentUserId).Distinct().ToListAsync();

            List <DTO> NativeLearnLangTests;
            List <DTO> NativeLearnLangSubCat;
            List <DTO> NativeLearnLangCat;

            NativeLearnLangTests = await _tests.GetTranslations(idLangLearn, idLangNative, null);

            NativeLearnLangSubCat = await _categories.GetTranslations(idLangLearn, idLangNative, -1);

            NativeLearnLangCat = await _categories.GetTranslations(idLangLearn, idLangNative, null);

            IQueryable <DTOTestResults> LearnLangCat = testResultQuery
                                                       .Join(
                totalScoresQuery,
                testResult => testResult.LangId,
                totalScore => totalScore.LangId,
                (testResult, totalScore) => new DTOTestResults
            {
                Id              = testResult.Id,
                TestName        = NativeLearnLangTests.Find(item => item.Id == testResult.TestId).WordNativeLang,
                LangName        = NativeLearnLang.Find(item => item.Id == testResult.LangId).WordNativeLang,
                SubCategoryName = NativeLearnLangSubCat.Find(item => item.Id == testResult.CategoryId).WordNativeLang,
                CategoryName    = NativeLearnLangCat.Find(item => item.Id == testResult.Category.ParentId).WordNativeLang,
                TestDate        = testResult.TestDate,
                Result          = testResult.Result
            }
                );

            List <HashSet <string> >      Categories    = new List <HashSet <string> >();
            List <HashSet <string> >      SubCategories = new List <HashSet <string> >();
            List <HashSet <string> >      Tests         = new List <HashSet <string> >();
            List <List <DTOTestResults> > TestScores    = new List <List <DTOTestResults> >();

            foreach (var lang in NativeLearnLang)
            {
                HashSet <string> categories = new HashSet <string>();

                foreach (var item in LearnLangCat.Where(item => item.LangName == lang.WordNativeLang))
                {
                    categories.Add(item.CategoryName);
                }

                Categories.Add(categories);

                foreach (var item in categories)
                {
                    HashSet <string> subCategories = new HashSet <string>();

                    foreach (var subCat in LearnLangCat.Where(subCat => subCat.LangName == lang.WordNativeLang && subCat.CategoryName == item))
                    {
                        subCategories.Add(subCat.SubCategoryName);
                    }

                    SubCategories.Add(subCategories);

                    foreach (var subCat in subCategories)
                    {
                        HashSet <string> tests = new HashSet <string>();

                        foreach (var test in LearnLangCat.Where(test => test.LangName == lang.WordNativeLang && test.CategoryName == item))
                        {
                            tests.Add(test.TestName.Replace("\\", "/"));
                        }

                        Tests.Add(tests);

                        foreach (var test in tests)
                        {
                            List <DTOTestResults> testScores = new List <DTOTestResults>();

                            foreach (var testScore in LearnLangCat.Where(testScore => testScore.LangName == lang.WordNativeLang &&
                                                                         testScore.CategoryName == item && testScore.SubCategoryName == subCat &&
                                                                         testScore.TestName.Replace("\\", "/") == test))
                            {
                                testScores.Add(testScore);
                            }

                            TestScores.Add(testScores);
                        }
                    }
                }
            }

            DTOStatistics statistics = new DTOStatistics()
            {
                LangList      = NativeLearnLang,
                Categories    = Categories,
                SubCategories = SubCategories,
                Tests         = Tests,
                TestScores    = TestScores
            };

            if (_signInManager.IsSignedIn(User))
            {
                statistics.IsSignedIn = true;
            }
            else
            {
                statistics.IsSignedIn = false;
            }

            return(View("./Manage/Statistics", statistics));
        }
コード例 #2
0
        public async Task <DTOStatistics> Statistics()
        {
            string currentUserId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var testResultQuery  = _testResults.GetAll().Where(x => x.UserId == currentUserId);
            var totalScoresQuery = _totalScores.GetAll().Where(x => x.UserId == currentUserId);

            int idLangLearn  = (int)HttpContext.Session.GetInt32("idLangLearn");
            int idLangNative = (int)HttpContext.Session.GetInt32("idLangNative");

            List <DTO> NativeLearnLang = await _languages.GetAll()
                                         .Join(
                _languageTranslations.GetAll().Where(s => s.LangId == idLangNative),
                lang => lang.Id,
                langTrans => langTrans.LangId,
                (lang, langTrans) => new DTO
            {
                Id             = langTrans.NativeLangId,
                WordNativeLang = langTrans.Translation,
            }
                )
                                         .Join(
                _totalScores.GetAll(),
                lang => lang.Id,
                total => total.LangId,
                (lang, total) => new DTO
            {
                Id             = lang.Id,
                WordNativeLang = lang.WordNativeLang,
                Total          = total.Total,
                UserId         = total.UserId
            }
                )
                                         .Where(x => x.UserId == currentUserId).Distinct().ToListAsync();

            List <DTO> NativeLearnLangTests;
            List <DTO> NativeLearnLangSubCat;
            List <DTO> NativeLearnLangCat;

            NativeLearnLangTests = await _tests.GetTranslations(idLangLearn, idLangNative, null);

            NativeLearnLangSubCat = await _categories.GetTranslations(idLangLearn, idLangNative, -1);

            NativeLearnLangCat = await _categories.GetTranslations(idLangLearn, idLangNative, null);

            List <DTOTestResults> LearnLangCat = await testResultQuery
                                                 .Join(
                totalScoresQuery,
                testResult => testResult.LangId,
                totalScore => totalScore.LangId,
                (testResult, totalScore) => new DTOTestResults
            {
                Id              = testResult.Id,
                TestName        = NativeLearnLangTests.Find(item => item.Id == testResult.TestId).WordNativeLang,
                LangName        = NativeLearnLang.Find(item => item.Id == testResult.LangId).WordNativeLang,
                SubCategoryName = NativeLearnLangSubCat.Find(item => item.Id == testResult.CategoryId).WordNativeLang,
                CategoryName    = NativeLearnLangCat.Find(item => item.Id == testResult.Category.ParentId).WordNativeLang,
                TestDate        = testResult.TestDate,
                Result          = testResult.Result
            }
                ).ToListAsync();

            DTOStatistics statistics = new DTOStatistics()
            {
                testResults = LearnLangCat,
                LangList    = NativeLearnLang,
                CatList     = NativeLearnLangCat,
                SubCatList  = NativeLearnLangSubCat
            };

            return(statistics);
        }