private IObservable <Unit> SyncCategories(string beginTime, ISourceCache <SSBakery.Models.CatalogCategory, string> categoryCache)
        {
            var catalogApi  = new CatalogApi();
            var objectTypes = new List <SearchCatalogObjectsRequest.ObjectTypesEnum> {
                SearchCatalogObjectsRequest.ObjectTypesEnum.CATEGORY
            };
            //var beginTime = "2018-08-02T15:00:00Z";
            var request = new SearchCatalogObjectsRequest(ObjectTypes: objectTypes, IncludeDeletedObjects: true, BeginTime: beginTime);

            IObservable <CatalogObject> resultStream = catalogApi
                                                       .SearchCatalogObjectsAsync(request)
                                                       .ToObservable()
                                                       .Where(x => x.Objects != null)
                                                       .SelectMany(x => x.Objects);

            IObservable <Unit> deletedCategories = resultStream
                                                   .Where(x => x.IsDeleted.Value && categoryCache.Lookup(x.Id).HasValue)
                                                   .SelectMany(x => _categoryRepo.Delete(x.Id));

            IObservable <Unit> addedOrModifiedCategories = resultStream
                                                           .Where(x => !x.IsDeleted.Value)
                                                           .Select(x => UpdateCategoryCache(x, categoryCache))
                                                           .SelectMany(category => _categoryRepo.Upsert(category));

            return(Observable.Merge(deletedCategories, addedOrModifiedCategories));
        }
        private IObservable <Unit> SyncItems(string beginTime)
        {
            var catalogApi  = new CatalogApi();
            var objectTypes = new List <SearchCatalogObjectsRequest.ObjectTypesEnum> {
                SearchCatalogObjectsRequest.ObjectTypesEnum.ITEM
            };
            var request = new SearchCatalogObjectsRequest(ObjectTypes: objectTypes, BeginTime: beginTime);

            IObservable <CatalogObject> resultStream = catalogApi
                                                       .SearchCatalogObjectsAsync(request)
                                                       .ToObservable()
                                                       .Where(x => x.Objects != null)
                                                       .SelectMany(x => x.Objects);

            IObservable <Unit> deletedItems = resultStream
                                              .Where(x => x.IsDeleted.Value && x.ItemData.CategoryId != null)
                                              .GroupBy(x => x.ItemData.CategoryId)
                                              .SelectMany(
                group =>
            {
                return(group
                       .Select(catalogObject => catalogObject.Id)
                       .SelectMany(itemId => _itemRepoFactory.Create(group.Key).Delete(itemId)));
            });

            IObservable <Unit> addedOrModifiedItems = resultStream
                                                      .Where(x => !x.IsDeleted.Value && x.ItemData.CategoryId != null)
                                                      .Select(MapDtoToItem)
                                                      .GroupBy(x => x.CategoryId)
                                                      .SelectMany(
                x =>
            {
                return(x
                       .SelectMany(item => _itemRepoFactory.Create(x.Key).Upsert(item)));
            });

            return(Observable.Merge(deletedItems, addedOrModifiedItems));
        }