ProductList
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using Bimix.Domain.Entities;
|
||||
using Bimix.Infrastructure.Data;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -8,17 +10,23 @@ namespace Bimix.Infrastructure.Sync;
|
||||
|
||||
public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfiguration configuration)
|
||||
{
|
||||
private readonly HttpClient _httpClient = httpClient;
|
||||
private readonly BimixDbContext _db = db;
|
||||
private readonly IConfiguration _configuration = configuration;
|
||||
/// <summary>
|
||||
/// Dekoduje encje HTML w ciągu znaków (np. " na ")
|
||||
/// </summary>
|
||||
private string DecodeHtmlEntities(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return text;
|
||||
|
||||
return HttpUtility.HtmlDecode(text);
|
||||
}
|
||||
public async Task RunAsync()
|
||||
{
|
||||
var apiKey = _configuration["E5_CRM:ApiKey"];
|
||||
var syncState = _db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
|
||||
var apiKey = configuration["E5_CRM:ApiKey"];
|
||||
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
|
||||
|
||||
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
|
||||
var response = await _httpClient.GetStringAsync(url);
|
||||
var response = await httpClient.GetStringAsync(url);
|
||||
|
||||
var products = JsonSerializer.Deserialize<List<JsonElement>>(response);
|
||||
if (products == null) return;
|
||||
@@ -28,10 +36,10 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
||||
foreach (var p in products)
|
||||
{
|
||||
var idStr = p.GetProperty("id").GetString() ?? "";
|
||||
var name = p.GetProperty("name").GetString() ?? "";
|
||||
var code = p.GetProperty("code").GetString() ?? "";
|
||||
var stockAddresses = p.GetProperty("stock_addresses").GetString() ?? "";
|
||||
var ean = p.GetProperty("ean").GetString() ?? "";
|
||||
var name = DecodeHtmlEntities(p.GetProperty("name").GetString() ?? "");
|
||||
var code = DecodeHtmlEntities(p.GetProperty("code").GetString() ?? "");
|
||||
var stockAddresses = DecodeHtmlEntities(p.GetProperty("stock_addresses").GetString() ?? "");
|
||||
var ean = DecodeHtmlEntities(p.GetProperty("ean").GetString() ?? "");
|
||||
|
||||
if (!Guid.TryParse(idStr, out Guid id))
|
||||
{
|
||||
@@ -39,7 +47,7 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
||||
continue;
|
||||
}
|
||||
|
||||
var existing = _db.Products.FirstOrDefault(x => x.Id == id);
|
||||
var existing = db.Products.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
@@ -53,7 +61,7 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
_db.Products.Add(product);
|
||||
db.Products.Add(product);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,17 +73,17 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
||||
|
||||
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
|
||||
await _httpClient.GetAsync(updateUrl);
|
||||
await httpClient.GetAsync(updateUrl);
|
||||
}
|
||||
syncState.LastSynced = now;
|
||||
if (_db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
|
||||
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
|
||||
{
|
||||
_db.SyncStates.Add(syncState);
|
||||
db.SyncStates.Add(syncState);
|
||||
}
|
||||
else
|
||||
{
|
||||
_db.SyncStates.Update(syncState);
|
||||
db.SyncStates.Update(syncState);
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user