feat: implement building alias and comment tables
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
namespace SanctionsSearch.Worker.Models;
|
||||
|
||||
class AliasMap : ClassMap<Alias>
|
||||
{
|
||||
public AliasMap()
|
||||
{
|
||||
Map(m => m.SdnId).Index(0);
|
||||
Map(m => m.Id).Index(1);
|
||||
Map(m => m.Type).Index(2).TypeConverter<NullCharacterConverter>();
|
||||
Map(m => m.Name).Index(3).TypeConverter<NullCharacterConverter>();
|
||||
Map(m => m.Remarks).Index(4).TypeConverter<NullCharacterConverter>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace SanctionsSearch.Worker.Models;
|
||||
|
||||
class CommentMap : ClassMap<Comment>
|
||||
{
|
||||
public CommentMap()
|
||||
{
|
||||
Map(m => m.SdnId).Index(0);
|
||||
Map(m => m.Remarks).Index(1).TypeConverter<NullCharacterConverter>();
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ class DatabaseMaintainer(
|
||||
|
||||
csv.Context.RegisterClassMap<SdnMap>();
|
||||
csv.Context.RegisterClassMap<AddressMap>();
|
||||
csv.Context.RegisterClassMap<AliasMap>();
|
||||
csv.Context.RegisterClassMap<CommentMap>();
|
||||
|
||||
_streamReaders.Add(reader);
|
||||
_csvReaders.Add(csv);
|
||||
@@ -77,14 +79,62 @@ class DatabaseMaintainer(
|
||||
await _unitOfWork.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task BuildCommentTableAsync()
|
||||
public async Task BuiltAliasTableAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var result = await _ofacFileService.GetAltNamesFileAsync();
|
||||
|
||||
if (result.IsFailed)
|
||||
{
|
||||
_logger.LogError("Failed to get Alias file from OFAC.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var stream = result.Value;
|
||||
var records = GetRecordsFromStream<Alias>(stream);
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
var sdn = await _unitOfWork.Sdns.Find(s => s.Id == record.SdnId);
|
||||
|
||||
if (sdn.Count() is 0)
|
||||
{
|
||||
_logger.LogWarning("Alias's SDN with ID {Id} not found. Skipping alias.", record.SdnId);
|
||||
continue;
|
||||
}
|
||||
|
||||
await _unitOfWork.Aliases.Upsert(record);
|
||||
}
|
||||
|
||||
await _unitOfWork.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task BuiltAliasTableAsync()
|
||||
public async Task BuildCommentTableAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var result = await _ofacFileService.GetCommentsFileAsync();
|
||||
|
||||
if (result.IsFailed)
|
||||
{
|
||||
_logger.LogError("Failed to get Comment file from OFAC.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var stream = result.Value;
|
||||
var records = GetRecordsFromStream<Comment>(stream);
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
var sdn = await _unitOfWork.Sdns.Find(s => s.Id == record.SdnId);
|
||||
|
||||
if (sdn.Count() is 0)
|
||||
{
|
||||
_logger.LogWarning("Comment's SDN with ID {Id} not found. Skipping comment.", record.SdnId);
|
||||
continue;
|
||||
}
|
||||
|
||||
await _unitOfWork.Comments.Upsert(record);
|
||||
}
|
||||
|
||||
await _unitOfWork.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user