This commit is contained in:
@@ -360,24 +360,60 @@ namespace AyaNova.Biz
|
|||||||
public async Task<List<string>> ImportData(AyImportData importData)
|
public async Task<List<string>> ImportData(AyImportData importData)
|
||||||
{
|
{
|
||||||
List<string> ImportResult = new List<string>();
|
List<string> ImportResult = new List<string>();
|
||||||
string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
|
string ImportTag = ImportUtil.GetImportTag();
|
||||||
|
//ignore these fields
|
||||||
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
||||||
foreach (JObject j in importData.Data)
|
foreach (JObject j in importData.Data)
|
||||||
{
|
{
|
||||||
var w = j.ToObject<Vendor>(jsset);
|
try
|
||||||
if (j["CustomFields"] != null)
|
|
||||||
w.CustomFields = j["CustomFields"].ToString();
|
|
||||||
w.Tags.Add(ImportTag);//so user can find them all and revert later if necessary
|
|
||||||
var res = await CreateAsync(w);
|
|
||||||
if (res == null)
|
|
||||||
{
|
{
|
||||||
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
long existingId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||||
this.ClearErrors();
|
if (existingId == 0)
|
||||||
|
{
|
||||||
|
if (importData.DoImport)
|
||||||
|
{
|
||||||
|
//import this record
|
||||||
|
var Target = j.ToObject<Vendor>(jsset);
|
||||||
|
Target.Tags.Add(ImportTag);
|
||||||
|
var res = await CreateAsync(Target);
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||||
|
this.ClearErrors();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImportResult.Add($"✔️ {Target.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (importData.DoUpdate)
|
||||||
|
{
|
||||||
|
//update this record with any data provided
|
||||||
|
//load existing record
|
||||||
|
var Target = await GetAsync((long)existingId);
|
||||||
|
var Source = j.ToObject<Vendor>(jsset);
|
||||||
|
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
|
||||||
|
propertiesToUpdate.Remove("Name");
|
||||||
|
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||||
|
var res = await PutAsync(Target);
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||||
|
this.ClearErrors();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImportResult.Add($"✔️ {Target.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ImportResult.Add($"{w.Name} - ok");
|
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ImportResult;
|
return ImportResult;
|
||||||
|
|||||||
Reference in New Issue
Block a user