153 lines
3.4 KiB
C#
153 lines
3.4 KiB
C#
using PartSource.Automation.Jobs.Interfaces;
|
|
using PartSource.Automation.Models;
|
|
using PartSource.Data;
|
|
using PartSource.Data.Models;
|
|
using PartSource.Services.Integrations;
|
|
using Ratermania.Shopify.Entities;
|
|
using System;
|
|
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()
|
|
{
|
|
IList<ImportData> items = _partSourceContext.ImportData
|
|
.Where(i => i.IsVariant.Value)
|
|
.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 = 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),
|
|
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)
|
|
{
|
|
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}");
|
|
}
|
|
|
|
items = _partSourceContext.ImportData
|
|
.Where(i => i.Title == _partSourceContext.ImportData.First(d => d.ShopifyId == null).Title)
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
//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);
|
|
// }
|
|
//}
|
|
}
|