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;
            }));
        }
示例#2
0
        public async Task <ActionResult <DemoProductPart> > GetProductPartById(string id)
        {
            var result = (await _partsService.GetByIdsAsync(new[] { id })).FirstOrDefault();

            return(Ok(result));
        }