bunch of fixes

This commit is contained in:
TheXamlGuy
2024-05-02 20:58:12 +01:00
parent e98e622003
commit 4b05abad9b
23 changed files with 115 additions and 192 deletions
+28 -6
View File
@@ -1,17 +1,39 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Toolkit.Foundation;
namespace Bitvault;
public class VaultStorage(IHostEnvironment environment,
IDbContextFactory<VaultDbContext> dbContextFactory) :
public class VaultStorage(IContainer<VaultStorageConnection> connection,
IHostEnvironment environment,
IServiceProvider provider) :
IVaultStorage
{
public bool Create(string name, VaultKey key)
public async Task<bool> CreateAsync(string name,
VaultKey key)
{
using VaultDbContext context = dbContextFactory.CreateDbContext();
context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, name)}" +
$".vault;Mode=ReadWriteCreate;Password={Convert.ToBase64String(key.Private)}");
connection.Set(new VaultStorageConnection($"Data Source={Path.Combine(environment.ContentRootPath, name)}" +
$".vault;Mode=ReadWriteCreate;Pooling=false;Password={Convert.ToBase64String(key.Private)}"));
IDbContextFactory<VaultDbContext> dbContextFactory = provider.GetRequiredService<IDbContextFactory<VaultDbContext>>();
using VaultDbContext 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;
}