Example #1
0
        public async Task <ShoppingCartSearchResult> SearchCartAsync(ShoppingCartSearchCriteria criteria)
        {
            var retVal = AbstractTypeFactory <ShoppingCartSearchResult> .TryCreateInstance();

            var cacheKey = CacheKey.With(GetType(), "SearchCartAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(CartSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    var sortInfos = GetSortInfos(criteria);
                    var query = GetQuery(repository, criteria, sortInfos);

                    retVal.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        var cartIds = await query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        retVal.Results = (await _cartService.GetByIdsAsync(cartIds, criteria.ResponseGroup)).AsQueryable().OrderBySortInfos(sortInfos).ToArray();
                    }

                    return retVal;
                }
            }));
        }
        public async Task <ShoppingCartSearchResult> SearchCartAsync(ShoppingCartSearchCriteria criteria)
        {
            var result = AbstractTypeFactory <ShoppingCartSearchResult> .TryCreateInstance();

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

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(CartSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    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 _cartService.GetByIdsAsync(ids, criteria.ResponseGroup)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }

                    return result;
                }
            }));
        }
        public async Task <GenericSearchResult <ShoppingCart> > SearchCartAsync(ShoppingCartSearchCriteria criteria)
        {
            var retVal   = new GenericSearchResult <ShoppingCart>();
            var cacheKey = CacheKey.With(GetType(), "SearchCartAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CartSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    var query = repository.ShoppingCarts;

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

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

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

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

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

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

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

                    if (!criteria.CustomerIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => criteria.CustomerIds.Contains(x.CustomerId));
                    }

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[]
                        {
                            new SortInfo
                            {
                                SortColumn = ReflectionUtility.GetPropertyName <ShoppingCartEntity>(x => x.CreatedDate),
                                SortDirection = SortDirection.Descending
                            }
                        };
                    }

                    query = query.OrderBySortInfos(sortInfos);

                    retVal.TotalCount = await query.CountAsync();

                    var carts = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                    retVal.Results = carts.Select(x => x.ToModel(AbstractTypeFactory <ShoppingCart> .TryCreateInstance())).ToList();

                    return retVal;
                }
            }));
        }