public async Task <SubscriptionSearchResult> SearchAsync(SubscriptionSearchCriteria searchCriteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchAsync), searchCriteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(SubscriptionSearchCacheRegion.CreateChangeToken());
                var result = new SubscriptionSearchResult();

                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

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

                    result.TotalCount = await query.CountAsync();

                    if (searchCriteria.Take > 0 && result.TotalCount > 0)
                    {
                        var subscriptionIds = query.OrderBySortInfos(sortInfos)
                                              .ThenBy(x => x.Id)
                                              .Select(x => x.Id)
                                              .Skip(searchCriteria.Skip)
                                              .Take(searchCriteria.Take)
                                              .ToArray();

                        result.Results = (await _subscriptionService.GetByIdsAsync(subscriptionIds, searchCriteria.ResponseGroup)).OrderBy(x => Array.IndexOf(subscriptionIds, x.Id)).ToArray();
                    }
                }

                return result;
            }));
        }
Esempio n. 2
0
        public IHttpActionResult SearchSubscriptions(SubscriptionSearchCriteria criteria)
        {
            var result = _subscriptionSearchService.SearchSubscriptions(criteria);
            var retVal = new SubscriptionSearchResult
            {
                Subscriptions = result.Results.ToList(),
                TotalCount    = result.TotalCount
            };

            return(Ok(retVal));
        }
Esempio n. 3
0
        public async Task <IActionResult> SearchSubscriptions([FromBody] SubscriptionSearchCriteria criteria)
        {
            var result = await _subscriptionSearchService.SearchSubscriptionsAsync(criteria);

            var retVal = new SubscriptionSearchResult
            {
                Subscriptions = result.Results.ToList(),
                TotalCount    = result.TotalCount
            };

            return(Ok(retVal));
        }
        public async Task TestDataExportImport()
        {
            // Arrange
            var firstSubscriptionCriteria = new SubscriptionSearchCriteria {
                Take = 0, ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var firstSubscriptionResult = new SubscriptionSearchResult {
                TotalCount = TestSubscriptions.Count
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(firstSubscriptionCriteria))
            .ReturnsAsync(firstSubscriptionResult);

            var secondSubscriptionCriteria = new SubscriptionSearchCriteria {
                Skip = 0, Take = ExpectedBatchSize, ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var secondSubscriptionResult = new SubscriptionSearchResult {
                TotalCount = TestSubscriptions.Count, Results = TestSubscriptions
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(secondSubscriptionCriteria))
            .ReturnsAsync(secondSubscriptionResult);

            var firstPaymentPlanCriteria = new PaymentPlanSearchCriteria {
                Take = 0
            };
            var firstPaymentPlanResult = new PaymentPlanSearchResult {
                TotalCount = TestPaymentPlans.Count
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(firstPaymentPlanCriteria))
            .ReturnsAsync(firstPaymentPlanResult);

            var secondPaymentPlanCriteria = new PaymentPlanSearchCriteria {
                Skip = 0, Take = ExpectedBatchSize
            };
            var secondPaymentPlanResult = new PaymentPlanSearchResult {
                TotalCount = TestPaymentPlans.Count, Results = TestPaymentPlans
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(secondPaymentPlanCriteria))
            .ReturnsAsync(secondPaymentPlanResult);

            Subscription[] actualSubscriptions = null;
            _subscriptionService.Setup(service => service.SaveSubscriptionsAsync(It.IsAny <Subscription[]>()))
            .Callback <Subscription[]>(subscriptions => actualSubscriptions = subscriptions)
            .Returns(Task.CompletedTask);

            PaymentPlan[] actualPaymentPlans = null;
            _paymentPlanService.Setup(service => service.SavePlansAsync(It.IsAny <PaymentPlan[]>()))
            .Callback <PaymentPlan[]>(paymentPlans => actualPaymentPlans = paymentPlans)
            .Returns(Task.CompletedTask);

            // Act
            string actualJson;

            using (var targetStream = new MemoryStream())
            {
                await _subscriptionExportImport.DoExportAsync(targetStream, IgnoreProgressInfo, _cancellationToken.Object);

                // DoExportAsync() closes targetStream, so extracting data from it is a bit tricky...
                var streamContents = targetStream.ToArray();
                using (var copiedStream = new MemoryStream(streamContents))
                    using (var textReader = new StreamReader(copiedStream))
                    {
                        actualJson = await textReader.ReadToEndAsync();
                    }
            }

            var importStream = GetStreamFromString(actualJson);
            await _subscriptionExportImport.DoImportAsync(importStream, IgnoreProgressInfo, _cancellationToken.Object);

            //Assert
            TestSubscriptions.Should().BeEquivalentTo(actualSubscriptions);
            TestPaymentPlans.Should().BeEquivalentTo(actualPaymentPlans);
        }
        public async Task TestDataExport()
        {
            // Arrange
            var firstSubscriptionCriteria = new SubscriptionSearchCriteria
            {
                Take          = 0,
                ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var firstSubscriptionResult = new SubscriptionSearchResult
            {
                TotalCount = TestSubscriptions.Count
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(firstSubscriptionCriteria))
            .ReturnsAsync(firstSubscriptionResult);

            var secondSubscriptionCriteria = new SubscriptionSearchCriteria
            {
                Skip          = 0,
                Take          = ExpectedBatchSize,
                ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var secondSubscriptionResult = new SubscriptionSearchResult
            {
                TotalCount = TestSubscriptions.Count,
                Results    = TestSubscriptions
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(secondSubscriptionCriteria))
            .ReturnsAsync(secondSubscriptionResult);

            var firstPaymentPlanCriteria = new PaymentPlanSearchCriteria
            {
                Take = 0,
            };

            var firstPaymentPlanResult = new PaymentPlanSearchResult
            {
                TotalCount = TestPaymentPlans.Count
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(firstPaymentPlanCriteria))
            .ReturnsAsync(firstPaymentPlanResult);

            var secondPaymentPlanCriteria = new PaymentPlanSearchCriteria
            {
                Skip = 0,
                Take = ExpectedBatchSize
            };

            var secondPaymentPlanResult = new PaymentPlanSearchResult
            {
                TotalCount = TestPaymentPlans.Count,
                Results    = TestPaymentPlans
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(secondPaymentPlanCriteria))
            .ReturnsAsync(secondPaymentPlanResult);

            string expectedJson;

            using (var resourceStream = ReadEmbeddedResource("Resources.SerializedSubscriptionData.json"))
                using (var textReader = new StreamReader(resourceStream))
                {
                    expectedJson = await textReader.ReadToEndAsync();
                }

            // Act
            string actualJson;

            using (var targetStream = new MemoryStream())
            {
                await _subscriptionExportImport.DoExportAsync(targetStream, IgnoreProgressInfo, _cancellationToken.Object);

                // DoExportAsync() closes targetStream, so extracting data from it is a bit tricky...
                var streamContents = targetStream.ToArray();
                using (var copiedStream = new MemoryStream(streamContents))
                    using (var textReader = new StreamReader(copiedStream))
                    {
                        actualJson = await textReader.ReadToEndAsync();
                    }
            }

            // Assert
            // NOTE: whitespace characters and indentation may vary, so comparing JSON as text will not work well.
            //       To overcome it, we compare JSON tokens here.
            var expectedJObject = JsonConvert.DeserializeObject <JObject>(expectedJson);
            var actualJObject   = JsonConvert.DeserializeObject <JObject>(actualJson);

            Assert.True(JToken.DeepEquals(expectedJObject, actualJObject));
        }