feat: implement cleaning up table

This commit is contained in:
Stevan Freeborn
2024-08-25 17:08:44 -05:00
parent a173220e51
commit c35bcdad90
10 changed files with 96 additions and 9 deletions
@@ -6,4 +6,5 @@ interface IDatabaseMaintainer
Task BuildAddressTableAsync();
Task BuiltAliasTableAsync();
Task BuildCommentTableAsync();
Task CleanupTablesAsync();
}
@@ -2,4 +2,5 @@ namespace SanctionsSearch.Worker.Interfaces;
interface ISdnRepository : IRepository<Sdn>
{
Task DeleteWhereAsync(Expression<Func<Sdn, bool>> predicate);
}
@@ -167,6 +167,27 @@ class DatabaseMaintainer(
_logger.LogInformation("Comment table built");
}
public async Task CleanupTablesAsync()
{
_logger.LogInformation("Cleaning up tables");
var result = await _ofacFileService.GetSdnFileAsync();
if (result.IsFailed)
{
_logger.LogError("Failed to get SDN file from OFAC.");
return;
}
using var stream = result.Value;
var records = GetRecordsFromStream<Sdn>(stream);
var sdnIds = records.Select(s => s.Id).ToList();
await _unitOfWork.Sdns.DeleteWhereAsync(s => sdnIds.Contains(s.Id) == false);
_logger.LogInformation("Tables cleaned up");
}
public void Dispose()
{
_csvReaders.ForEach(csv => csv.Dispose());
@@ -1,9 +1,13 @@
namespace SanctionsSearch.Worker.Persistence;
class EfUnitOfWork(DbContext context, ILoggerFactory loggerFactory) : IUnitOfWork, IAsyncDisposable, IDisposable
class EfUnitOfWork(
DbContext context,
ILogger<EfUnitOfWork> logger,
ILoggerFactory loggerFactory
) : IUnitOfWork, IAsyncDisposable, IDisposable
{
private readonly DbContext _context = context;
private readonly ILogger<EfUnitOfWork> _logger = loggerFactory.CreateLogger<EfUnitOfWork>();
private readonly ILogger<EfUnitOfWork> _logger = logger;
public ISdnRepository Sdns { get; } = new SdnRepository(context, loggerFactory.CreateLogger<SdnRepository>());
public IAddressRepository Addresses { get; } = new AddressRepository(context, loggerFactory.CreateLogger<AddressRepository>());
public IAliasRepository Aliases { get; } = new AliasRepository(context, loggerFactory.CreateLogger<AliasRepository>());
@@ -5,4 +5,8 @@ class SdnRepository(
ILogger<SdnRepository> logger
) : EfRepository<Sdn>(dbContext, logger), ISdnRepository
{
public Task DeleteWhereAsync(Expression<Func<Sdn, bool>> predicate)
{
return _context.Set<Sdn>().Where(predicate).ExecuteDeleteAsync();
}
}
@@ -52,6 +52,7 @@ public class DatabaseWorker(
await databaseMaintainer.BuildAddressTableAsync();
await databaseMaintainer.BuiltAliasTableAsync();
await databaseMaintainer.BuildCommentTableAsync();
await databaseMaintainer.CleanupTablesAsync();
_logger.LogInformation("Database updated");
}