#pragma warning disable S4457 // Parameter validation in "async"/"await" methods should be wrapped
        public virtual async Task <PromotionUsageSearchResult> SearchUsagesAsync(PromotionUsageSearchCriteria criteria)
#pragma warning restore S4457 // Parameter validation in "async"/"await" methods should be wrapped
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

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

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

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

                using (var repository = _repositoryFactory())
                {
                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(repository, criteria);

                    result.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var usages = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                     .Skip(criteria.Skip).Take(criteria.Take)
                                     .ToArrayAsync();
                        result.Results = usages.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToList();
                    }

                    return result;
                }
            }));
        }
        public virtual GenericSearchResult <PromotionUsage> SearchUsages(PromotionUsageSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            using (var repository = _repositoryFactory())
            {
                var query = GetPromotionUsageQuery(repository, criteria);

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

                var searchResult = new GenericSearchResult <PromotionUsage> {
                    TotalCount = query.Count()
                };

                var coupons = query.Skip(criteria.Skip).Take(criteria.Take).ToList();
                searchResult.Results = coupons.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToList();

                return(searchResult);
            }
        }
        public virtual async Task <IEnumerable <string> > GetCustomerOrderCoupons()
        {
            var criteria = new PromotionUsageSearchCriteria
            {
                ObjectId   = Order.Id,
                ObjectType = nameof(CustomerOrder)
            };

            var result = await _promotionUsageSearchService.SearchUsagesAsync(criteria);

            return(result.Results.Select(x => x.CouponCode));
        }
        protected virtual IList <SortInfo> BuildSortExpression(PromotionUsageSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[]
                {
                    new SortInfo
                    {
                        SortColumn    = nameof(PromotionUsage.ModifiedDate),
                        SortDirection = SortDirection.Descending
                    }
                };
            }

            return(sortInfos);
        }
        protected virtual IQueryable <PromotionUsageEntity> GetPromotionUsageQuery(IMarketingRepository repository, PromotionUsageSearchCriteria criteria)
        {
            var query = repository.PromotionUsages;

            if (!string.IsNullOrEmpty(criteria.PromotionId))
            {
                query = query.Where(x => x.PromotionId == criteria.PromotionId);
            }
            if (!string.IsNullOrEmpty(criteria.CouponCode))
            {
                query = query.Where(x => x.CouponCode == criteria.CouponCode);
            }
            if (!string.IsNullOrEmpty(criteria.ObjectId))
            {
                query = query.Where(x => x.ObjectId == criteria.ObjectId);
            }
            if (!string.IsNullOrEmpty(criteria.ObjectType))
            {
                query = query.Where(x => x.ObjectType == criteria.ObjectType);
            }
            if (!string.IsNullOrWhiteSpace(criteria.UserId))
            {
                query = query.Where(x => x.UserId == criteria.UserId);
            }
            if (!string.IsNullOrWhiteSpace(criteria.UserName))
            {
                query = query.Where(x => x.UserName == criteria.UserName);
            }

            return(query);
        }
示例#6
0
        public virtual async Task <GenericSearchResult <PromotionUsage> > SearchUsagesAsync(PromotionUsageSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

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

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

                using (var repository = _repositoryFactory())
                {
                    var query = GetPromotionUsageQuery(repository, criteria);

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

                    var totalCount = await query.CountAsync();
                    var searchResult = new GenericSearchResult <PromotionUsage> {
                        TotalCount = totalCount
                    };

                    if (criteria.Take > 0)
                    {
                        var coupons = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        searchResult.Results = coupons.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToList();
                    }

                    return searchResult;
                }
            }));
        }
 protected virtual GenericSearchResult <PromotionUsage> LoadPromotionUsages(PromotionUsageSearchCriteria criteria)
 {
     return(_usageService.SearchUsages(criteria));
 }