Files
Walleby/Wallet/WalletConnectionFactory.cs
T
TheXamlGuy 9daebc7ba2 Codemaid
2024-07-17 21:43:58 +01:00

35 lines
1011 B
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Wallet.Data;
namespace Wallet;
public class WalletConnectionFactory(IHostEnvironment environment) :
IWalletConnectionFactory
{
public async Task<WalletConnection?> Create(string name, string key)
{
string databaseFile = $"{Path.Combine(environment.ContentRootPath, name)}.wallet";
if (File.Exists(databaseFile))
{
try
{
return await Task.Run(async () =>
{
WalletConnection connection = new($"Data Source={databaseFile};Mode=ReadWriteCreate;Pooling=true;Password={key}");
using WalletContext context = new(connection);
await context.Database.OpenConnectionAsync().ConfigureAwait(false);
return connection;
});
}
catch
{
return null;
}
}
return null;
}
}