diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index 430aa9f..1e9f4d4 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -1,6 +1,8 @@ using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Toolkit.Avalonia; @@ -17,7 +19,26 @@ public partial class App : Application public override async void OnFrameworkInitializationCompleted() { - IHost? host = DefaultBuilder.Create() + //var connectionString = new SqliteConnectionStringBuilder(@"Filename=C:\\Users\\dan_c\\source\\repos\\Bitvault\\Bitvault.Avalonia.Desktop\\bin\\Debug\\net8.0\\SQssLite.sql") + //{ + // Mode = SqliteOpenMode.ReadWriteCreate, + // Password = "Test123" + //}.ToString(); + + //var connection = new SqliteConnection(connectionString); + //connection.Open(); + + //var command = connection.CreateCommand(); + //command.CommandText = "SELECT quote($newPassword);"; + //command.Parameters.AddWithValue("$newPassword", "Test123"); + //var quotedNewPassword = (string)command.ExecuteScalar(); + + //command.CommandText = "PRAGMA rekey = " + quotedNewPassword; + //command.Parameters.Clear(); + //command.ExecuteNonQuery(); + + + IHost? host = DefaultHostBuilder.Create() .AddConfiguration(args => args.Name = "Personal", "Vault:*") .ConfigureServices((context, services) => @@ -25,19 +46,38 @@ public partial class App : Application services.AddAvalonia(); services.AddHandler(); - services.AddTransient(); - services.AddTransient(); - services.AddInitializer(); - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime) { services.AddTemplate("MainWindow"); } + services.AddTransient (provider => Component.Create(provider, args => + { + args.AddServices(services => + { + services.AddDbContextFactory(args => + { + args.UseSqlite(); + }); + + services.AddDbContextFactory(); + services.AddHandler(); + + services.AddTemplate(); + services.AddTemplate(); + services.AddTemplate(); + services.AddTemplate(); + + services.AddTemplate("Vault"); + services.AddTemplate("Lock"); + }); + })!); + services.AddSingleton(); - services.AddSingleton(); services.AddHandler(); + //services.AddInitializer(); + services.AddTemplate("Main"); services.AddTemplate(); diff --git a/Bitvault.Avalonia/Bitvault.Avalonia.csproj b/Bitvault.Avalonia/Bitvault.Avalonia.csproj index f12073e..6c3b0b5 100644 --- a/Bitvault.Avalonia/Bitvault.Avalonia.csproj +++ b/Bitvault.Avalonia/Bitvault.Avalonia.csproj @@ -17,6 +17,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Bitvault.Avalonia/VaultComponent.cs b/Bitvault.Avalonia/VaultComponent.cs deleted file mode 100644 index aaf7895..0000000 --- a/Bitvault.Avalonia/VaultComponent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Toolkit.Foundation; - -namespace Bitvault.Avalonia; - -public class VaultComponent : - IVaultComponent -{ - public IComponentBuilder Create() => - ComponentBuilder.Create() - .AddServices(services => - { - // services.AddInitializer(); - - services.AddTemplate(); - services.AddTemplate(); - services.AddTemplate(); - services.AddTemplate(); - - services.AddTemplate("Vault"); - services.AddTemplate("Lock"); - }); -} \ No newline at end of file diff --git a/Bitvault/Bitvault.csproj b/Bitvault/Bitvault.csproj index b9f1c41..5e64ceb 100644 --- a/Bitvault/Bitvault.csproj +++ b/Bitvault/Bitvault.csproj @@ -6,8 +6,17 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Bitvault/CreateVaultViewModel.cs b/Bitvault/CreateVaultViewModel.cs index e4c7836..d772ea0 100644 --- a/Bitvault/CreateVaultViewModel.cs +++ b/Bitvault/CreateVaultViewModel.cs @@ -19,6 +19,6 @@ public partial class CreateVaultViewModel(IServiceProvider provider, public async Task Confirm() { - return await Mediator.Handle, bool>(Create.As(new Vault(Name))); + return await Mediator.Handle, bool>(Create.As(new Vault(Name, ""))); } } \ No newline at end of file diff --git a/Bitvault/IVaultComponent.cs b/Bitvault/IVaultComponent.cs index fe61a2d..807f210 100644 --- a/Bitvault/IVaultComponent.cs +++ b/Bitvault/IVaultComponent.cs @@ -2,4 +2,9 @@ namespace Bitvault; -public interface IVaultComponent : IComponent; \ No newline at end of file +public interface IVaultComponent : IComponent; + +public class VaultComponent(IComponentBuilder builder) : Component(builder), + IVaultComponent +{ +} \ No newline at end of file diff --git a/Bitvault/IVaultFactory.cs b/Bitvault/IVaultFactory.cs index 0404a85..75dcc2b 100644 --- a/Bitvault/IVaultFactory.cs +++ b/Bitvault/IVaultFactory.cs @@ -4,6 +4,8 @@ namespace Bitvault { public interface IVaultFactory { - IComponentHost? Create(string name, VaultConfiguration configuration); + IComponentHost? Create(string name, + string password, + VaultConfiguration configuration); } } \ No newline at end of file diff --git a/Bitvault/Locker.cs b/Bitvault/Locker.cs new file mode 100644 index 0000000..85d722e --- /dev/null +++ b/Bitvault/Locker.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace Bitvault; + +public record Locker +{ + [Key] + public int Id { get; set; } + + public string? Name { get; set; } +} diff --git a/Bitvault/Vault.cs b/Bitvault/Vault.cs index a07a23c..058004b 100644 --- a/Bitvault/Vault.cs +++ b/Bitvault/Vault.cs @@ -1,3 +1,3 @@ namespace Bitvault; -public record Vault(string Name); \ No newline at end of file +public record Vault(string Name, string Password); \ No newline at end of file diff --git a/Bitvault/VaultDbContext.cs b/Bitvault/VaultDbContext.cs new file mode 100644 index 0000000..505a894 --- /dev/null +++ b/Bitvault/VaultDbContext.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace Bitvault; + +public class VaultDbContext: DbContext +{ + public DbSet Lockers { get; set; } + + public VaultDbContext(DbContextOptions options): base(options) + { + + } +} diff --git a/Bitvault/VaultFactory.cs b/Bitvault/VaultFactory.cs deleted file mode 100644 index 8443910..0000000 --- a/Bitvault/VaultFactory.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Toolkit.Foundation; - -namespace Bitvault; - -public class VaultFactory(IServiceProvider provider, - IProxyServiceCollection proxy, - IComponentScopeCollection scopes, - IVaultHostCollection vaults) : IVaultFactory -{ - public IComponentHost? Create(string name, - VaultConfiguration configuration) - { - if (provider.GetRequiredService() is IVaultComponent component) - { - IComponentBuilder builder = component.Create(); - builder.AddServices(services => - { - services.AddTransient(_ => - provider.GetRequiredService>()); - - services.AddTransient(_ => - provider.GetRequiredService>()); - - services.AddScoped(_ => - provider.GetRequiredService()); - - services.AddScoped(_ => - provider.GetRequiredService()); - - services.AddScoped(_ => - provider.GetRequiredService()); - - services.AddTransient(_ => - provider.GetRequiredService()); - - services.AddRange(proxy.Services); - services.AddSingleton(new ComponentScope(name)); - }); - - builder.AddConfiguration(name, configuration); - IComponentHost host = builder.Build(); - - scopes.Add(new ComponentScopeDescriptor(name, - host.Services.GetRequiredService())); - - vaults.Add(host); - return host; - } - - return default; - } -} \ No newline at end of file diff --git a/Bitvault/VaultHandler.cs b/Bitvault/VaultHandler.cs index d1cda3d..dbefb36 100644 --- a/Bitvault/VaultHandler.cs +++ b/Bitvault/VaultHandler.cs @@ -1,37 +1,29 @@ -using LiteDB; +using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; -public class VaultStorageHandler : - IHandler, bool> -{ - public Task Handle(Create args, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } -} -public class DatabaseFactory -{ - -} - -public record VaultStorage(string Name); - -public class VaultHandler(IVaultFactory factory) : +public class VaultHandler(IComponentFactory factory) : IHandler, bool> { - public async Task Handle(Create args, CancellationToken cancellationToken) + public async Task Handle(Create args, + CancellationToken cancellationToken) { if (args.Value is Vault vault) { - if (factory.Create($"Vault:{vault.Name}", new VaultConfiguration { Name = vault.Name }) is IComponentHost host) + if (factory.Create($"Vault:{vault.Name}", new VaultConfiguration { Name = vault.Name }) is IComponentHost host) { - await host.StartAsync(cancellationToken); + if (host.Services.GetRequiredService() is IMediator mediator) + { + if (await mediator.Handle, bool>(Create.As(new VaultStorage(vault.Name, vault.Password)), cancellationToken)) + { + await host.StartAsync(cancellationToken); + } + } } } - return true; + return false; } } \ No newline at end of file diff --git a/Bitvault/VaultStorage.cs b/Bitvault/VaultStorage.cs new file mode 100644 index 0000000..8e3f523 --- /dev/null +++ b/Bitvault/VaultStorage.cs @@ -0,0 +1,3 @@ +namespace Bitvault; + +public record VaultStorage(string Name, string Password); diff --git a/Bitvault/VaultStorageHandler.cs b/Bitvault/VaultStorageHandler.cs new file mode 100644 index 0000000..e1e1255 --- /dev/null +++ b/Bitvault/VaultStorageHandler.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using Toolkit.Foundation; + +namespace Bitvault; + +public class VaultStorageHandler(IHostEnvironment environment, + IDbContextFactory dbContextFactory) : + IHandler, bool> +{ + public async Task Handle(Create args, CancellationToken cancellationToken) + { + if (args.Value is VaultStorage storage) + { + using VaultDbContext context = dbContextFactory.CreateDbContext(); + await Task.Run(async () => + { + context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, storage.Name)}.vault;Mode=ReadWriteCreate;Password={storage.Password}"); + await context.Database.EnsureCreatedAsync(cancellationToken); + }, cancellationToken); + + return true; + } + + return false; + } +} diff --git a/Bitvault/VaultsInitializer.cs b/Bitvault/VaultsInitializer.cs index 746cec7..c454762 100644 --- a/Bitvault/VaultsInitializer.cs +++ b/Bitvault/VaultsInitializer.cs @@ -9,10 +9,10 @@ public class VaultsInitializer(IEnumerable configuration in configurations) { - if (factory.Create(configuration.Section, configuration.Value) is IComponentHost host) - { - await host.StartAsync(); - } + //if (factory.Create(configuration.Section, configuration.Value) is IComponentHost host) + //{ + // await host.StartAsync(); + //} } } } \ No newline at end of file