public void Process(Dictionary<string, PriceItem> priceItems, GeneratedPriceType generatedPriceType, PriceType processingPriceType)
        {
            Log.Debug("Connecting to API");

            _apiFactory.InitFactories(_apiUrl, _accessToken);
            _productUpdater = new ProductUpdater(_apiFactory);
            _productRemover = new ProductRemover(_apiFactory);
            _productCreator = new ProductCreator(_apiFactory);

            ProcessDiff(priceItems, generatedPriceType, processingPriceType);
        }
        private void ProcessDiff(Dictionary<string, PriceItem> priceItems, GeneratedPriceType generatedPriceType, PriceType processingPriceType)
        {
            int currentCount = 0;
            var photoLoadErrorsOccured = false;

            foreach (var item in priceItems.Values)
            {
                currentCount++;
                var filter = new Dictionary<string, string> { { "reference", item.Reference } };
                var existingProd = _apiFactory.ProductFactory.GetByFilter(filter, null, null);

                switch (generatedPriceType)
                {
                    case GeneratedPriceType.NewItems:
                        if (existingProd == null || !existingProd.Any())
                        {
                            try
                            {
                                Log.Info("Adding product {0} from {1}; Reference: {2}", currentCount, priceItems.Count, item.Reference);
                                _productCreator.Create(item);
                            }
                            catch (PhotoLoadException)
                            {
                                photoLoadErrorsOccured = true;
                            }
                            catch (Exception ex)
                            {
								Log.Error("Product add error. Reference: {0}; {1}", item.Reference, ex);
                            }
                        }
                        else
                        {
                            try
                            {
                                Log.Debug("Updating item {0} from {1}; Reference: {2}", currentCount, priceItems.Count, item.Reference);
                                _productUpdater.Update(existingProd.First(), item, processingPriceType);
                            }
                            catch (Exception ex)
                            {
                                Log.Error("Balance update error. Reference: {0}; {1}", item.Reference, ex);
                            }
                        }
                        break;
                    case GeneratedPriceType.SameItems:
                        if (existingProd == null || !existingProd.Any())
                        {
                            Log.Warn("Product does't exists. It will be added later. Reference: {0}", item.Reference);
                        }
                        else
                        {
                            try
                            {
                                Log.Debug("Updating item {0} from {1}; Reference: {2}", currentCount, priceItems.Count, item.Reference);
                                _productUpdater.Update(existingProd.First(), item, processingPriceType);
                            }
                            catch (Exception ex)
                            {
                                Log.Error("Update error. Reference: {0}; {1}", item.Reference, ex);
                            }
                        }
                        break;
                    case GeneratedPriceType.DeletedItems:
                        if (existingProd != null && existingProd.Any())
                        {
                            try
                            {
                                if (processingPriceType != PriceType.Discount)
                                {
                                    Log.Info("Disabling product {0} from {1}; Reference: {2}", currentCount, priceItems.Count, item.Reference);
                                    _productRemover.Remove(existingProd.First());
                                }
                                else
                                {
                                    _productUpdater.RemoveDiscountInfo(item, existingProd.First());
                                }
                            }
                            catch (Exception ex)
                            {
								Log.Error("Disable product error. Reference: {0}; {1}", item.Reference, ex);
                            }
                        }
                        break;
                }
            }
            if(photoLoadErrorsOccured)
            {
                throw new PhotoLoadException();
            }
        }
예제 #3
0
        public async Task Process(Dictionary <string, PriceItem> priceItems, GeneratedPriceType generatedPriceType, PriceType processingPriceType)
        {
            var currentCount           = 0;
            var photoLoadErrorsOccured = false;

            foreach (var kvp in priceItems)
            {
                currentCount++;

                var item = kvp.Value;
                Log.Information("Processing product {current} of {count} Reference: {reference}", currentCount, priceItems.Count, item.Reference);

                var filter = new Dictionary <string, string> {
                    { "reference", item.Reference }
                };
                var existingProd = await _apiFactory.ProductFactory.GetByFilter(filter, null, null);

                switch (generatedPriceType)
                {
                case GeneratedPriceType.NewItems:
                    if (existingProd == null || !existingProd.Any())
                    {
                        try
                        {
                            await _productCreator.Create(item);
                        }
                        catch (PhotoLoadException)
                        {
                            photoLoadErrorsOccured = true;
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Product add error. Reference: {reference}", item.Reference);
                        }
                    }
                    else
                    {
                        try
                        {
                            await _productUpdater.Update(existingProd.First(), item, processingPriceType);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Balance update error. Reference: {reference}", item.Reference);
                            await _productRemover.Remove(existingProd.First());
                        }
                    }
                    break;

                case GeneratedPriceType.SameItems:
                    if (existingProd == null || !existingProd.Any())
                    {
                        Log.Warning("Product does't exists. It will be added later. Reference: {0}", item.Reference);
                    }
                    else
                    {
                        try
                        {
                            await _productUpdater.Update(existingProd.First(), item, processingPriceType);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Update error. Reference: {reference}", item.Reference);
                            await _productRemover.Remove(existingProd.First());
                        }
                    }
                    break;

                case GeneratedPriceType.DeletedItems:
                    if (existingProd != null && existingProd.Any())
                    {
                        try
                        {
                            if (processingPriceType != PriceType.Discount)
                            {
                                await _productRemover.Remove(existingProd.First());
                            }
                            else
                            {
                                await _productUpdater.RemoveDiscountInfo(item, existingProd.First());
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Disable product error. Reference: {reference}", item.Reference);
                        }
                    }
                    break;
                }
            }
            if (photoLoadErrorsOccured)
            {
                throw new PhotoLoadException();
            }
        }