Esempio n. 1
0
        private async Task RecordUsages(string objectId, IEnumerable <PromotionUsage> oldUsages, IEnumerable <PromotionUsage> newUsages)
        {
            var toAddUsages    = newUsages.Except(oldUsages, EqualityComparer);
            var toRemoveUsages = oldUsages.Except(newUsages, EqualityComparer);

            if (!toAddUsages.IsNullOrEmpty())
            {
                await _usageService.SaveUsagesAsync(toAddUsages.ToArray());
            }
            if (!toRemoveUsages.IsNullOrEmpty())
            {
                var alreadyExistUsages = (await _promotionUsageSearchService.SearchUsagesAsync(new PromotionUsageSearchCriteria {
                    ObjectId = objectId
                })).Results;
                await _usageService.DeleteUsagesAsync(alreadyExistUsages.Intersect(toRemoveUsages, EqualityComparer).Select(x => x.Id).ToArray());
            }
        }
        public async Task DoImportAsync(Stream inputStream, Action <ExportImportProgressInfo> progressCallback,
                                        ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var progressInfo = new ExportImportProgressInfo();

            using (var streamReader = new StreamReader(inputStream))
                using (var reader = new JsonTextReader(streamReader))
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            if (reader.Value.ToString() == "Promotions")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <Promotion>(_jsonSerializer, _batchSize, items => _promotionService.SavePromotionsAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } promotions have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "DynamicContentFolders")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <DynamicContentFolder>(_jsonSerializer, _batchSize, items => _dynamicContentService.SaveFoldersAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } dynamic content items have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "DynamicContentItems")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <DynamicContentItem>(_jsonSerializer, _batchSize, items => _dynamicContentService.SaveContentItemsAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } dynamic content items have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "DynamicContentPlaces")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <DynamicContentPlace>(_jsonSerializer, _batchSize, items => _dynamicContentService.SavePlacesAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } dynamic content places have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "DynamicContentPublications")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <DynamicContentPublication>(_jsonSerializer, _batchSize, items => _dynamicContentService.SavePublicationsAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } dynamic content publications have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "Coupons")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <Coupon>(_jsonSerializer, _batchSize, items => _couponService.SaveCouponsAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } coupons have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                            else if (reader.Value.ToString() == "Usages")
                            {
                                await reader.DeserializeJsonArrayWithPagingAsync <PromotionUsage>(_jsonSerializer, _batchSize, items => _promotionUsageService.SaveUsagesAsync(items.ToArray()), processedCount =>
                                {
                                    progressInfo.Description = $"{ processedCount } usages have been imported";
                                    progressCallback(progressInfo);
                                }, cancellationToken);
                            }
                        }
                    }
                }
        }