using Bitvault.Data; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.Extensions.Configuration; using Toolkit.Foundation; namespace Bitvault; public class ArchiveItemHandler(IValueStore valueStore, IDbContextFactory dbContextFactory) : INotificationHandler> { public async Task Handle(ArchiveEventArgs args) { try { if (valueStore.Value is Item item) { await Task.Run(async () => { using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync(); if (await context.FindAsync(item.Id) is ItemEntry result) { result.State = 3; await context.SaveChangesAsync(); } }); } } catch { } } } public class CreateItemHandler(IDbContextFactory dbContextFactory, IPublisher publisher) : IHandler, bool> { public async Task Handle(CreateEventArgs args, CancellationToken cancellationToken) { if (args.Value is ItemConfiguration configuration) { try { using ContainerDbContext context = dbContextFactory.CreateDbContext(); EntityEntry? result = null; await Task.Run(async () => { result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken); await context.SaveChangesAsync(cancellationToken); }, cancellationToken); if (result is not null) { Item item = new() { Id = result.Entity.Id, Name = configuration.Name }; publisher.Publish(Activated.As(item), cancellationToken); return true; } } catch { } } return false; } }