private static void ValidateParameters(DemoProductPartSearchCriteria criteria)
 {
     if (criteria == null)
     {
         throw new ArgumentNullException(nameof(criteria));
     }
 }
        protected virtual IList <SortInfo> BuildSortExpression(DemoProductPartSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[] { new SortInfo {
                                        SortColumn = nameof(DemoProductPart.Name)
                                    } };
            }

            return(sortInfos);
        }
Пример #3
0
        public async Task <ActionResult <DemoProductPartSearchResult> > Search([FromBody] DemoProductPartSearchCriteria criteria)
        {
            var result = await _partsSerarchService.SearchProductPartsAsync(criteria);

            return(Ok(result));
        }
        protected virtual IQueryable <DemoProductPartEntity> BuildQuery(ICatalogRepository catalogRepository, DemoProductPartSearchCriteria criteria)
        {
            var query = ((DemoCatalogRepository)catalogRepository).ConfiguredProductParts;

            if (!string.IsNullOrEmpty(criteria.Keyword))
            {
                query = query.Where(x => x.Name.Contains(criteria.Keyword));
            }

            if (criteria.ConfiguredProductId != null)
            {
                query = query.Where(x => x.ConfiguredProductId == criteria.ConfiguredProductId);
            }

            return(query);
        }
        public virtual async Task <DemoProductPartSearchResult> SearchProductPartsAsync(DemoProductPartSearchCriteria criteria)
        {
            ValidateParameters(criteria);

            var cacheKey = CacheKey.With(GetType(), nameof(SearchProductPartsAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(DemoProductPartSearchCacheRegion.CreateChangeToken());

                var result = AbstractTypeFactory <DemoProductPartSearchResult> .TryCreateInstance();

                using (var catalogRepository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    catalogRepository.DisableChangesTracking();
                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(catalogRepository, criteria);

                    result.TotalCount = await query.CountAsync();

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

                        result.Results = (await _productPartService.GetByIdsAsync(ids)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                }

                return result;
            }));
        }