public virtual async Task <Subscription[]> GetByIdsAsync(string[] subscriptionIds, string responseGroup = null) { var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", subscriptionIds), responseGroup); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry => { var retVal = new List <Subscription>(); var subscriptionResponseGroup = EnumUtility.SafeParseFlags(responseGroup, SubscriptionResponseGroup.Full); using (var repository = _subscriptionRepositoryFactory()) { repository.DisableChangesTracking(); var subscriptionEntities = await repository.GetSubscriptionsByIdsAsync(subscriptionIds, responseGroup); foreach (var subscriptionEntity in subscriptionEntities) { var subscription = AbstractTypeFactory <Subscription> .TryCreateInstance(); if (subscription != null) { subscription = subscriptionEntity.ToModel(subscription); retVal.Add(subscription); } } } CustomerOrder[] orderPrototypes = null; CustomerOrder[] subscriptionOrders = null; if (subscriptionResponseGroup.HasFlag(SubscriptionResponseGroup.WithOrderPrototype)) { orderPrototypes = await _customerOrderService.GetByIdsAsync(retVal.Select(x => x.CustomerOrderPrototypeId).ToArray()); } if (subscriptionResponseGroup.HasFlag(SubscriptionResponseGroup.WithRelatedOrders)) { //Loads customer order prototypes and related orders for each subscription via order service var criteria = new CustomerOrderSearchCriteria { SubscriptionIds = subscriptionIds }; subscriptionOrders = (await _customerOrderSearchService.SearchCustomerOrdersAsync(criteria)).Results.ToArray(); } foreach (var subscription in retVal) { if (!orderPrototypes.IsNullOrEmpty()) { subscription.CustomerOrderPrototype = orderPrototypes.FirstOrDefault(x => x.Id == subscription.CustomerOrderPrototypeId); } if (!subscriptionOrders.IsNullOrEmpty()) { subscription.CustomerOrders = subscriptionOrders.Where(x => x.SubscriptionId == subscription.Id).ToList(); subscription.CustomerOrdersIds = subscription.CustomerOrders.Select(x => x.Id).ToArray(); } cacheEntry.AddExpirationToken(SubscriptionCacheRegion.CreateChangeToken(subscription)); } return retVal.ToArray(); })); }
public async Task <SubscriptionInfo[]> GetByIdsAsync(string[] ids, string responseGroup = null) { var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", ids)); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(SubscriptionCacheRegion.CreateChangeToken()); var result = new List <SubscriptionInfo>(); if (!ids.IsNullOrEmpty()) { using (var repository = _subscriptionRepositoryFactory()) { var entities = await repository.GetSubscriptionsByIdsAsync(ids); if (!entities.IsNullOrEmpty()) { result.AddRange(entities.Select(x => x.ToModel(AbstractTypeFactory <SubscriptionInfo> .TryCreateInstance()))); } } } return result.ToArray(); })); }