private Expression <Func <ScrimMatchInfo, bool> > GetHistoricalScrimMatchBrowserWhereExpression(ScrimMatchReportBrowserSearchFilter searchFilter)
        {
            var isDefaultFilter = searchFilter.IsDefaultFilter;

            Expression <Func <ScrimMatchInfo, bool> > whereExpression = null;

            if (isDefaultFilter)
            {
                Expression <Func <ScrimMatchInfo, bool> > roundExpression = m => m.RoundCount >= searchFilter.MinimumRoundCount;

                var twoHoursAgo = DateTime.UtcNow - TimeSpan.FromHours(2);
                Expression <Func <ScrimMatchInfo, bool> > recentMatchExpression = m => m.StartTime >= twoHoursAgo;

                roundExpression = roundExpression.Or(recentMatchExpression);

                whereExpression = whereExpression == null ? roundExpression : whereExpression.And(roundExpression);
            }
            else
            {
                Expression <Func <ScrimMatchInfo, bool> > roundExpression = m => m.RoundCount >= searchFilter.MinimumRoundCount;

                whereExpression = whereExpression == null ? roundExpression : whereExpression.And(roundExpression);
            }

            if (searchFilter.SearchStartDate != null)
            {
                Expression <Func <ScrimMatchInfo, bool> > startDateExpression = m => m.StartTime >= searchFilter.SearchStartDate;

                whereExpression = whereExpression == null ? startDateExpression : whereExpression.And(startDateExpression);
            }

            if (searchFilter.SearchEndDate != null)
            {
                Expression <Func <ScrimMatchInfo, bool> > endDateExpression = m => m.StartTime <= searchFilter.SearchEndDate;

                whereExpression = whereExpression == null ? endDateExpression : whereExpression.And(endDateExpression);
            }

            if (searchFilter.FacilityId != -1)
            {
                Expression <Func <ScrimMatchInfo, bool> > facilityExpression;

                if (searchFilter.FacilityId == 0)
                {
                    facilityExpression = m => m.FacilityId != null;
                }
                else
                {
                    facilityExpression = m => m.FacilityId == searchFilter.FacilityId;
                }

                whereExpression = whereExpression == null ? facilityExpression : whereExpression.And(facilityExpression);
            }

            if (searchFilter.WorldId != 0)
            {
                Expression <Func <ScrimMatchInfo, bool> > worldExpression = m => m.WorldId == searchFilter.WorldId;

                whereExpression = whereExpression == null ? worldExpression : whereExpression.And(worldExpression);
            }

            if (searchFilter.SearchTermsList.Any() || searchFilter.AliasSearchTermsList.Any())
            {
                Expression <Func <ScrimMatchInfo, bool> > termsExpresion        = null;
                Expression <Func <ScrimMatchInfo, bool> > searchTermsExpression = null;
                Expression <Func <ScrimMatchInfo, bool> > aliasTermsExpression  = null;

                foreach (var term in searchFilter.SearchTermsList)
                {
                    Expression <Func <ScrimMatchInfo, bool> > exp = m => m.Title.Contains(term); // DbFunctionsExtensions.Like(EF.Functions, m.Title, "%" + term + "%");
                    searchTermsExpression = searchTermsExpression == null ? exp : searchTermsExpression.Or(exp);
                }

                termsExpresion = searchTermsExpression;


                foreach (var term in searchFilter.AliasSearchTermsList)
                {
                    Expression <Func <ScrimMatchInfo, bool> > exp = m => m.ScrimMatchId.Contains(term); // DbFunctionsExtensions.Like(EF.Functions, m.ScrimMatchId, "%" + term + "%");
                    aliasTermsExpression = aliasTermsExpression == null ? exp : aliasTermsExpression.And(exp);
                }

                termsExpresion = termsExpresion == null ? aliasTermsExpression : termsExpresion.Or(aliasTermsExpression);

                whereExpression = whereExpression == null ? termsExpresion : whereExpression.And(termsExpresion);
            }

            return(whereExpression);
        }
        public async Task <PaginatedList <ScrimMatchInfo> > GetHistoricalScrimMatchesListAsync(int?pageIndex, ScrimMatchReportBrowserSearchFilter searchFilter, CancellationToken cancellationToken)
        {
            try
            {
                using var factory = _dbContextHelper.GetFactory();
                var dbContext = factory.GetDbContext();

                var scrimMatchesQuery = dbContext.ScrimMatchInfo
                                        .Where(GetHistoricalScrimMatchBrowserWhereExpression(searchFilter))
                                        .AsQueryable();

                var paginatedList = await PaginatedList <ScrimMatchInfo> .CreateAsync(scrimMatchesQuery.AsNoTracking().OrderByDescending(m => m.StartTime), pageIndex ?? 1, _scrimMatchBrowserPageSize, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();

                if (paginatedList == null)
                {
                    return(null);
                }

                foreach (var match in paginatedList.Contents)
                {
                    match.SetTeamAliases();
                }

                return(paginatedList);
            }
            catch (TaskCanceledException)
            {
                _logger.LogInformation($"Task Request cancelled: GetHistoricalScrimMatchesListAsync page {pageIndex}");
                return(null);
            }
            catch (OperationCanceledException)
            {
                _logger.LogInformation($"Request cancelled: GetHistoricalScrimMatchesListAsync page {pageIndex}");
                return(null);
            }
            catch (Exception ex)
            {
                _logger.LogError($"{ex}");

                return(null);
            }
        }