public async Task <ActionResult <Catalog[]> > GetCatalogs(string sort = null, int skip = 0, int take = 20)
        {
            var criteria = AbstractTypeFactory <CatalogSearchCriteria> .TryCreateInstance();

            criteria.Sort = sort;
            criteria.Skip = skip;
            criteria.Take = take;

            var authorizationResult = await _authorizationService.AuthorizeAsync(User, criteria, new CatalogAuthorizationRequirement(ModuleConstants.Security.Permissions.Read));

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }

            var result = await _catalogSearchService.SearchCatalogsAsync(criteria);

            return(Ok(result.Results));
        }
        protected override ExportableSearchResult FetchData(CatalogSearchCriteria searchCriteria)
        {
            var searchResult = _catalogSearchService.SearchCatalogsAsync(searchCriteria).GetAwaiter().GetResult();

            return(new ExportableSearchResult
            {
                TotalCount = searchResult.TotalCount,
                Results = searchResult.Results.ToList <IExportable>(),
            });
        }
        protected virtual async Task LoadDependenciesAsync(Property[] properties)
        {
            var catalogsByIdDict = ((await _catalogSearchService.SearchCatalogsAsync(new Core.Model.Search.CatalogSearchCriteria {
                Take = int.MaxValue
            })).Results).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase)
                                   .WithDefaultValue(null);

            foreach (var property in properties)
            {
                property.Catalog = catalogsByIdDict[property.CatalogId];
            }
        }
        public async Task DoExportAsync(Stream outStream, ExportImportOptions options, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);

            using (var sw = new StreamWriter(outStream))
                using (var writer = new JsonTextWriter(sw))
                {
                    await writer.WriteStartObjectAsync();

                    #region Export catalogs
                    progressInfo.Description = "Catalogs exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Catalogs");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <Catalog>) await _catalogSearchService.SearchCatalogsAsync(new CatalogSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } catalogs have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export categories
                    progressInfo.Description = "Categories exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Categories");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _categorySearchService.SearchCategoriesAsync(new CategorySearchCriteria {
                            Skip = skip, Take = take
                        });
                        LoadImages(searchResult.Results.OfType <IHasImages>().ToArray(), progressInfo, options.HandleBinaryData);
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }

                        return((GenericSearchResult <Category>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Categories have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export properties
                    progressInfo.Description = "Properties exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Properties");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _propertySearchService.SearchPropertiesAsync(new PropertySearchCriteria {
                            Skip = skip, Take = take
                        });
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }
                        return((GenericSearchResult <Property>)searchResult);
                    }
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } properties have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export propertyDictionaryItems
                    progressInfo.Description = "PropertyDictionaryItems exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("PropertyDictionaryItems");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <PropertyDictionaryItem>) await _propertyDictionarySearchService.SearchAsync(new PropertyDictionaryItemSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } property dictionary items have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export products
                    progressInfo.Description = "Products exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Products");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _productSearchService.SearchProductsAsync(new ProductSearchCriteria {
                            Skip = skip, Take = take, ResponseGroup = ItemResponseGroup.Full.ToString()
                        });
                        LoadImages(searchResult.Results.OfType <IHasImages>().ToArray(), progressInfo, options.HandleBinaryData);
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }
                        return((GenericSearchResult <CatalogProduct>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Products have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    await writer.WriteEndObjectAsync();

                    await writer.FlushAsync();
                }
        }