Example #1
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

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

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

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

                    retVal.TotalCount = await query.CountAsync();

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

                        //Load plans with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.OrderBy(x => Array.IndexOf(paymentPlanIds, x.Id)).ToArray();
                    }

                    return retVal;
                }
            }));
        }
Example #2
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

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

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = repository.PaymentPlans;

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

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var paymentPlanEntities = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        var paymentPlanIds = paymentPlanEntities.Select(x => x.Id).ToArray();

                        //Load subscriptions with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.AsQueryable().OrderBySortInfos(sortInfos).ToArray();
                    }

                    return retVal;
                }
            }));
        }