Migration to DevOps
This commit is contained in:
75
PartSource.Automation/Jobs/SyncronizeProducts.cs
Normal file
75
PartSource.Automation/Jobs/SyncronizeProducts.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using PartSource.Automation.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Data;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using PartSource.Data.Models;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user