146 lines
3.3 KiB
C#
146 lines
3.3 KiB
C#
using PartSource.Automation.Jobs.Interfaces;
|
|
using PartSource.Automation.Models;
|
|
using PartSource.Automation.Services;
|
|
using PartSource.Data;
|
|
using PartSource.Data.Models;
|
|
using PartSource.Data.Shopify;
|
|
using PartSource.Services;
|
|
using PartSource.Services.Integrations;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PartSource.Automation.Jobs
|
|
{
|
|
public class AddProducts : IAutomationJob
|
|
{
|
|
private readonly PartSourceContext _partSourceContext;
|
|
private readonly ShopifyClient _shopifyClient;
|
|
|
|
public AddProducts(PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
|
{
|
|
_partSourceContext = partSourceContext;
|
|
_shopifyClient = shopifyClient;
|
|
}
|
|
|
|
|
|
public async Task<AutomationJobResult> Run()
|
|
{
|
|
await AddSkus();
|
|
|
|
return new AutomationJobResult
|
|
{
|
|
IsSuccess = true
|
|
};
|
|
}
|
|
|
|
public async Task AddSkus()
|
|
{
|
|
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(p => !p.ShopifyId.HasValue);
|
|
|
|
while (importData != null)
|
|
{
|
|
if (importData == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Images
|
|
List<ProductImage> productImages = new List<ProductImage>();
|
|
string[] imageUrls = importData.ImageSrc?.Split(',');
|
|
|
|
if (imageUrls?.Length > 0)
|
|
{
|
|
foreach (string url in imageUrls)
|
|
{
|
|
productImages.Add(new ProductImage
|
|
{
|
|
Src = url,
|
|
Alt = importData.ImageAltText
|
|
});
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
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>
|
|
{
|
|
importData.LineCode,
|
|
importData.PartNumber,
|
|
};
|
|
|
|
List<ProductVariant> productVariants = new List<ProductVariant>();
|
|
productVariants.Add(new ProductVariant
|
|
{
|
|
InventoryPolicy = "Deny",
|
|
CompareAtPrice = importData.CompareAt,
|
|
Price = importData.Price,
|
|
Sku = importData.VariantSku,
|
|
Title = importData.VariantTitle,
|
|
Option1 = importData.IsVariant.ToString(),
|
|
RequiresShipping = false
|
|
});
|
|
|
|
Product requestData = new Product
|
|
{
|
|
BodyHtml = importData.BodyHtml,
|
|
Title = importData.Title,
|
|
Vendor = importData.Vendor,
|
|
Tags = string.Join(",", productTags),
|
|
Published = true,
|
|
//ProductType = importData.FINELINE_NM,
|
|
Images = productImages.ToArray(),
|
|
Variants = productVariants.ToArray(),
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now
|
|
};
|
|
|
|
requestData = await _shopifyClient.Products.Add(requestData);
|
|
|
|
if (requestData.Id > 0)
|
|
{
|
|
importData.ShopifyId = requestData.Id;
|
|
|
|
_partSourceContext.SaveChanges();
|
|
|
|
Console.WriteLine($"{importData.VariantSku}");
|
|
}
|
|
|
|
else
|
|
{
|
|
Console.WriteLine($"SHOPIFY ID WAS 0 - {importData.VariantSku}");
|
|
}
|
|
|
|
importData = _partSourceContext.ImportData.FirstOrDefault(p => !p.ShopifyId.HasValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
//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);
|
|
// }
|
|
//}
|
|
}
|