Files
Partsource/PartSource.Automation/Jobs/DeleteProducts.cs

73 lines
1.9 KiB
C#

using PartSource.Automation.Jobs.Interfaces;
using PartSource.Automation.Models;
using PartSource.Data;
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 DeleteProducts : IAutomationJob
{
private readonly ShopifyClient _shopifyClient;
private readonly PartSourceContext _partSourceContext;
public DeleteProducts(ShopifyClient shopifyClient, PartSourceContext partSourceContext)
{
_shopifyClient = shopifyClient;
_partSourceContext = partSourceContext;
}
// If this job fails, oh well. Run it again and again until it works, or use the Shopify UI (LOL)
public async Task<AutomationJobResult> Run()
{
Console.WriteLine("This job will delete ALL PRODUCTS from Shopify. If you really want to delete EVERY SINGLE PRODUCT, type 'mechanical keyboard' below.");
string input = Console.ReadLine();
if (input != "mechanical keyboard")
{
return new AutomationJobResult
{
IsSuccess = true
};
}
IList<long?> shopifyIds = _partSourceContext.ImportData
.Select(i => i.ShopifyId)
.Distinct()
.ToList();
foreach (long? id in shopifyIds)
{
Product product = await _shopifyClient.Products.GetById((long)id);
await _shopifyClient.Products.Delete(product);
Console.WriteLine(id);
}
//IEnumerable<Product> products = await _shopifyClient.Products.Get();
//while (products != null)
//{
// foreach (Product product in products)
// {
// bool result = await _shopifyClient.Products.Delete(product);
// }
// products = await _shopifyClient.Products.GetNext();
// Console.Write('.');
//}
return new AutomationJobResult
{
Message = "All products deleted. Don't forget to truncate the ImportData table",
IsSuccess = true
};
}
}
}