Stuff and things

This commit is contained in:
2021-06-29 19:00:13 -04:00
parent 0ff42f148a
commit 962ad3383f
22 changed files with 320 additions and 226 deletions

View File

@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using PartSource.Data.Dtos;
using PartSource.Data.Models;
using PartSource.Services;
using Ratermania.Shopify.Resources;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace PartSource.Api.Controllers
{
/// <remarks>
/// This endpoint handles Shopify webhooks
/// </remarks>
[Route("v2/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = "v2")]
public class WebhooksController : BaseApiController
{
private readonly ShopifyChangelogService _shopifyChangelogService;
public WebhooksController(ShopifyChangelogService shopifyChangelogService)
{
_shopifyChangelogService = shopifyChangelogService;
}
[HttpPost]
[Route("products/update")]
public async Task<ActionResult> OnProductUpdate([FromBody]Product product)
{
await _shopifyChangelogService.AddEntry(product);
return Ok();
}
}
}

View File

@@ -28,8 +28,11 @@
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.15" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Ratermania.Shopify" Version="1.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.ReDoc" Version="5.5.1" />
</ItemGroup>

View File

@@ -106,5 +106,10 @@
<response code="200"><strong>OK:</strong> The engine with the provided EngineConfigId.</response>
<response code="404"><strong>Not Found:</strong> No engine was found matching the provided EngineConfigId.</response>
</member>
<member name="T:PartSource.Api.Controllers.WebhooksController">
<remarks>
This endpoint handles Shopify webhooks
</remarks>
</member>
</members>
</doc>

View File

@@ -6,9 +6,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using PartSource.Api.Formatters;
using PartSource.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using PartSource.Data.AutoMapper;
using PartSource.Data.Contexts;
using PartSource.Services;
using System.IO;
@@ -30,6 +31,14 @@ namespace PartSource.Api
{
options.OutputFormatters.RemoveType(typeof(StringOutputFormatter));
options.EnableEndpointRouting = false;
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
});
services.AddSwaggerGen(c =>
@@ -46,6 +55,7 @@ namespace PartSource.Api
services.AddTransient<NexpartService>();
services.AddTransient<SecurityService>();
services.AddTransient<VehicleService>();
services.AddTransient<ShopifyChangelogService>();
services.AddCors(o => o.AddPolicy("Default", builder =>
{
@@ -57,6 +67,9 @@ namespace PartSource.Api
services.AddDbContext<PartSourceContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("PartSourceDatabase"))
);
services.AddDbContext<FitmentContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("FitmentDatabase"))
);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@@ -1,7 +1,8 @@
{
"ConnectionStrings": {
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"ConnectionStrings": {
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"

View File

@@ -42,94 +42,36 @@ namespace PartSource.Automation.Jobs
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
ConcurrentQueue<FileInfo> files = new ConcurrentQueue<FileInfo>(directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).OrderBy(f => f.Length));
IEnumerable<IGrouping<string, FileInfo>> fileGroups = directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last());
while (files.Count > 0)
foreach (IGrouping<string, FileInfo> fileGroup in fileGroups)
{
Parallel.For(0, 4, index =>
foreach (FileInfo fileInfo in fileGroup)
{
if (!files.TryDequeue(out FileInfo fileInfo))
{
return;
}
string filename = Decompress(fileInfo);
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("LineCode", typeof(string));
dataTable.Columns.Add("PartNumber", typeof(string));
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("EngineConfigId", typeof(int));
dataTable.Columns.Add("Position", typeof(string));
dataTable.Columns.Add("NoteText", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
try
{
int skippedLines = 0;
while (reader.Peek() > 0)
{
line = reader.ReadLine();
string[] columns = line.Split("\",\"");
if (columns.Length != 8)
{
skippedLines++;
continue;
}
for (int i = 0; i < columns.Length; i++)
{
columns[i] = columns[i].Replace("\"", string.Empty);
}
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
string position = columns[7].Trim();
string noteText = columns[4].Trim();
if (!string.IsNullOrEmpty(lineCode)
&& !string.IsNullOrEmpty(partNumber)
&& int.TryParse(columns[5], out int baseVehicleId)
&& int.TryParse(columns[6], out int engineConfigId))
{
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteText });
}
}
string filename = Decompress(fileInfo);
DataTable dataTable = GetDataTable(filename);
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
_whiSeoService.BulkCopy(_seoDataType, dataTable, tableName);
if (skippedLines == 0)
{
_logger.LogInformation($"Copied {filename} to the database.");
}
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
else
{
_logger.LogWarning($"Copied {filename} to the database with warnings. {skippedLines} lines contained errors and could not be processed.");
}
File.Delete(fileInfo.FullName);
File.Delete(filename);
}
catch (Exception ex)
{
_logger.LogError($"Failed to copy {filename} to the database.", ex);
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
}
}
try
{
reader.Close();
File.Delete(filename);
}
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
_whiSeoService.CreateFitmentTable(fitmentTable);
catch (Exception ex) { }
});
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
}
_whiSeoService.CreateFitmentView();
@@ -146,5 +88,45 @@ namespace PartSource.Automation.Jobs
return decompressedFile;
}
private DataTable GetDataTable(string filename)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("LineCode", typeof(string));
dataTable.Columns.Add("PartNumber", typeof(string));
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("EngineConfigId", typeof(int));
//dataTable.Columns.Add("Position", typeof(string));
//dataTable.Columns.Add("NoteText", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
while (reader.Peek() > 0)
{
line = reader.ReadLine();
string[] columns = line.Split("\",\"");
for (int i = 0; i < columns.Length; i++)
{
columns[i] = columns[i].Replace("\"", string.Empty);
}
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
string position = columns[7].Trim();
string noteText = columns[4].Trim();
if (!string.IsNullOrEmpty(lineCode)
&& !string.IsNullOrEmpty(partNumber)
&& int.TryParse(columns[5], out int baseVehicleId)
&& int.TryParse(columns[6], out int engineConfigId))
{
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId }); //, position, noteText });
}
}
return dataTable;
}
}
}

View File

@@ -1,17 +1,14 @@
using Ratermania.Automation.Interfaces;
using PartSource.Automation.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PartSource.Data;
using PartSource.Services;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using PartSource.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{

View File

@@ -74,7 +74,7 @@ namespace PartSource.Automation.Jobs
bool isFitment = false;
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode, 255);
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
//if (vehicles.Count > 250)
//{
@@ -166,9 +166,9 @@ namespace PartSource.Automation.Jobs
tags.AddRange(ymmFitment);
if (tags.Count > 250)
if (tags.Count > 249)
{
tags = tags.Take(250).ToList();
tags = tags.Take(249).ToList();
}
string zzzIsFitment = isFitment
@@ -188,7 +188,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku}", ex);
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}

View File

@@ -1,9 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Ratermania.Automation.Interfaces;
using PartSource.Automation.Models;
using PartSource.Data;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Services;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using Ratermania.Shopify.Resources.Enums;
@@ -11,8 +11,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Services;
namespace PartSource.Automation.Jobs
{
@@ -56,35 +54,45 @@ namespace PartSource.Automation.Jobs
{
if (product.Variants.Length > 0)
{
Variant variant = product.Variants[0];
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
bool hasUpdate = false;
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
for (int i = 0; i < product.Variants.Length; i++)
{
continue;
Variant variant = product.Variants[i];
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
{
continue;
}
if (product.Variants[i].Price.ToString("G29") != partPrice.Your_Price.Value.ToString("G29") || product.Variants[i].CompareAtPrice.ToString("G29") != partPrice.Compare_Price.Value.ToString("G29"))
{
product.Variants[i].Price = partPrice.Your_Price.Value;
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
product.PublishedScope = PublishedScope.Global;
//Metafield metafield = new Metafield
//{
// Namespace = "Pricing",
// Key = "CorePrice",
// Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
// ValueType = "string",
// OwnerResource = "product",
// OwnerId = product.Id
//};
hasUpdate = true;
}
}
if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
if (hasUpdate)
{
product.Variants[0].Price = partPrice.Your_Price.Value;
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
product.PublishedScope = PublishedScope.Global;
Metafield metafield = new Metafield
{
Namespace = "Pricing",
Key = "CorePrice",
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
ValueType = "string",
OwnerResource = "product",
OwnerId = product.Id
};
try
{
await _shopifyClient.Metafields.Add(metafield);
//await _shopifyClient.Metafields.Add(metafield);
await _shopifyClient.Products.Update(product);
updateCount++;
@@ -92,7 +100,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
_logger.LogWarning(ex, $"Failed to update pricing for product ID {product.Id}");
}
}
}
@@ -100,7 +108,7 @@ namespace PartSource.Automation.Jobs
try
{
products = await _shopifyClient.Products.GetNext();
products = await _shopifyClient.Products.GetNext();
_logger.LogInformation($"Total updated: {updateCount}");
}

View File

@@ -70,23 +70,23 @@ namespace PartSource.Automation
{
options.HasBaseInterval(new TimeSpan(0, 15, 0))
.HasMaxFailures(5)
//
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
//.HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<ProcessWhiFitment>()
// .StartsAt(DateTime.Today.AddHours(8))
//)
.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
.StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
)
.HasJob<ExecuteSsisPackages>(options => options.HasInterval(new TimeSpan(24, 0, 0))
.StartsAt(DateTime.Today.AddHours(26))
)
//
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
//.HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<ProcessWhiFitment>()
// .StartsAt(DateTime.Today.AddHours(8))
//)
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
//)
.HasJob<ExecuteSsisPackages>(options => options.HasInterval(new TimeSpan(24, 0, 0))
.StartsAt(DateTime.Today.AddHours(26))
)
.HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
.HasDependency<ExecuteSsisPackages>()
.StartsAt(DateTime.Today.AddHours(27)
)
.HasDependency<ExecuteSsisPackages>()
.StartsAt(DateTime.Today.AddHours(27)
)
);
//.AddApiServer();
})

View File

@@ -1,4 +1,6 @@
using Microsoft.Extensions.Configuration;
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Configuration;
using PartSource.Automation.Models.Enums;
@@ -65,20 +67,28 @@ namespace PartSource.Automation.Services
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
using SqlCommand command = new SqlCommand($"EXEC AddFitmentTable @tableName = '{tableName}'", connection);
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"{seoDataType}.{tableName}",
DestinationTableName = $"FitmentTemp.{tableName}",
BulkCopyTimeout = 14400
};
bulk.WriteToServer(dataTable);
}
public void CreateFitmentTable(string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
}
public void CreateFitmentView()
{
using SqlConnection connection = new SqlConnection(_connectionString);
@@ -89,3 +99,4 @@ namespace PartSource.Automation.Services
}
}
}
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

View File

@@ -2,9 +2,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using PartSource.Data.Converters;
using PartSource.Data.Models;
namespace PartSource.Data
namespace PartSource.Data.Contexts
{
public class PartSourceContext : DbContext
{
@@ -29,6 +30,8 @@ namespace PartSource.Data
public DbSet<Part> Parts { get; set; }
public DbSet<ShopifyChangelog> ShopifyChangelogs { get; set; }
public DbSet<Vehicle> Vehicles { get; set; }
public DbSet<PartsAvailability> PartAvailabilities { get; set; }
@@ -55,11 +58,18 @@ namespace PartSource.Data
modelBuilder.Entity<PartsAvailability>().HasKey(p => new { p.Store, p.SKU });
modelBuilder.Entity<ShopifyChangelog>()
.Property(s => s.ResourceType)
.HasConversion(new TypeToStringConverter());
modelBuilder.Entity<ShopifyChangelog>()
.Property(s => s.Data)
.HasConversion(new ObjectToJsonConverter());
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
entityType.Relational().TableName = entityType.ClrType.Name;
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace PartSource.Data.Converters
{
public class ObjectToJsonConverter : ValueConverter<object, string>
{
private static readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
public ObjectToJsonConverter() : base(ObjectToJson, JsonToObject) { }
public static Expression<Func<string, object>> JsonToObject = value => JsonConvert.DeserializeObject(value, _serializerSettings);
public static Expression<Func<object, string>> ObjectToJson = value => JsonConvert.SerializeObject(value, _serializerSettings);
}
}

View File

@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace PartSource.Data.Converters
{
public class TypeToStringConverter : ValueConverter<Type, string>
{
public TypeToStringConverter() : base(TypeToString, StringToType) { }
public static Expression<Func<string, Type>> StringToType = value => Type.GetType(value);
public static Expression<Func<Type, string>> TypeToString = value => value.AssemblyQualifiedName;
}
}

View File

@@ -1,15 +1,14 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PartSource.Data;
using PartSource.Data.Contexts;
using System;
namespace PartSource.Data.Migrations
{
[DbContext(typeof(PartSourceContext))]
[DbContext(typeof(PartSourceContext))]
[Migration("20190811020941_ImportDateDcfMapping")]
partial class ImportDateDcfMapping
{

View File

@@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PartSource.Data;
using PartSource.Data.Contexts;
namespace PartSource.Data.Migrations
{

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace PartSource.Data.Models
{
public class ShopifyChangelog
{
public int Id { get; set; }
public Type ResourceType { get; set; }
public long ShopifyId { get; set; }
public DateTime Timestamp { get; set; }
public object Data { get; set; }
}
}

View File

@@ -1,73 +0,0 @@
//using Newtonsoft.Json;
//using PartSource.Data.Shopify;
//using PartSource.Services.Integrations;
//using Ratermania.Shopify;
//using System;
//using System.Collections.Generic;
//using System.Diagnostics.CodeAnalysis;
//using System.Text;
//using System.Threading.Tasks;
//namespace PartSource.Services
//{
// public class MetafieldService
// {
// private readonly ShopifyClient _shopifyClient;
// public MetafieldService(ShopifyClient shopifyClient)
// {
// _shopifyClient = shopifyClient;
// }
// [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Lowercase is expected by Shopify")]
// public async Task SaveMetafield<T>(T shopifyEntity, IList<int> vehicleIds, string @namespace) where T : ShopifyEntity
// {
// if (vehicleIds.Count == 0)
// {
// return;
// }
// string json = JsonConvert.SerializeObject(vehicleIds);
// if (json.Length >= 100000)
// {
// // TODO: Logging
// return;
// }
// string key = @namespace.ToLowerInvariant().Replace(" ", "_");
// if (key.Length > 20)
// {
// key = key.Substring(0, 20);
// }
// Metafield metafield = new Metafield
// {
// Namespace = "position",
// Key = key,
// Value = json,
// ValueType = "json_string",
// OwnerResource = "product",
// OwnerId = shopifyEntity.Id
// };
// await _shopifyClient.Metafields.Add(metafield);
// }
// public async Task DeleteMetafields<T>(long shopifyId) where T : ShopifyEntity
// {
// IDictionary<string, object> parameters = new Dictionary<string, object>
// {
// { "metafield[owner_id]", shopifyId},
// { "metafield[owner_resource]", "product" },
// { "namespace", "position" },
// };
// IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(parameters);
// foreach (Metafield metafield in metafields)
// {
// await _shopifyClient.Metafields.Delete(metafield);
// }
// }
// }
//}

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Dtos;
using PartSource.Data.Models;
using System;

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Ratermania.Shopify" Version="1.3.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using System.Threading.Tasks;

View File

@@ -0,0 +1,34 @@
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ratermania.Shopify.Resources;
namespace PartSource.Services
{
public class ShopifyChangelogService
{
private readonly PartSourceContext _partsourceContext;
public ShopifyChangelogService(PartSourceContext partsourceContext)
{
_partsourceContext = partsourceContext;
}
public async Task AddEntry<T>(T data) where T : BaseShopifyResource
{
ShopifyChangelog shopifyChangelog = new ShopifyChangelog
{
ResourceType = typeof(T),
ShopifyId = data.Id,
Data = data,
Timestamp = DateTime.Now
};
await _partsourceContext.AddAsync(shopifyChangelog);
await _partsourceContext.SaveChangesAsync();
}
}
}