Example #1
0
        public virtual async Task <IShoppingCartBuilder> GetOrCreateCartAsync(string storeId, string customerId, string cartName, string currency, string cultureName)
        {
            var criteria = new ShoppingCartSearchCriteria
            {
                CustomerId = customerId,
                StoreId    = storeId,
                Name       = cartName,
                Currency   = currency
            };

            var searchResult = await _shoppingCartSearchService.SearchCartAsync(criteria);

            Cart = searchResult.Results.FirstOrDefault();

            if (Cart == null)
            {
                //TODO
                //var customerContact = _memberService.GetByIds(new[] { customerId }).OfType<Contact>().FirstOrDefault();
                Cart = AbstractTypeFactory <ShoppingCart> .TryCreateInstance();

                Cart.Name         = cartName;
                Cart.LanguageCode = cultureName;
                Cart.Currency     = currency;
                Cart.CustomerId   = customerId;
                //Cart.CustomerName = customerContact != null ? customerContact.FullName : "Anonymous";
                //Cart.IsAnonymous = customerContact == null;
                Cart.StoreId = storeId;

                await _shoppingCartService.SaveChangesAsync(new[] { Cart });

                Cart = await _shoppingCartService.GetByIdAsync(Cart.Id);
            }

            return(this);
        }
Example #2
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;
                }
            }));
        }
Example #3
0
        public async Task <CartAggregate> GetCartAsync(string cartName, string storeId, string userId, string language, string currencyCode, string type = null, string responseGroup = null)
        {
            var criteria = new ShoppingCartSearchCriteria
            {
                StoreId = storeId,
                // IMPORTANT! Need to specify customerId, otherwise any user cart could be returned while we expect anonymous in this case.
                CustomerId    = userId ?? AnonymousUser.UserName,
                Name          = cartName,
                Currency      = currencyCode,
                Type          = type,
                ResponseGroup = EnumUtility.SafeParseFlags(responseGroup, CartResponseGroup.Full).ToString()
            };

            var cartSearchResult = await _shoppingCartSearchService.SearchAsync(criteria);

            //The null value for the Type parameter should be interpreted as a valuable parameter, and we must return a cart object with Type property that has null exactly set.
            //otherwise, for the case where the system contains carts with different Types, the resulting cart may be a random result.
            var cart = cartSearchResult.Results.FirstOrDefault(x => (type != null) || x.Type == null);

            if (cart != null)
            {
                return(await InnerGetCartAggregateFromCartAsync(cart.Clone() as ShoppingCart, language));
            }

            return(null);
        }
        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 GenericSearchResult <ShoppingCart> Search(ShoppingCartSearchCriteria criteria)
        {
            var retVal = new GenericSearchResult <ShoppingCart>();

            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);
                }

                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 = query.Count();

                var cartIds = query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArray();
                var carts   = GetByIds(cartIds);
                retVal.Results = carts.AsQueryable().OrderBySortInfos(sortInfos).ToList();

                return(retVal);
            }
        }
        public IHttpActionResult Search(ShoppingCartSearchCriteria criteria)
        {
            var result = _searchService.Search(criteria);
            var retVal = new ShoppingCartSearchResult
            {
                Results    = result.Results.ToList(),
                TotalCount = result.TotalCount
            };

            return(Ok(retVal));
        }
        protected virtual SortInfo[] GetSortInfos(ShoppingCartSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[] { new SortInfo {
                                        SortColumn = ReflectionUtility.GetPropertyName <ShoppingCartEntity>(x => x.CreatedDate), SortDirection = SortDirection.Descending
                                    } };
            }
            return(sortInfos);
        }
        public async Task <SearchCartResponse> SearchCartAsync(ShoppingCartSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            var searchResult = await _shoppingCartSearchService.SearchCartAsync(criteria);

            var cartAggregates = await GetCartsForShoppingCartsAsync(searchResult.Results);

            return(new SearchCartResponse()
            {
                Results = cartAggregates, TotalCount = searchResult.TotalCount
            });
        }
        public GenericSearchResult <ShoppingCart> Search(ShoppingCartSearchCriteria criteria)
        {
            var retVal = new GenericSearchResult <ShoppingCart>();

            using (var repository = RepositoryFactory())
            {
                var query = GetQuery(repository, criteria);

                query = SortQuery(query, criteria);

                retVal.TotalCount = query.Count();

                var cartIds = query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArray();
                var carts   = GetByIds(cartIds);
                retVal.Results = carts.AsQueryable().OrderBySortInfos(GetSortInfos(criteria)).ToList();

                return(retVal);
            }
        }
        public async Task <CartAggregate> GetCartAsync(string cartName, string storeId, string userId, string language, string currencyCode, string type = null, string responseGroup = null)
        {
            var criteria = new ShoppingCartSearchCriteria
            {
                StoreId = storeId,
                // IMPORTANT! Need to specify customerId, otherwise any user cart could be returned while we expect anonymous in this case.
                CustomerId    = userId ?? AnonymousUser.UserName,
                Name          = cartName,
                Currency      = currencyCode,
                Type          = type,
                ResponseGroup = EnumUtility.SafeParseFlags(responseGroup, CartResponseGroup.Full).ToString()
            };

            var cartSearchResult = await _shoppingCartSearchService.SearchCartAsync(criteria);

            var cart = cartSearchResult.Results.FirstOrDefault();

            if (cart != null)
            {
                return(await InnerGetCartAggregateFromCartAsync(cart.Clone() as ShoppingCart, language));
            }

            return(null);
        }
Example #11
0
        protected virtual IQueryable <ShoppingCartEntity> GetQuery(ICartRepository repository, ShoppingCartSearchCriteria criteria, IEnumerable <SortInfo> sortInfos)
        {
            var query = GetQueryableShoppingCarts(repository);

            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));
            }

            query.OrderBySortInfos(sortInfos);

            return(query);
        }
Example #12
0
 public CartSearchCriteriaBuilder()
 {
     _searchCriteria = AbstractTypeFactory <ShoppingCartSearchCriteria> .TryCreateInstance();
 }
        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;
                }
            }));
        }
        public async Task <ActionResult <GenericSearchResult <ShoppingCart> > > Search(ShoppingCartSearchCriteria criteria)
        {
            var result = await _searchService.SearchCartAsync(criteria);

            return(Ok(result));
        }
 protected virtual IQueryable <ShoppingCartEntity> SortQuery(IQueryable <ShoppingCartEntity> query, ShoppingCartSearchCriteria criteria)
 {
     return(query.OrderBySortInfos(GetSortInfos(criteria)));
 }
        protected virtual IQueryable <ShoppingCartEntity> BuildQuery(ICartRepository repository, ShoppingCartSearchCriteria criteria)
        {
            var query = repository.ShoppingCarts.Where(x => x.IsDeleted == false);

            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));
            }

            if (criteria.CreatedStartDate != null)
            {
                query = query.Where(x => x.CreatedDate >= criteria.CreatedStartDate.Value);
            }

            if (criteria.CreatedEndDate != null)
            {
                query = query.Where(x => x.CreatedDate <= criteria.CreatedEndDate.Value);
            }

            if (criteria.ModifiedStartDate != null)
            {
                query = query.Where(x => x.ModifiedDate >= criteria.ModifiedStartDate.Value);
            }

            if (criteria.ModifiedEndDate != null)
            {
                query = query.Where(x => x.ModifiedDate <= criteria.ModifiedEndDate.Value);
            }

            return(query);
        }
        protected virtual IQueryable <ShoppingCartEntity> GetQuery(ICartRepository repository, ShoppingCartSearchCriteria criteria)
        {
            var query = GetQueryable(repository).Where(x => x.IsDeleted == false);

            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));
            }

            return(query);
        }