diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index 3a14af1..e99ed39 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -87,11 +87,12 @@ public partial class App : Application if (provider.GetRequiredService>() is IDecoratorService connection) { - args.UseSqlite($"{connection.Value}"); + args.UseSqlite($"{connection.Service}"); } }); services.AddHandler(); + services.AddHandler(); services.AddHandler(); services.AddHandler(); services.AddHandler(); diff --git a/Bitvault.Avalonia/ItemHeaderView.axaml b/Bitvault.Avalonia/ItemHeaderView.axaml index 0c809d3..c6af953 100644 --- a/Bitvault.Avalonia/ItemHeaderView.axaml +++ b/Bitvault.Avalonia/ItemHeaderView.axaml @@ -17,36 +17,13 @@ + + - - - - - - - - + + + + + + - + diff --git a/Bitvault.Data/BlobEntry.cs b/Bitvault.Data/BlobEntry.cs index 5f93d14..9b8651e 100644 --- a/Bitvault.Data/BlobEntry.cs +++ b/Bitvault.Data/BlobEntry.cs @@ -13,5 +13,5 @@ public record BlobEntry [Key] public int Id { get; set; } - public DateTimeOffset DateTime { get; set; } + public DateTime DateTime { get; set; } } \ No newline at end of file diff --git a/Bitvault.Data/ItemEntry.cs b/Bitvault.Data/ItemEntry.cs index b502dc5..88c3d8b 100644 --- a/Bitvault.Data/ItemEntry.cs +++ b/Bitvault.Data/ItemEntry.cs @@ -17,7 +17,7 @@ public record ItemEntry public required string Category { get; set; } - public ICollection? Tags { get; } + public ICollection Tags { get; set; } = new List(); - public ICollection? Blobs { get; } + public ICollection Blobs { get; set; } = new List(); } \ No newline at end of file diff --git a/Bitvault/ArchiveItemHandler.cs b/Bitvault/ArchiveItemHandler.cs index 0bfab99..3fdf807 100644 --- a/Bitvault/ArchiveItemHandler.cs +++ b/Bitvault/ArchiveItemHandler.cs @@ -11,7 +11,7 @@ public class ArchiveItemHandler(IDecoratorService> decorato { try { - if (decoratorService.Value is Item<(Guid, string)> item) + if (decoratorService.Service is Item<(Guid, string)> item) { if (cache.Contains(item)) { diff --git a/Bitvault/ConfirmCreateItemHandler.cs b/Bitvault/ConfirmCreateItemHandler.cs index 5166072..04098b2 100644 --- a/Bitvault/ConfirmCreateItemHandler.cs +++ b/Bitvault/ConfirmCreateItemHandler.cs @@ -3,13 +3,13 @@ namespace Bitvault; public class ConfirmCreateItemHandler(IMediator mediator, - IDecoratorService decoratorItemConfiguration, + IDecoratorService itemConfigurationDecorator, IPublisher publisher) : INotificationHandler> { public async Task Handle(ConfirmEventArgs args) { - if (decoratorItemConfiguration.Value is ItemConfiguration configuration) + if (itemConfigurationDecorator.Service is ItemConfiguration configuration) { string? name = await mediator.Handle, string>(Confirm.As()); if (name is not null) diff --git a/Bitvault/ConfirmUpdateItemHandler.cs b/Bitvault/ConfirmUpdateItemHandler.cs index 65e45b7..8faccb9 100644 --- a/Bitvault/ConfirmUpdateItemHandler.cs +++ b/Bitvault/ConfirmUpdateItemHandler.cs @@ -2,32 +2,33 @@ namespace Bitvault; -public class ConfirmUpdateItemHandler(IDecoratorService> decoratorItem, - IDecoratorService decoratorItemConfiguration, +public class ConfirmUpdateItemHandler(IDecoratorService> itemDecorator, + IDecoratorService itemConfigurationDecorator, IMediator mediator, IPublisher publisher) : INotificationHandler> { public async Task Handle(ConfirmEventArgs args) { - string? name = await mediator.Handle, - string>(Confirm.As()); - - if (name is not null) + if (itemDecorator?.Service is Item<(Guid, string)> item && + itemConfigurationDecorator.Service is ItemConfiguration configuration) { - var dd = decoratorItemConfiguration; - publisher.Publish(Notify.As(new ItemHeader(name))); - if (decoratorItem?.Value is Item<(Guid, string)> item) + string? name = await mediator.Handle, + string>(Confirm.As()); + + if (name is not null) { + publisher.Publish(Notify.As(new ItemHeader(name))); + (Guid id, string _) = item.Value; Item<(Guid, string)> newItem = new((id, name)); publisher.Publish(Modified.As(item, newItem)); - decoratorItem.Set(newItem); + itemDecorator.Set(newItem); - await mediator.Handle, bool>(new UpdateEventArgs<(Guid, string, - ItemConfiguration)>((id, name, new ItemConfiguration()))); + await mediator.Handle>, bool>(new UpdateEventArgs>(new Item<(Guid, string, ItemConfiguration)>((id, name, configuration)))); } } } diff --git a/Bitvault/CreateItemHandler.cs b/Bitvault/CreateItemHandler.cs index 179f354..afe89f6 100644 --- a/Bitvault/CreateItemHandler.cs +++ b/Bitvault/CreateItemHandler.cs @@ -1,6 +1,8 @@ using Bitvault.Data; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; +using System.Text; +using System.Text.Json; using Toolkit.Foundation; namespace Bitvault; @@ -15,15 +17,24 @@ public class CreateItemHandler(IDbContextFactory dbContextFactory { try { - using LockerContext context = dbContextFactory.CreateDbContext(); - EntityEntry? result = null; - - await Task.Run(async () => + string content = JsonSerializer.Serialize(configuration); + ItemEntry itemEntry = new() { - result = await context.AddAsync(new ItemEntry { Id = id, Name = name, Category = category }, cancellationToken); - await context.SaveChangesAsync(cancellationToken); + Id = id, + Name = name, + Category = category + }; - }, cancellationToken); + itemEntry.Blobs.Add(new() + { + Data = Encoding.UTF8.GetBytes(content), + DateTime = DateTime.Now, + Type = 0, + }); + + using LockerContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); + EntityEntry? result = await context.AddAsync(itemEntry, cancellationToken); + await context.SaveChangesAsync(cancellationToken); if (result is not null) { diff --git a/Bitvault/CreateLockerHandler.cs b/Bitvault/CreateLockerHandler.cs index dedcd02..6dd0672 100644 --- a/Bitvault/CreateLockerHandler.cs +++ b/Bitvault/CreateLockerHandler.cs @@ -7,36 +7,41 @@ namespace Bitvault; public class CreateLockerHandler(ILockerFactory componentFactory, IPublisher publisher) : - IHandler, bool> + IHandler>, bool> { - public async Task Handle(CreateEventArgs args, + public async Task Handle(CreateEventArgs> args, CancellationToken cancellationToken) { - if (args.Value is Locker locker && locker.Name is { Length: > 0 } name && - locker.Password is { Length: > 0 } password) + if (args.Value is Locker <(string, string)> locker) { - if (componentFactory.Create(name) is IComponentHost host) + if (locker.Value is (string name, string password) && + name is { Length: > 0 } && + password is { Length: > 0 }) { - ISecurityKeyFactory keyVaultFactory = host.Services.GetRequiredService(); - IDecoratorService secureKeyStore = host.Services.GetRequiredService>(); - ILockerStorageFactory lockerStorageFactory = host.Services.GetRequiredService(); - - if (keyVaultFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key) + if (componentFactory.Create(name) is IComponentHost host) { - secureKeyStore.Set(key); + ISecurityKeyFactory keyVaultFactory = host.Services.GetRequiredService(); + IDecoratorService secureKeyStore = host.Services.GetRequiredService>(); + ILockerStorageFactory lockerStorageFactory = host.Services.GetRequiredService(); - if (await lockerStorageFactory.Create(name, key)) + if (keyVaultFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key) { - IWritableConfiguration configuration = - host.Services.GetRequiredService>(); + secureKeyStore.Set(key); - configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}"); - host.Start(); + if (await lockerStorageFactory.Create(name, key)) + { + IWritableConfiguration configuration = + host.Services.GetRequiredService>(); - publisher.Publish(Activated.As(host), cancellationToken); - return true; + configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}"); + host.Start(); + + publisher.Publish(Activated.As(host), cancellationToken); + return true; + } } } + } } diff --git a/Bitvault/CreateLockerViewModel.cs b/Bitvault/CreateLockerViewModel.cs index 5eeca87..8de7472 100644 --- a/Bitvault/CreateLockerViewModel.cs +++ b/Bitvault/CreateLockerViewModel.cs @@ -22,5 +22,5 @@ public partial class CreateLockerViewModel(IServiceProvider provider, private string password; public async Task Confirm() => - await Mediator.Handle, bool>(Create.As(new Locker(Name, Password))); + await Mediator.Handle>, bool>(Create.As(new Locker<(string, string)>((Name, Password)))); } \ No newline at end of file diff --git a/Bitvault/FavouriteItemHandler.cs b/Bitvault/FavouriteItemHandler.cs index e081d48..a402b6c 100644 --- a/Bitvault/FavouriteItemHandler.cs +++ b/Bitvault/FavouriteItemHandler.cs @@ -10,7 +10,7 @@ public class FavouriteItemHandler(IDecoratorService> decora { try { - if (decoratorService.Value is Item<(Guid, string)> item) + if (decoratorService.Service is Item<(Guid, string)> item) { (Guid id, string name) = item.Value; await mediator.Handle, bool>(new UpdateEventArgs<(Guid, int)>((id, 1))); diff --git a/Bitvault/ItemEntryViewModel.cs b/Bitvault/ItemEntryViewModel.cs index 8450b25..4b7fb76 100644 --- a/Bitvault/ItemEntryViewModel.cs +++ b/Bitvault/ItemEntryViewModel.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using CommunityToolkit.Mvvm.ComponentModel; using Toolkit.Foundation; namespace Bitvault; @@ -13,7 +13,33 @@ public partial class ItemEntryViewModel(IServiceProvider provider, string? key = default, object? value = default) : Observable(provider, factory, mediator, publisher, subscriber, disposer, key, value), - IItemEntryViewModel + IItemEntryViewModel, + INotificationHandler>, + INotificationHandler>, + INotificationHandler> { - protected override void OnValueChanged() => configuration.Value = Value; + [ObservableProperty] + private ItemState state = ItemState.Read; + + protected override void OnValueChanged() => + configuration.Value = Value; + + public Task Handle(UpdateEventArgs args) => + Task.FromResult(State = ItemState.Write); + + public Task Handle(CancelEventArgs args) + { + Revert(); + + State = ItemState.Read; + return Task.CompletedTask; + } + + public Task Handle(ConfirmEventArgs args) + { + Commit(); + + State = ItemState.Read; + return Task.CompletedTask; + } } diff --git a/Bitvault/ItemViewModel.cs b/Bitvault/ItemViewModel.cs index d0bdfa2..412e947 100644 --- a/Bitvault/ItemViewModel.cs +++ b/Bitvault/ItemViewModel.cs @@ -118,7 +118,7 @@ public partial class ItemViewModel : { Publisher.Publish(Notify.As(Factory.Create(new List { - Factory.Create(State), + Factory.Create(), Factory.Create(), }))); } diff --git a/Bitvault/Locker.cs b/Bitvault/Locker.cs index ad30de8..a8590c2 100644 --- a/Bitvault/Locker.cs +++ b/Bitvault/Locker.cs @@ -1,23 +1,5 @@ namespace Bitvault; -public record Locker -{ - public Locker(string name, string password) - { - Name = name; - Password = password; - } +public record Locker(TValue Value); - public Locker(string password) - { - Password = password; - } - - public Locker() - { - } - - public string Name { get; } = ""; - - public string? Password { get; } = ""; -} \ No newline at end of file +public record Locker; \ No newline at end of file diff --git a/Bitvault/OpenLockerHandler.cs b/Bitvault/OpenLockerHandler.cs index 92bb135..01b2b0f 100644 --- a/Bitvault/OpenLockerHandler.cs +++ b/Bitvault/OpenLockerHandler.cs @@ -6,12 +6,14 @@ namespace Bitvault; public class OpenLockerHandler(IConfigurationDescriptor descriptor, ISecurityKeyFactory securityKeyFactory, ILockerStorageFactory lockerStorageFactory) : - IHandler, bool> + IHandler>, bool> { - public async Task Handle(ActivateEventArgs args, + public async Task Handle(ActivateEventArgs> args, CancellationToken cancellationToken) { - if (args.Value is Locker locker && descriptor.Name is { Length: > 0 } name && locker.Password is { Length: > 0 } password) + if (args.Value is Locker locker && + descriptor.Name is { Length: > 0 } name && + locker.Value is { Length: > 0 } password) { LockerConfiguration configuration = descriptor.Value; if (configuration.Key?.Split(':') is { Length: >= 2 } keyPart) diff --git a/Bitvault/OpenLockerViewModel.cs b/Bitvault/OpenLockerViewModel.cs index ea29cdd..1d38afa 100644 --- a/Bitvault/OpenLockerViewModel.cs +++ b/Bitvault/OpenLockerViewModel.cs @@ -24,7 +24,7 @@ public partial class OpenLockerViewModel(IServiceProvider provider, { if (Password is { Length: > 0 }) { - if (await Mediator.Handle, bool>(Activate.As(new Locker(Password)))) + if (await Mediator.Handle>, bool>(Activate.As(new Locker(Password)))) { Publisher.Publish(Opened.As()); } diff --git a/Bitvault/QueryItemConfiguration.cs b/Bitvault/QueryItemConfiguration.cs deleted file mode 100644 index 98628ee..0000000 --- a/Bitvault/QueryItemConfiguration.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Bitvault; - -public record QueryItemConfiguration -{ - public int Id { get; set; } -} \ No newline at end of file diff --git a/Bitvault/QueryLockerConfiguration.cs b/Bitvault/QueryLockerConfiguration.cs deleted file mode 100644 index 17bf81a..0000000 --- a/Bitvault/QueryLockerConfiguration.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Bitvault; - -public record QueryLockerConfiguration -{ - public string? Filter { get; set; } - - public string? Query { get; set; } -} \ No newline at end of file diff --git a/Bitvault/QueryLockerHandler.cs b/Bitvault/QueryLockerHandler.cs index 215795e..736c669 100644 --- a/Bitvault/QueryLockerHandler.cs +++ b/Bitvault/QueryLockerHandler.cs @@ -6,52 +6,50 @@ using Toolkit.Foundation; namespace Bitvault; public class QueryLockerHandler(IDbContextFactory dbContextFactory) : - IHandler, IReadOnlyCollection<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)>> + IHandler>, IReadOnlyCollection<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)>> { - public async Task> Handle(RequestEventArgs args, - CancellationToken cancellationToken) + public async Task> + Handle(QueryEventArgs> args,CancellationToken cancellationToken) { List<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)> items = []; - - if (args.Value is QueryLockerConfiguration queryConfiguration) + if (args.Value is Locker<(string, string)> locker) { + (string filter, string text) = locker.Value; + ExpressionStarter predicate = PredicateBuilder.New(true); - if (queryConfiguration.Filter == "All") + if (filter == "All") { predicate = predicate.And(x => x.State != 2); } - if (queryConfiguration.Filter == "Starred") + if (filter == "Starred") { predicate = predicate.And(x => x.State != 2 && x.State == 1); } - if (queryConfiguration.Filter == "Archive") + if (filter == "Archive") { predicate = predicate.And(x => x.State == 2); } - if (queryConfiguration.Query is { Length: > 0 } query) + if (text is { Length: > 0 }) { - predicate = predicate.And(x => EF.Functions.Like(x.Name, $"%{query}%")); + predicate = predicate.And(x => EF.Functions.Like(x.Name, $"%{text}%")); } - var results = await Task.Run(async () => - { - using LockerContext context = dbContextFactory.CreateDbContext(); - return await context.Set() - .Where(predicate) - .Select(x => new - { - x.Id, - x.Name, - x.Category, - Favourite = x.State == 1, - Archived = x.State == 2 - }).ToListAsync(); - }); + using LockerContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); + var results = await context.Set() + .Where(predicate) + .Select(x => new + { + x.Id, + x.Name, + x.Category, + Favourite = x.State == 1, + Archived = x.State == 2 + }).ToListAsync(cancellationToken: cancellationToken); foreach (var result in results.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase)) { diff --git a/Bitvault/RequestItemHandler.cs b/Bitvault/RequestItemHandler.cs new file mode 100644 index 0000000..c41fec2 --- /dev/null +++ b/Bitvault/RequestItemHandler.cs @@ -0,0 +1,55 @@ +using Bitvault.Data; +using Microsoft.EntityFrameworkCore; +using System.Text.Json; +using Toolkit.Foundation; + +namespace Bitvault; + +public class RequestItemHandler(IDbContextFactory dbContextFactory) : + IHandler>, (Guid, string, string?, string, ItemConfiguration?)> +{ + public async Task<(Guid, string, string?, string, ItemConfiguration?)> Handle(RequestEventArgs> args, + CancellationToken cancellationToken) + { + if (args.Value is Item item) + { + Guid id = item.Value; + + using LockerContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); + var result = await context.Set() + .Where(x => x.Id == id) + .Select(x => new + { + x.Id, + x.Name, + x.Description, + x.Category, + Blob = x.Blobs + .Where(b => b.Type == 0) + .OrderByDescending(b => b.DateTime) + .FirstOrDefault() + }) + .FirstOrDefaultAsync(cancellationToken); + + if (result is not null) + { + ItemConfiguration? configuration = null; + if (result.Blob is BlobEntry blob && blob.Data is { Length: > 0 } data) + { + try + { + configuration = JsonSerializer.Deserialize(data); + } + catch + { + + } + } + + return (result.Id, result.Name, result.Description, result.Category, configuration); + } + } + + return default; + } +} diff --git a/Bitvault/SynchronizeItemContentViewModelHandler.cs b/Bitvault/SynchronizeItemContentViewModelHandler.cs index 1030faf..9d715c2 100644 --- a/Bitvault/SynchronizeItemContentViewModelHandler.cs +++ b/Bitvault/SynchronizeItemContentViewModelHandler.cs @@ -1,19 +1,52 @@ -using Toolkit.Foundation; +using System.Reflection; +using Toolkit.Foundation; namespace Bitvault; -public class SynchronizeItemContentViewModelHandler(IMediator mediator, +public class SynchronizeItemContentViewModelHandler(IDecoratorService> itemDecorator, + IDecoratorService itemConfigurationDecorator, + IMediator mediator, IServiceFactory serviceFactory, IPublisher publisher) : - INotificationHandler> + INotificationHandler> { - public Task Handle(SynchronizeEventArgs args) + public async Task Handle(SynchronizeEventArgs args) { - //wModel>(false) is ItemHeaderViewModel viewModel) - //{ - // publisher.Publish(Create.As(viewModel), nameof(ItemViewModel)); - //} + if (itemDecorator.Service is Item<(Guid, string)> item) + { + if (item.Value is (Guid Id, _)) + { + (_, _, _, _, ItemConfiguration? configuration) = await mediator.Handle>, (Guid, string, string?, string, + ItemConfiguration?)>(Request.As(new Item(Id))); - return Task.CompletedTask; + if (configuration is not null) + { + itemConfigurationDecorator.Set(configuration); + foreach (ItemSectionConfiguration configurationSection in configuration.Sections) + { + string id = $"{nameof(ItemSection)}:{Guid.NewGuid()}"; + if (serviceFactory.Create(id) + is ItemSectionViewModel sectionViewModel) + { + publisher.Publish(Create.As(sectionViewModel), nameof(ItemContentViewModel)); + foreach (IItemEntryConfiguration entryConfiguration in configurationSection.Entries) + { + Type messageType = typeof(CreateEventArgs<>).MakeGenericType(entryConfiguration.GetType()); + ConstructorInfo? constructor = messageType.GetConstructor([entryConfiguration.GetType(), typeof(object[])]); + + if (constructor?.Invoke(new object[] { entryConfiguration, new object[] { sectionViewModel } }) is object message) + { + if (await mediator.Handle(message, + entryConfiguration.GetType().Name) is IItemEntryViewModel entryViewModel) + { + publisher.Publish(Create.As(entryViewModel), id); + } + } + } + } + } + } + } + } } } diff --git a/Bitvault/SynchronizeItemViewModelHandler.cs b/Bitvault/SynchronizeItemViewModelHandler.cs index cb342de..13e987c 100644 --- a/Bitvault/SynchronizeItemViewModelHandler.cs +++ b/Bitvault/SynchronizeItemViewModelHandler.cs @@ -17,12 +17,11 @@ public class SynchronizeItemViewModelHandler(IMediator mediator, cache.Clear(); bool selected = true; - if (await mediator.Handle, - IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)>>(Request.As(new QueryLockerConfiguration - { - Filter = configuration.Filter, - Query = configuration.Query - })) is IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)> results) + IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)>? results = + await mediator.Handle>, + IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)>>(Query.As(new Locker<(string?, string?)>((configuration.Filter, configuration.Query)))); + + if (results is not null) { foreach ((Guid Id, string Name, string Category, bool Favourite, bool Archived) in results) { diff --git a/Bitvault/UnarchiveItemHandler.cs b/Bitvault/UnarchiveItemHandler.cs index db78507..8a2236c 100644 --- a/Bitvault/UnarchiveItemHandler.cs +++ b/Bitvault/UnarchiveItemHandler.cs @@ -1,5 +1,6 @@ using Bitvault.Data; using Microsoft.EntityFrameworkCore; +using System.Threading; using Toolkit.Foundation; namespace Bitvault; @@ -12,19 +13,16 @@ public class UnarchiveItemHandler(IDecoratorService> decora { try { - if (decoratorService.Value is Item<(Guid, string)> item) + if (decoratorService.Service is Item<(Guid, string)> item) { (Guid id, string name) = item.Value; - await Task.Run(async () => - { - using LockerContext context = await dbContextFactory.CreateDbContextAsync(); - if (await context.FindAsync(id) is ItemEntry result) - { - result.State = 0; - await context.SaveChangesAsync(); - } - }); + using LockerContext context = await dbContextFactory.CreateDbContextAsync(); + if (await context.FindAsync(id) is ItemEntry result) + { + result.State = 0; + await context.SaveChangesAsync(); + } } } catch diff --git a/Bitvault/UnfavouriteItemHandler.cs b/Bitvault/UnfavouriteItemHandler.cs index cd04389..3fa44d5 100644 --- a/Bitvault/UnfavouriteItemHandler.cs +++ b/Bitvault/UnfavouriteItemHandler.cs @@ -9,7 +9,7 @@ public class UnfavouriteItemHandler(IDecoratorService> deco { try { - if (decoratorService.Value is Item<(Guid, string)> item) + if (decoratorService.Service is Item<(Guid, string)> item) { (Guid id, string name) = item.Value; await mediator.Handle, bool>(new UpdateEventArgs<(Guid, int)>((id, 0))); diff --git a/Bitvault/UpdateItemHander.cs b/Bitvault/UpdateItemHander.cs index 6dc36f8..dbb9048 100644 --- a/Bitvault/UpdateItemHander.cs +++ b/Bitvault/UpdateItemHander.cs @@ -1,31 +1,39 @@ using Bitvault.Data; using Microsoft.EntityFrameworkCore; +using System.Text.Json; +using System.Text; using Toolkit.Foundation; namespace Bitvault; public class UpdateItemHander(IDbContextFactory dbContextFactory) : - IHandler, bool> + IHandler>, bool> { - public async Task Handle(UpdateEventArgs<(Guid, string, ItemConfiguration)> args, + public async Task Handle(UpdateEventArgs> args, CancellationToken cancellationToken) { - if (args.Value is (Guid id, string name, ItemConfiguration configuration)) + if (args.Value is Item<(Guid, string, ItemConfiguration)> item) { + (Guid id, string name, ItemConfiguration configuration) = item.Value; + try { - using LockerContext context = dbContextFactory.CreateDbContext(); - ItemEntry? result = null; + using LockerContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); + ItemEntry? result = result = await context.Set().FindAsync([id], cancellationToken); - await Task.Run(async () => + if (result is not null) { - result = await context.Set().FindAsync(id); - if (result is not null) + string content = JsonSerializer.Serialize(configuration); + result.Blobs.Add(new() { - result.Name = name; - await context.SaveChangesAsync(cancellationToken); - } - }, cancellationToken); + Data = Encoding.UTF8.GetBytes(content), + DateTime = DateTime.Now, + Type = 0, + }); + + result.Name = name; + await context.SaveChangesAsync(cancellationToken); + } if (result is not null) { diff --git a/Bitvault/UpdateItemStateHandler.cs b/Bitvault/UpdateItemStateHandler.cs index 42845ea..612f965 100644 --- a/Bitvault/UpdateItemStateHandler.cs +++ b/Bitvault/UpdateItemStateHandler.cs @@ -14,7 +14,7 @@ public class UpdateItemStateHandler(IDbContextFactory dbContextFa { await Task.Run(async () => { - using LockerContext context = await dbContextFactory.CreateDbContextAsync(); + using LockerContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); if (await context.FindAsync(id) is ItemEntry result) { result.State = state;