Files
Walleby/Wallet/WalletStoreFactory.cs
T
2024-06-29 19:39:34 +01:00

40 lines
1.3 KiB
C#

using Wallet.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Toolkit.Foundation;
namespace Wallet;
public class WalletStoreFactory(IDecoratorService<WalletConnection> connection,
IHostEnvironment environment,
IServiceProvider provider) :
IWalletStoreFactory
{
public async Task<bool> Create(string name,
SecurityKey key)
{
connection.Set(new WalletConnection($"Data Source={Path.Combine(environment.ContentRootPath, name)}" +
$".wallet;Mode=ReadWriteCreate;Pooling=true;Password={Convert.ToBase64String(key.DecryptedKey)}"));
IDbContextFactory<WalletContext> dbContextFactory = provider.GetRequiredService<IDbContextFactory<WalletContext>>();
using WalletContext context = await dbContextFactory.CreateDbContextAsync();
try
{
await Task.Run(async () =>
{
await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
await context.Database.CloseConnectionAsync().ConfigureAwait(false);
context.Database.SetConnectionString(null);
}).ConfigureAwait(false);
}
catch
{
return false;
}
return true;
}
}