public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); string apiKey = Settings.GetApiKey(); if (!string.IsNullOrEmpty(apiKey)) { var products = new ProductInventoryRepository(Settings.ConnectionString).GetAll().ToList(); var existingCategories = WebServiceHelper.GetAllCategories(); var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; var bulkPushRequest = new List <InventoryItemRequest>(); var productsToAdd = products.Where(p => existingInventory.All(ei => ei.PrivateSKU != p.Id)); // Add new items foreach (var product in productsToAdd) { try { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); WebServiceHelper.PushInventoryItem(request, out var statusCode, out var content); bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug( $"Finished importing product {items} of {products.Count}. Id: {product.Id}"); Logger.Instance.Debug($"Adding response {product.Id} {statusCode}"); Logger.Instance.Debug($"Adding response {product.Id} {content}"); } catch (Exception ex) { Logger.Instance.Error("Adding " + JsonConvert.SerializeObject(product) + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } } // Update existing items foreach (var product in products.Where(p => existingInventory.Any(ei => ei.PrivateSKU == p.Id))) { try { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); if (request != null) { bulkPushRequest.Add(request); } if (bulkPushRequest.Count > 10) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray(), out var statusCode, out var content); Logger.Instance.Debug($"Bulk Push: Response: {statusCode}"); Logger.Instance.Debug($"Bulk Push: Response Content: {content}"); bulkPushRequest.Clear(); } bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug( $"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } catch (Exception ex) { Logger.Instance.Error("Updating " + JsonConvert.SerializeObject(product) + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } } if (bulkPushRequest.Count > 0) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray(), out var statusCode, out var content); Logger.Instance.Debug($"Bulk Push Response {statusCode}"); Logger.Instance.Debug($"Bulk Push Response {content}"); } WebServiceHelper.PostInventoryImport(); publishDetails.Insert(0, $"{items} products published to LinkGreen"); return(true); } publishDetails.Insert(0, "No Api Key set while executing products publish."); Logger.Instance.Warning("No Api Key set while executing products publish."); return(false); }
public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); string apiKey = Settings.GetApiKey(); if (!string.IsNullOrEmpty(apiKey)) { var products = new ProductInventoryRepository(Settings.ConnectionString).GetAll().ToList(); var existingCategories = WebServiceHelper.GetAllCategories(); var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; var bulkPushRequest = new List <InventoryItemRequest>(); var productsToAdd = products.Where(p => existingInventory.All(ei => ei.PrivateSKU != p.Id)); // Add new items foreach (var product in productsToAdd) { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); WebServiceHelper.PushInventoryItem(request); bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug($"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } // Update existing items foreach (var product in products.Where(p => existingInventory.Any(ei => ei.PrivateSKU == p.Id))) { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); if (request != null) { bulkPushRequest.Add(request); } if (bulkPushRequest.Count > 10) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray()); bulkPushRequest.Clear(); } bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug($"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } if (bulkPushRequest.Count > 0) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray()); } WebServiceHelper.PostInventoryImport(); publishDetails.Insert(0, $"{items} products published to LinkGreen"); return(true); } publishDetails.Insert(0, "No Api Key set while executing products publish."); Logger.Instance.Warning("No Api Key set while executing products publish."); return(false); }