Exemple #1
0
        public virtual async Task SavePricesAsync(Price[] prices)
        {
            var pkMap = new PrimaryKeyResolvingMap();

            using (var repository = _repositoryFactory())
            {
                var pricesIds = prices.Select(x => x.Id).Where(x => x != null).Distinct().ToArray();
                var alreadyExistPricesEntities = await repository.GetPricesByIdsAsync(pricesIds);

                //Create default priceLists for prices without pricelist
                foreach (var priceWithoutPricelistGroup in prices.Where(x => x.PricelistId == null).GroupBy(x => x.Currency))
                {
                    var defaultPriceListId = GetDefaultPriceListName(priceWithoutPricelistGroup.Key);
                    var pricelists         = await GetPricelistsByIdAsync(new[] { defaultPriceListId });

                    if (pricelists.IsNullOrEmpty())
                    {
                        var defaultPriceList = AbstractTypeFactory <Pricelist> .TryCreateInstance();

                        defaultPriceList.Id          = defaultPriceListId;
                        defaultPriceList.Currency    = priceWithoutPricelistGroup.Key;
                        defaultPriceList.Name        = defaultPriceListId;
                        defaultPriceList.Description = defaultPriceListId;
                        repository.Add(AbstractTypeFactory <PricelistEntity> .TryCreateInstance().FromModel(defaultPriceList, pkMap));
                    }
                    foreach (var priceWithoutPricelist in priceWithoutPricelistGroup)
                    {
                        priceWithoutPricelist.PricelistId = defaultPriceListId;
                    }
                }

                foreach (var price in prices)
                {
                    var sourceEntity = AbstractTypeFactory <PriceEntity> .TryCreateInstance().FromModel(price, pkMap);

                    var targetEntity = alreadyExistPricesEntities.FirstOrDefault(x => x.Id == price.Id);
                    if (targetEntity != null)
                    {
                        sourceEntity.Patch(targetEntity);
                    }
                    else
                    {
                        repository.Add(sourceEntity);
                    }
                }

                await repository.UnitOfWork.CommitAsync();

                pkMap.ResolvePrimaryKeys();

                foreach (var price in prices)
                {
                    PricesCacheRegion.ExpirePrice(price.Id);
                }
                ResetCache();
            }
        }
Exemple #2
0
        public virtual async Task DeletePricesAsync(string[] ids)
        {
            using (var repository = _repositoryFactory())
            {
                await repository.DeletePricesAsync(ids);

                await repository.UnitOfWork.CommitAsync();

                foreach (var id in ids)
                {
                    PricesCacheRegion.ExpirePrice(id);
                }
                ResetCache();
            }
        }
        public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey());

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

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

                using (var repository = _repositoryFactory())
                {
                    var query = await BuildQueryAsync(repository, criteria);
                    var sortInfos = BuildSortExpression(criteria);
                    //Try to replace sorting columns names
                    TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);

                    if (criteria.GroupByProducts)
                    {
                        var groupedQuery = query.Select(x => x.ProductId).OrderBy(x => x).Distinct();
                        result.TotalCount = await groupedQuery.CountAsync();

                        if (criteria.Take > 0)
                        {
                            query = query.Where(x => groupedQuery.Contains(x.ProductId));
                        }
                    }
                    else
                    {
                        result.TotalCount = await query.CountAsync();
                    }

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

                        var unorderedResults = await _pricingService.GetPricesByIdAsync(priceIds);
                        result.Results = unorderedResults.OrderBy(x => Array.IndexOf(priceIds, x.Id)).ToList();
                    }
                }
                return result;
            }));
        }
        public virtual async Task DeletePricesAsync(string[] ids)
        {
            using (var repository = _repositoryFactory())
            {
                var prices = await GetPricesByIdAsync(ids);

                var changedEntries = prices.Select(x => new GenericChangedEntry <Price>(x, EntryState.Deleted));
                await repository.DeletePricesAsync(ids);

                await repository.UnitOfWork.CommitAsync();

                foreach (var id in ids)
                {
                    PricesCacheRegion.ExpirePrice(id);
                }
                ResetCache();

                await _eventPublisher.Publish(new PriceChangedEvent(changedEntries));
            }
        }
        public virtual async Task <Price[]> GetPricesByIdAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(GetPricesByIdAsync), string.Join("-", ids));
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                Price[] result = null;
                if (ids != null)
                {
                    using (var repository = _repositoryFactory())
                    {
                        cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken(ids));

                        result = (await repository.GetPricesByIdsAsync(ids)).Select(x => x.ToModel(AbstractTypeFactory <Price> .TryCreateInstance())).ToArray();
                    }
                }

                return(result);
            });

            return(result.Select(x => x.Clone() as Price).ToArray());
        }
Exemple #6
0
        public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey());

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

                var retVal = AbstractTypeFactory <PriceSearchResult> .TryCreateInstance();

                using (var repository = _repositoryFactory())
                {
                    var query = repository.Prices;

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

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

                    if (criteria.ModifiedSince.HasValue)
                    {
                        query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince);
                    }

                    var sortInfos = criteria.SortInfos.ToArray();
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <Price>(x => x.List)
                                            } };
                    }
                    //Try to replace sorting columns names
                    TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);


                    query = query.OrderBySortInfos(sortInfos);

                    // TODO: add checks for criteria.Take being greater than 0
                    if (criteria.GroupByProducts)
                    {
                        var groupedQuery = query.GroupBy(x => x.ProductId).OrderBy(x => 1);
                        retVal.TotalCount = await groupedQuery.CountAsync();
                        query = groupedQuery.Skip(criteria.Skip).Take(criteria.Take).SelectMany(x => x);
                    }
                    else
                    {
                        retVal.TotalCount = await query.CountAsync();
                        query = query.Skip(criteria.Skip).Take(criteria.Take);
                    }

                    if (criteria.Take > 0)
                    {
                        var pricesIds = await query.Select(x => x.Id).ToListAsync();
                        retVal.Results = (await _pricingService.GetPricesByIdAsync(pricesIds.ToArray()))
                                         .OrderBy(x => pricesIds.IndexOf(x.Id))
                                         .ToList();
                    }
                }
                return retVal;
            }));
        }