This commit is contained in:
TheXamlGuy
2024-04-30 20:46:47 +01:00
parent 2a4194ee22
commit 81f266d8c4
22 changed files with 296 additions and 162 deletions
+35 -35
View File
@@ -1,41 +1,41 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Toolkit.Foundation;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.Hosting;
//using Toolkit.Foundation;
namespace Bitvault;
//namespace Bitvault;
public class OpenVaultStorageHandler(IHostEnvironment environment,
IDbContextFactory<VaultDbContext> dbContextFactory) : IHandler<Open<VaultStorage>, bool>
{
public async Task<bool> Handle(Open<VaultStorage> args, CancellationToken cancellationToken)
{
if (args.Value is VaultStorage vault)
{
if (vault.Name is { Length: > 0 } name && vault.Password is { Length: > 0 } password)
{
using VaultDbContext context = dbContextFactory.CreateDbContext();
var d = context.Database.GetDbConnection().ConnectionString;
context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, name)}.vault;Mode=ReadWriteCreate;Password={password}");
//public class OpenVaultStorageHandler(IHostEnvironment environment,
// IDbContextFactory<VaultDbContext> dbContextFactory) : IHandler<Open<VaultStorage>, bool>
//{
// public async Task<bool> Handle(Open<VaultStorage> args, CancellationToken cancellationToken)
// {
// if (args.Value is VaultStorage vault)
// {
// if (vault.Name is { Length: > 0 } name && vault.Password is { Length: > 0 } password)
// {
// using VaultDbContext context = dbContextFactory.CreateDbContext();
// var d = context.Database.GetDbConnection().ConnectionString;
// context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, name)}.vault;Mode=ReadWriteCreate;Password={password}");
bool isOpen = false;
await Task.Run(async () =>
{
try
{
await context.Database.OpenConnectionAsync();
isOpen = true;
}
catch
{
// We are ignoring this exception as it is either a go, or not.
}
// bool isOpen = false;
// await Task.Run(async () =>
// {
// try
// {
// await context.Database.OpenConnectionAsync();
// isOpen = true;
// }
// catch
// {
// // We are ignoring this exception as it is either a go, or not.
// }
}, cancellationToken);
// }, cancellationToken);
return isOpen;
}
}
// return isOpen;
// }
// }
return false;
}
}
// return false;
// }
//}