72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using PartSource.Data.Contexts;
|
|
using PartSource.Data.Models;
|
|
using Ratermania.Automation.Interfaces;
|
|
using Ratermania.Shopify;
|
|
using Ratermania.Shopify.Resources;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PartSource.Automation.Jobs
|
|
{
|
|
/// <summary>
|
|
/// Ensures syncronization between Shopify IDs and Partsource SKUs
|
|
/// </summary>
|
|
public class SyncronizeProducts : IAutomationJob
|
|
{
|
|
private readonly PartSourceContext _partSourceContext;
|
|
private readonly ShopifyClient _shopifyClient;
|
|
private readonly ILogger<TestJob> _logger;
|
|
|
|
public SyncronizeProducts(ILogger<TestJob> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
|
{
|
|
_partSourceContext = partSourceContext;
|
|
_shopifyClient = shopifyClient;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
public async Task Run()
|
|
{
|
|
IList<ImportData> importData = _partSourceContext.ImportData.FromSql<ImportData>($"SELECT * FROM ImportDataFilters").ToList();
|
|
|
|
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
|
|
|
while (products?.Any() == true)
|
|
{
|
|
|
|
foreach (Product product in products)
|
|
{
|
|
foreach (Variant variant in product.Variants)
|
|
{
|
|
ImportData item = importData.FirstOrDefault(i => i.VariantSku == variant.Sku);
|
|
|
|
if (item != null)
|
|
{
|
|
_partSourceContext.Database.ExecuteSqlCommand($"UPDATE ImportDataFilters SET ShopifyId = {product.Id} WHERE VariantSku = {variant.Sku}");
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
_logger.LogInformation("Did 250");
|
|
//await _partSourceContext.SaveChangesAsync();
|
|
}
|
|
|
|
catch
|
|
{
|
|
Console.WriteLine("Failed to save a batch of products");
|
|
}
|
|
|
|
finally
|
|
{
|
|
products = await _shopifyClient.Products.GetNext();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |