Exemplo n.º 1
0
        /// <summary>
        /// Search members in database by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        protected virtual async Task <MemberSearchResult> RegularSearchMembersAsync(MembersSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchMembersAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CustomerSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();
                    var result = AbstractTypeFactory <MemberSearchResult> .TryCreateInstance();

                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(repository, criteria);

                    result.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        var ids = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                  .Select(x => x.Id)
                                  .Skip(criteria.Skip).Take(criteria.Take)
                                  .ToArrayAsync();

                        result.Results = (await _memberService.GetByIdsAsync(ids, criteria.ResponseGroup)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                    return result;
                }
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Search members in database by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public virtual async Task <MemberSearchResult> SearchMembersAsync(MembersSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchMembersAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CustomerSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();
                    var result = AbstractTypeFactory <MemberSearchResult> .TryCreateInstance();

                    var query = LinqKit.Extensions.AsExpandable(repository.Members);

                    if (!criteria.MemberTypes.IsNullOrEmpty())
                    {
                        query = query.Where(m => criteria.MemberTypes.Contains(m.MemberType));
                    }

                    if (!criteria.Groups.IsNullOrEmpty())
                    {
                        query = query.Where(m => m.Groups.Any(g => criteria.Groups.Contains(g.Group)));
                    }

                    if (criteria.MemberId != null)
                    {
                        //TODO: DeepSearch in specified member
                        query = query.Where(m => m.MemberRelations.Any(r => r.AncestorId == criteria.MemberId));
                    }
                    else if (!criteria.DeepSearch)
                    {
                        query = query.Where(m => !m.MemberRelations.Any());
                    }

                    //Get extra predicates (where clause)
                    var predicate = GetQueryPredicate(criteria);
                    query = query.Where(LinqKit.Extensions.Expand(predicate));

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] {
                            new SortInfo {
                                SortColumn = ReflectionUtility.GetPropertyName <Member>(m => m.MemberType), SortDirection = SortDirection.Descending
                            },
                            new SortInfo {
                                SortColumn = ReflectionUtility.GetPropertyName <Member>(m => m.Name), SortDirection = SortDirection.Ascending
                            },
                        };
                    }

                    query = query.OrderBySortInfos(sortInfos);

                    result.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        var memberIds = await query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        result.Results = (await _memberService.GetByIdsAsync(memberIds, criteria.ResponseGroup)).AsQueryable().OrderBySortInfos(sortInfos).ToList();
                    }
                    return result;
                }
            }));
        }