2024-08-18 20:16:38 -05:00
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
|
|
|
|
|
namespace SanctionsSearch.Worker.Persistence;
|
|
|
|
|
|
|
|
|
|
class AppDbContext(IOptionsSnapshot<DbOptions> options) : DbContext, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly IOptionsSnapshot<DbOptions> _options = options;
|
|
|
|
|
private SqliteConnection? _connection;
|
|
|
|
|
public DbSet<Sdn> Sdns { get; set; } = default!;
|
|
|
|
|
public DbSet<Address> Addresses { get; set; } = default!;
|
|
|
|
|
public DbSet<Alias> Aliases { get; set; } = default!;
|
|
|
|
|
public DbSet<Comment> Comments { get; set; } = default!;
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
2024-08-18 20:34:07 -05:00
|
|
|
var folder = Assembly.GetExecutingAssembly().Location;
|
|
|
|
|
var path = Path.GetDirectoryName(folder);
|
|
|
|
|
var dataDir = Path.Combine(path!, "Data");
|
2024-08-18 20:16:38 -05:00
|
|
|
|
|
|
|
|
if (Directory.Exists(dataDir) == false)
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dataDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dbPath = Path.Combine(dataDir, _options.Value.DatabaseName);
|
|
|
|
|
|
|
|
|
|
_connection = new SqliteConnection($"Data Source={dbPath}");
|
|
|
|
|
_connection.Open();
|
|
|
|
|
|
|
|
|
|
optionsBuilder.UseSqlite(_connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_connection?.Dispose();
|
|
|
|
|
base.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|