Example #1
0
        public GenericSearchResult <Coupon> SearchCoupons(CouponSearchCriteria criteria)
        {
            var cacheKey = GetCacheKey("ICouponService.SearchCoupons", criteria.GetCacheKey());
            var retVal   = _cacheManager.Get(cacheKey, RegionName, () => _couponService.SearchCoupons(criteria));

            return(retVal);
        }
Example #2
0
        protected virtual IList <SortInfo> BuildSearchExpression(CouponSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            //TODO: Sort by TotalUsesCount
            if (sortInfos.IsNullOrEmpty() || sortInfos.Any(x => x.SortColumn.EqualsInvariant(nameof(Coupon.TotalUsesCount))))
            {
                sortInfos = new[]
                {
                    new SortInfo
                    {
                        SortColumn    = nameof(Coupon.Code),
                        SortDirection = SortDirection.Descending
                    }
                }.ToList();
            }

            if (sortInfos.Count < 2)
            {
                sortInfos.Add(new SortInfo
                {
                    SortColumn    = nameof(Coupon.Id),
                    SortDirection = SortDirection.Ascending
                });
            }

            return(sortInfos);
        }
Example #3
0
        public async Task <GenericSearchResult <Coupon> > SearchCouponsAsync(CouponSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria");
            }

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

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

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

                    if (!string.IsNullOrEmpty(criteria.PromotionId))
                    {
                        query = query.Where(c => c.PromotionId == criteria.PromotionId);
                    }
                    if (!string.IsNullOrEmpty(criteria.Code))
                    {
                        query = query.Where(c => c.Code == criteria.Code);
                    }
                    if (!criteria.Codes.IsNullOrEmpty())
                    {
                        query = query.Where(c => criteria.Codes.Contains(c.Code));
                    }

                    var sortInfos = criteria.SortInfos;
                    //TODO: Sort by TotalUsesCount
                    if (sortInfos.IsNullOrEmpty() || sortInfos.Any(x => x.SortColumn.EqualsInvariant(ReflectionUtility.GetPropertyName <Coupon>(p => p.TotalUsesCount))))
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <Coupon>(x => x.Code), SortDirection = SortDirection.Descending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

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

                    if (criteria.Take > 0)
                    {
                        var ids = await query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        searchResult.Results = await GetByIdsAsync(ids);
                    }

                    return searchResult;
                }
            }));
        }
Example #4
0
        public IHttpActionResult SearchCoupons(CouponSearchCriteria criteria)
        {
            var searchResult = _couponService.SearchCoupons(criteria);

            // actualize coupon totalUsage field
            using (var repository = _repositoryFactory())
            {
                var ids            = searchResult.Results.Select(x => x.Id).ToArray();
                var couponEntities = repository.GetCouponsByIds(ids);
                searchResult.Results.ForEach(coupon => coupon.TotalUsesCount = couponEntities.First(c => c.Id == coupon.Id).TotalUsesCount);
            }
            return(Ok(searchResult));
        }
Example #5
0
        public GenericSearchResult <Coupon> SearchCoupons(CouponSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria");
            }

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

                if (!string.IsNullOrEmpty(criteria.PromotionId))
                {
                    query = query.Where(c => c.PromotionId == criteria.PromotionId);
                }
                if (!string.IsNullOrEmpty(criteria.Code))
                {
                    query = query.Where(c => c.Code == criteria.Code);
                }
                if (!criteria.Codes.IsNullOrEmpty())
                {
                    query = query.Where(c => criteria.Codes.Contains(c.Code));
                }

                var sortInfos = criteria.SortInfos;
                //TODO: Sort by TotalUsesCount
                if (sortInfos.IsNullOrEmpty() || sortInfos.Any(x => x.SortColumn.EqualsInvariant(ReflectionUtility.GetPropertyName <Coupon>(p => p.TotalUsesCount))))
                {
                    sortInfos = new[] { new SortInfo {
                                            SortColumn = ReflectionUtility.GetPropertyName <Coupon>(x => x.Code), SortDirection = SortDirection.Descending
                                        } };
                }
                query = query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id);

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

                var ids = query.Select(x => x.Id)
                          .Skip(criteria.Skip)
                          .Take(criteria.Take)
                          .ToArray();

                searchResult.Results = GetByIds(ids)
                                       .OrderBy(x => Array.IndexOf(ids, x.Id))
                                       .ToList();
                return(searchResult);
            }
        }
Example #6
0
        public async Task <ActionResult <CouponSearchResult> > SearchCoupons([FromBody] CouponSearchCriteria criteria)
        {
            var searchResult = await _couponSearchService.SearchCouponsAsync(criteria);

            // actualize coupon totalUsage field
            using (var repository = _repositoryFactory())
            {
                var ids            = searchResult.Results.Select(x => x.Id).ToArray();
                var couponEntities = await repository.GetCouponsByIdsAsync(ids);

                foreach (var coupon in searchResult.Results)
                {
                    coupon.TotalUsesCount = couponEntities.First(c => c.Id == coupon.Id).TotalUsesCount;
                }
            }
            return(Ok(searchResult));
        }
Example #7
0
        protected virtual IQueryable <CouponEntity> BuildQuery(CouponSearchCriteria criteria, IMarketingRepository repository)
        {
            var query = repository.Coupons;

            if (!string.IsNullOrEmpty(criteria.PromotionId))
            {
                query = query.Where(c => c.PromotionId == criteria.PromotionId);
            }

            if (!string.IsNullOrEmpty(criteria.Code))
            {
                query = query.Where(c => c.Code == criteria.Code);
            }

            if (!criteria.Codes.IsNullOrEmpty())
            {
                query = query.Where(c => criteria.Codes.Contains(c.Code));
            }

            return(query);
        }
Example #8
0
        public async Task <CouponSearchResult> SearchCouponsAsync(CouponSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

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

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

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

                    var totalCount = await query.CountAsync();
                    var searchResult = AbstractTypeFactory <CouponSearchResult> .TryCreateInstance();
                    searchResult.TotalCount = totalCount;

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

                        searchResult.Results = (await _couponService.GetByIdsAsync(ids)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }

                    return searchResult;
                }
            }));
        }
        public IHttpActionResult SearchCoupons(CouponSearchCriteria criteria)
        {
            var searchResult = _couponService.SearchCoupons(criteria);

            return(Ok(searchResult));
        }
 protected virtual GenericSearchResult <Coupon> LoadCoupons(CouponSearchCriteria criteria)
 {
     return(_couponService.SearchCoupons(criteria));
 }