From 2842fc71087c5ac42c04b97d930456db04a4792d Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 12 May 2024 20:21:55 +0100 Subject: [PATCH] An attempt to send messages out of the scope --- Bitvault.Avalonia/App.axaml.cs | 1 + Bitvault.Data/{Document.cs => BlobEntry.cs} | 6 ++++-- Bitvault.Data/ContainerDbContext.cs | 12 +++++------ Bitvault.Data/Item.cs | 20 ------------------ Bitvault.Data/ItemEntry.cs | 21 +++++++++++++++++++ Bitvault.Data/{Tag.cs => TagEntry.cs} | 4 +++- Bitvault/ContainerActivatedHandler.cs | 11 ---------- Bitvault/ContainerViewModelHandler.cs | 4 ++-- Bitvault/CreateItemHandler.cs | 14 +++++++++---- Bitvault/Item.cs | 18 +--------------- Bitvault/ItemActivatedHandler.cs | 23 +++++++++++++++++++++ 11 files changed, 71 insertions(+), 63 deletions(-) rename Bitvault.Data/{Document.cs => BlobEntry.cs} (55%) delete mode 100644 Bitvault.Data/Item.cs create mode 100644 Bitvault.Data/ItemEntry.cs rename Bitvault.Data/{Tag.cs => TagEntry.cs} (64%) create mode 100644 Bitvault/ItemActivatedHandler.cs diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index f267988..33af2a9 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -90,6 +90,7 @@ public partial class App : Application services.AddTemplate(); services.AddHandler(); + services.AddHandler(); }); })!); diff --git a/Bitvault.Data/Document.cs b/Bitvault.Data/BlobEntry.cs similarity index 55% rename from Bitvault.Data/Document.cs rename to Bitvault.Data/BlobEntry.cs index cdc13de..bdc6fd8 100644 --- a/Bitvault.Data/Document.cs +++ b/Bitvault.Data/BlobEntry.cs @@ -1,10 +1,12 @@ using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace Bitvault.Data; -public record Document +[Table("Blobs")] +public record BlobEntry { - public byte[]? Blob { get; set; } + public byte[]? Data { get; set; } public DocumentType Type { get; set; } diff --git a/Bitvault.Data/ContainerDbContext.cs b/Bitvault.Data/ContainerDbContext.cs index 0bf006d..b3e48aa 100644 --- a/Bitvault.Data/ContainerDbContext.cs +++ b/Bitvault.Data/ContainerDbContext.cs @@ -5,20 +5,20 @@ namespace Bitvault.Data; public class ContainerDbContext(DbContextOptions options) : DbContext(options) { - public DbSet Documents { get; set; } + public DbSet Blobs { get; set; } - public DbSet Items { get; set; } + public DbSet Items { get; set; } - public DbSet Tags { get; set; } + public DbSet Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity() + modelBuilder.Entity() .HasMany(x => x.Tags) .WithOne(); - modelBuilder.Entity() - .HasMany(x => x.Documents) + modelBuilder.Entity() + .HasMany(x => x.Blobs) .WithOne(); } } diff --git a/Bitvault.Data/Item.cs b/Bitvault.Data/Item.cs deleted file mode 100644 index 2031f90..0000000 --- a/Bitvault.Data/Item.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Bitvault.Data; -using System.ComponentModel.DataAnnotations; - -namespace Bitvault.Data; - -public record Item -{ - [Key] - public int Id { get; set; } - - public string? Name { get; set; } - - public string? Description { get; set; } - - public int State { get; set; } = 0; - - public ICollection? Tags { get; } - - public ICollection? Documents { get; } -} \ No newline at end of file diff --git a/Bitvault.Data/ItemEntry.cs b/Bitvault.Data/ItemEntry.cs new file mode 100644 index 0000000..0c8ae06 --- /dev/null +++ b/Bitvault.Data/ItemEntry.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Bitvault.Data; + +[Table("Items")] +public record ItemEntry +{ + [Key] + public int Id { get; set; } + + public string? Name { get; set; } + + public string? Description { get; set; } + + public int State { get; set; } = 0; + + public ICollection? Tags { get; } + + public ICollection? Blobs { get; } +} \ No newline at end of file diff --git a/Bitvault.Data/Tag.cs b/Bitvault.Data/TagEntry.cs similarity index 64% rename from Bitvault.Data/Tag.cs rename to Bitvault.Data/TagEntry.cs index 0451089..3974b6f 100644 --- a/Bitvault.Data/Tag.cs +++ b/Bitvault.Data/TagEntry.cs @@ -1,8 +1,10 @@ using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace Bitvault.Data; -public class Tag +[Table("Tags")] +public class TagEntry { [Key] public int Id { get; set; } diff --git a/Bitvault/ContainerActivatedHandler.cs b/Bitvault/ContainerActivatedHandler.cs index 92fdb30..80fd71d 100644 --- a/Bitvault/ContainerActivatedHandler.cs +++ b/Bitvault/ContainerActivatedHandler.cs @@ -31,15 +31,4 @@ public class ContainerActivatedHandler(IContainerHostCollection containers, } } } -} - -public class ItemActivatedHandler(IServiceFactory serviceFactory, - IPublisher publisher) : - INotificationHandler> -{ - public async Task Handle(ActivatedEventArgs args, - CancellationToken cancellationToken = default) - { - - } } \ No newline at end of file diff --git a/Bitvault/ContainerViewModelHandler.cs b/Bitvault/ContainerViewModelHandler.cs index 24692e5..2ac3249 100644 --- a/Bitvault/ContainerViewModelHandler.cs +++ b/Bitvault/ContainerViewModelHandler.cs @@ -16,7 +16,7 @@ public class ContainerViewModelHandler(IDbContextFactory dbC { if (args.Options is ContainerViewModelConfiguration configuration) { - ExpressionStarter predicate = PredicateBuilder.New(true); + ExpressionStarter predicate = PredicateBuilder.New(true); if (configuration.Filter == "All") { @@ -36,7 +36,7 @@ public class ContainerViewModelHandler(IDbContextFactory dbC var items = await Task.Run(async () => { using ContainerDbContext context = dbContextFactory.CreateDbContext(); - return await context.Set().Where(predicate).Select(x => new + return await context.Set().Where(predicate).Select(x => new { x.Id, x.Name diff --git a/Bitvault/CreateItemHandler.cs b/Bitvault/CreateItemHandler.cs index 7831675..630e110 100644 --- a/Bitvault/CreateItemHandler.cs +++ b/Bitvault/CreateItemHandler.cs @@ -1,5 +1,6 @@ using Bitvault.Data; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; using Toolkit.Foundation; namespace Bitvault; @@ -14,16 +15,21 @@ public class CreateItemHandler(IDbContextFactory dbContextFa { try { + using ContainerDbContext context = dbContextFactory.CreateDbContext(); + EntityEntry? result = null; + await Task.Run(async () => { - using ContainerDbContext context = dbContextFactory.CreateDbContext(); - await context.AddAsync(new Data.Item { Name = configuration.Name }, cancellationToken); + result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken); await context.SaveChangesAsync(cancellationToken); }, cancellationToken); - await publisher.Publish(Activated.As(configuration)); - return true; + if (result is not null) + { + await publisher.Publish(Activated.As(new Item { Id = result.Entity.Id }), cancellationToken); + return true; + } } catch { diff --git a/Bitvault/Item.cs b/Bitvault/Item.cs index 4a123bb..bf4c395 100644 --- a/Bitvault/Item.cs +++ b/Bitvault/Item.cs @@ -1,23 +1,7 @@ namespace Bitvault; -public record Item(TValue? Value = default); - public record Item { - public Item(int id) - { - Id = id; - } - - public Item() - { - - } - - public static Item As(TValue value) => new(value); - - public static Item As() where TValue : new() => new(new TValue()); - - public int Id { get; } + public int Id { get; init; } } diff --git a/Bitvault/ItemActivatedHandler.cs b/Bitvault/ItemActivatedHandler.cs new file mode 100644 index 0000000..edbbcf7 --- /dev/null +++ b/Bitvault/ItemActivatedHandler.cs @@ -0,0 +1,23 @@ +using Microsoft.Extensions.DependencyInjection; +using Toolkit.Foundation; + +namespace Bitvault; + +public class ItemActivatedHandler(IServiceProvider serviceProvider, + IProxyService proxyPublisher) : + INotificationHandler> +{ + public async Task Handle(ActivatedEventArgs args, + CancellationToken cancellationToken = default) + { + IServiceScope serviceScope = serviceProvider.CreateScope(); + IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); + + if (serviceFactory.Create(2, "efesf", "Description " + 1) is ItemNavigationViewModel viewModel) + { + // somehow, we need to get back out of the scope back to the compoment level, this currently doesnt work, and we need a better and cleaner way + await proxyPublisher.Proxy.Publish(new CreateEventArgs(viewModel), + nameof(ContainerViewModel), cancellationToken); + } + } +} \ No newline at end of file