示例#1
0
        public OperationResponse <CategoriesUpdateCommandOutput> Execute(SyncCommandEntityWrapperInput <CategoriesUpdateCommandInput> input)
        {
            var result = new OperationResponse <CategoriesUpdateCommandOutput>();

            using (var dbContextScope = this.DbContextScopeFactory.Create())
            {
                OperationResponse <DomainModel.Category> getByFunzaIdResult;
                OperationResponse    prepareToSaveResult;
                DomainModel.Category entity = null;

                try
                {
                    foreach (var dtoItem in input.SyncItems)
                    {
                        getByFunzaIdResult = this.Repository.GetByFunzaId(dtoItem.FunzaId);
                        bool addEntity = false;
                        if (!getByFunzaIdResult.IsSucceed)
                        {
                            addEntity = true;
                        }
                        else if (getByFunzaIdResult.Bag == null)
                        {
                            addEntity = true;
                        }

                        entity = getByFunzaIdResult.Bag;
                        entity = this.MapDTO(dtoItem, input.IntegrationId, entity);

                        if (addEntity)
                        {
                            prepareToSaveResult = this.Repository.Add(entity);
                            result.AddResponse(prepareToSaveResult);
                        }
                    }

                    if (result.IsSucceed)
                    {
                        dbContextScope.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    result.AddError("Error Sync Funza Product Categories", ex);
                }
            }

            using (var dbContextScope = this.DbContextScopeFactory.Create())
            {
                result.AddResponse(this.Repository.DeleteNotInIntegration(input.IntegrationId));
            }

            return(result);
        }
        public async Task <OperationResponse> ExecuteAsync()
        {
            var taskDictionary = new Dictionary <string, Task <Object> >();
            var result         = new OperationResponse();

            var integrationId = Guid.NewGuid();

            //taskDictionary.Add(
            //    "Products"
            //    , Task.Run<object>(async () =>
            //    {
            //        var combination = new List<FunzaGetProductsCommandOutput>();
            //        var filter = new PageQuery<FunzaGetProductsCommandInput>();
            //        var source = await this.GetProductsCommand.ExecuteAsync(filter);
            //        combination.AddRange(source.Bag);

            //        var mapping = this.Map(combination, integrationId: integrationId);
            //        var wrapper = new SyncCommandEntityWrapperInput<ProductsUpdateCommandInput>
            //        {
            //            IntegrationId = integrationId,
            //            SyncItems = mapping
            //        };
            //        return this.ProductsUpdateCommand.Execute(wrapper);
            //    })
            //);

            //taskDictionary.Add(
            //    "Colors"
            //    , Task.Run<object>(async () =>
            //    {
            //        var combination = new List<DirectGetProductColorsResult>();
            //        var filter = new PageQuery<FunzaGetColorsCommandInput>();
            //        var source = await this.GetColorsCommand.ExecuteAsync(filter);
            //        combination.AddRange(source.Bag);

            //        var mapping = this.Map(combination, integrationId: integrationId);
            //        var wrapper = new SyncCommandEntityWrapperInput<ColorsUpdateCommandInput>
            //        {
            //            IntegrationId = integrationId,
            //            SyncItems = mapping
            //        };
            //        return this.ColorsUpdateCommand.Execute(wrapper);
            //    })
            //);

            //taskDictionary.Add(
            //    "Categories"
            //    , Task.Run<object>(async () =>
            //    {
            //        var combination = new List<DirectGetProductCategoriesResult>();
            //        var filter = new PageQuery<FunzaGetCategoriesCommandInput>();
            //        var source = await this.GetCategoriesCommand.ExecuteAsync(filter);
            //        combination.AddRange(source.Bag);

            //        var mapping = this.Map(combination, integrationId: integrationId);
            //        var wrapper = new SyncCommandEntityWrapperInput<CategoriesUpdateCommandInput>
            //        {
            //            IntegrationId = integrationId,
            //            SyncItems = mapping
            //        };
            //        return this.CategoriesUpdateCommand.Execute(wrapper);
            //    })
            //);

            taskDictionary.Add(
                "Packings"
                , Task.Run <object>(async() =>
            {
                var combination = new List <DirectGetPackingsResult>();
                var filter      = new PageQuery <GetPackingsCommandInput>();
                var source      = await this.GetPackingsCommand.ExecuteAsync(filter);
                combination.AddRange(source.Bag);

                var mapping = this.Map(combination, integrationId: integrationId);
                var wrapper = new SyncCommandEntityWrapperInput <PackingsUpdateCommandInput>
                {
                    IntegrationId = integrationId,
                    SyncItems     = mapping
                };
                return(this.PackingsUpdateCommand.Execute(wrapper));
            })
                );

            //taskDictionary.Add(
            //    "Colors"
            //    , Task.Run<object>(async () =>
            //    {
            //        var combination = new List<FunzaGetColorsCommandOutput>();
            //        var filter = new PageQuery<FunzaGetColorsCommandInput>();
            //        var source = await this.GetColorsCommand.ExecuteAsync(filter);
            //        combination.AddRange(source.Bag.Items);
            //        while (source.IsSucceed && source.Bag?.Items.Count > 0)
            //        {
            //            filter.PageIndex++;
            //            source = await this.GetColorsCommand.ExecuteAsync(filter);
            //            if (source.IsSucceed && source.Bag != null)
            //            {
            //                combination.AddRange(source.Bag.Items);
            //            }
            //        }

            //        var mapping = this.Map(combination);

            //        return this.ColorsUpdateCommand.Execute(mapping);

            //    })
            //);

            //taskDictionary.Add(
            //    "Categories"
            //    , Task.Run<object>(async () =>
            //    {
            //        var combination = new List<FunzaGetCategoriesCommandOutput>();
            //        var filter = new PageQuery<FunzaGetCategoriesCommandInput>();
            //        var source = await this.GetCategoriesCommand.ExecuteAsync(filter);
            //        combination.AddRange(source.Bag.Items);
            //        while (source.IsSucceed && source.Bag?.Items.Count > 0)
            //        {
            //            filter.PageIndex++;
            //            source = await this.GetCategoriesCommand.ExecuteAsync(filter);
            //            if (source.IsSucceed && source.Bag != null)
            //            {
            //                combination.AddRange(source.Bag.Items);
            //            }
            //        }

            //        var mapping = this.Map(combination);

            //        return this.CategoriesUpdateCommand.Execute(mapping);

            //    })
            //);

            /*
             * taskDictionary.Add(
             *  "Packings"
             *  , Task.Run<object>(() =>
             *  {
             *      var source = this.GetPackingsCommand.Execute();
             *      if (source.IsSucceed && source.Bag != null && source.Bag.Count() > 0)
             *      {
             *          var dest = this.Map(source.Bag);
             *          return this.PackingsUpdateCommand.Execute(dest);
             *      }
             *
             *      return new OperationResponse<FunzaPackingsUpdateCommandOutputDTO>();
             *  })
             * );
             *
             * taskDictionary.Add(
             *  "Quotes"
             *  , Task.Run<object>(() =>
             *  {
             *      int pageIndex = 0;
             *      int pageSize = 100;
             *      var pageQuery = new PageQuery<FunzaQuoteGetItemsCommandInputDTO> {
             *          PageIndex = pageIndex, PageSize = pageSize
             *      };
             *
             *      PageResult<FunzaQuoteGetItemsCommandOutputDTO> loopResult = null;
             *
             *      for (; pageIndex == 0 || (pageIndex > 0 && loopResult.Items.Count == pageSize); pageIndex++)
             *      {
             *          var source = this.GetQuotesCommand.Execute(pageQuery);
             *          loopResult = source.Bag;
             *          if (source.IsSucceed && source.Bag != null && loopResult.Items.Count() > 0)
             *          {
             *              var dest = this.Map(loopResult.Items);
             *              return this.QuotesUpdateCommand.Execute(dest);
             *          }
             *      }
             *
             *      return new OperationResponse<FunzaPackingsUpdateCommandOutputDTO>();
             *  })
             * );*/

            await Task.WhenAll(taskDictionary.Values.ToArray());

            result.AddResponse(taskDictionary["Products"].Result as OperationResponse);
            result.AddResponse(taskDictionary["Colors"].Result as OperationResponse);
            result.AddResponse(taskDictionary["Categories"].Result as OperationResponse);
            //result.AddResponse(taskDictionary["Packings"].Result as OperationResponse);
            //result.AddResponse(taskDictionary["Quotes"].Result as OperationResponse);

            return(result);
        }