Files
Partsource/PartSource.Automation/Jobs/Archive/AddAndUpdateProducts.cs

325 lines
8.1 KiB
C#

//using PartSource.Automation.Models;
//using PartSource.Data;
//using PartSource.Data.Models;
//using Ratermania.Shopify;
//using Ratermania.Shopify.Resources;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//namespace PartSource.Automation.Jobs
//{
// public class AddAndUpdateProducts// : IAutomationJob
// {
// private readonly PartSourceContext _partSourceContext;
// private readonly ShopifyClient _shopifyClient;
// public AddAndUpdateProducts(PartSourceContext partSourceContext, ShopifyClient shopifyClient)
// {
// _partSourceContext = partSourceContext;
// _shopifyClient = shopifyClient;
// }
// public async Task<AutomationJobResult> Run()
// {
// //throw new Exception("You need to add a ProductVariant resource to the Shopify client");
// await SyncronizeIdsAndSkus();
// //await AddSkus();
// // await AddVariants();
// return new AutomationJobResult
// {
// IsSuccess = true
// };
// }
// /// <summary>
// /// Ensures syncronization between Shopify IDs and Partsource SKUs
// /// </summary>
// private async Task SyncronizeIdsAndSkus()
// {
// IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
// //_partSourceContext.Database.ExecuteSqlCommand("UPDATE ImportData SET ShopifyId = NULL");
// while (products != null && products.Any())
// {
// foreach (Product product in products)
// {
// foreach (Variant variant in product.Variants)
// {
// ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == variant.Sku);
// if (importData != null)
// {
// importData.ShopifyId = product.Id;
// }
// }
// }
// try
// {
// await _partSourceContext.SaveChangesAsync();
// }
// catch
// {
// Console.WriteLine("Failed to save a batch of products");
// }
// finally
// {
// products = await _shopifyClient.Products.GetNext();
// }
// }
// }
// public async Task AddSkus()
// {
// IList<ImportData> items = _partSourceContext.ImportData
// .Where(i => i.IsVariant == null && i.ShopifyId == null)
// // .OrderBy(i => i.Title)
// .ToList();
// // items = items.Where(i => i.Title == items.First().Title).ToList();
// //
// foreach (ImportData importData in items)
// {
// try
// {
// // Images
// IList<ProductImage> productImages = new List<ProductImage>();
// if (!string.IsNullOrEmpty(importData.ImageSrc))
// {
// foreach (string src in importData?.ImageSrc.Split(','))
// {
// productImages.Add(new ProductImage
// {
// Src = src,
// Alt = importData.ImageAltText
// });
// }
// if (productImages.Count > 0)
// {
// productImages.Add(new ProductImage
// {
// Src = "https://cdn.shopify.com/s/files/1/2239/4255/files/No_Image_Found.jpg",
// Alt = "No Image Found"
// });
// }
// }
// // Product Tags
// List<string> productTags = new List<string>
// {
// items[0].LineCode,
// items[0].PartNumber,
// };
// //List<ProductVariant> productVariants = new List<ProductVariant>();
// ////foreach (ImportData itemVariant in items)
// ////{
// //productVariants.Add(new ProductVariant
// //{
// // InventoryPolicy = "Deny",
// // CompareAtPrice = importData.CompareAt,
// // Price = importData.Price,
// // Sku = importData.VariantSku,
// // Title = importData.VariantTitle ?? importData.Title,
// // Option1 = importData.VariantTitle,
// // RequiresShipping = false,
// //});
// //}
// Product requestData = new Product
// {
// BodyHtml = importData.BodyHtml,
// Title = importData.Title,
// Vendor = importData.Vendor,
// Tags = string.Join(",", productTags),
// //ProductType = importData.FINELINE_NM,
// Images = productImages.ToArray(),
// //Variants = productVariants.ToArray(),
// CreatedAt = DateTime.Now,
// UpdatedAt = DateTime.Now,
// PublishedAt = DateTime.Now
// };
// requestData = await _shopifyClient.Products.Add(requestData);
// if (requestData.Id > 0)
// {
// //foreach (ImportData variant in items)
// //{
// // variant.ShopifyId = requestData.Id;
// //}
// importData.ShopifyId = requestData.Id;
// _partSourceContext.SaveChanges();
// Console.WriteLine($"{importData.VariantSku}");
// }
// else
// {
// Console.WriteLine($"SHOPIFY ID WAS 0 - {importData.VariantSku}");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Failed to add SKU {importData.VariantSku}: {ex.StackTrace}");
// }
// //items = _partSourceContext.ImportData
// // .Where(i => i.Title == _partSourceContext.ImportData.First(d => d.ShopifyId == null).Title)
// // .ToList();
// }
// }
// public async Task AddVariants()
// {
// IList<ImportData> items = _partSourceContext.ImportData
// .Where(i => i.IsVariant.Value && i.ShopifyId == null)
// .OrderBy(i => i.Title)
// .ToList();
// items = items.Where(i => i.Title == items.First().Title).ToList();
// while (items != null && items.Count > 0)
// {
// // Images
// IList<ProductImage> productImages = new List<ProductImage>();
// if (!string.IsNullOrEmpty(items[0].ImageSrc))
// {
// productImages = items.SelectMany(v =>
// {
// IList<ProductImage> images = new List<ProductImage>();
// foreach (string src in v.ImageSrc?.Split(','))
// {
// images.Add(new ProductImage
// {
// Src = src,
// Alt = v.ImageAltText
// });
// }
// return images;
// }).ToList();
// if (productImages.Count > 0)
// {
// productImages.Add(new ProductImage
// {
// Src = "https://cdn.shopify.com/s/files/1/2239/4255/files/No_Image_Found.jpg",
// Alt = "No Image Found"
// });
// }
// }
// // Product Tags
// List<string> productTags = new List<string>
// {
// items[0].LineCode,
// items[0].PartNumber,
// };
// //List<ProductVariant> productVariants = new List<ProductVariant>();
// //foreach (ImportData itemVariant in items)
// //{
// // productVariants.Add(new ProductVariant
// // {
// // InventoryPolicy = "Deny",
// // CompareAtPrice = itemVariant.CompareAt,
// // Price = itemVariant.Price,
// // Sku = itemVariant.VariantSku,
// // Title = itemVariant.VariantTitle,
// // Option1 = itemVariant.VariantTitle,
// // RequiresShipping = false,
// // });
// //}
// Product requestData = new Product
// {
// BodyHtml = items[0].BodyHtml,
// Title = items[0].Title,
// Vendor = items[0].Vendor,
// Tags = string.Join(",", productTags),
// //ProductType = importData.FINELINE_NM,
// Images = productImages.ToArray(),
// //Variants = productVariants.ToArray(),
// CreatedAt = DateTime.Now,
// UpdatedAt = DateTime.Now,
// PublishedAt = DateTime.Now
// };
// requestData = await _shopifyClient.Products.Add(requestData);
// if (requestData.Id > 0)
// {
// foreach (ImportData variant in items)
// {
// variant.ShopifyId = requestData.Id;
// }
// _partSourceContext.SaveChanges();
// Console.WriteLine($"{items[0].VariantSku}");
// }
// else
// {
// Console.WriteLine($"SHOPIFY ID WAS 0 - {items[0].VariantSku}");
// }
// ImportData next = _partSourceContext.ImportData.FirstOrDefault(i => i.IsVariant != null && i.ShopifyId == null);
// if (next != null)
// {
// items = _partSourceContext.ImportData
// .Where(i => i.Title == next.Title)
// .ToList();
// }
// else
// {
// items = null;
// }
// }
// }
// }
// //private void Log(string message)
// //{
// // try
// // {
// // using (FileStream fileStream = File.OpenWrite(@"C:\users\tommy\desktop\log.txt"))
// // {
// // fileStream.Write(Encoding.UTF8.GetBytes(message + "\n"));
// // }
// // }
// // catch
// // {
// // // LOL Fix this
// // Log(message);
// // }
// //}
//}