diff --git a/Wallet.Data/BlobEntry.cs b/Wallet.Data/BlobEntry.cs index 7a66d61..ab5503b 100644 --- a/Wallet.Data/BlobEntry.cs +++ b/Wallet.Data/BlobEntry.cs @@ -11,7 +11,7 @@ public record BlobEntry public int Type { get; set; } [Key] - public int Id { get; set; } + public Guid Id { get; set; } public DateTime DateTime { get; set; } } \ No newline at end of file diff --git a/Wallet.Data/ItemEntry.cs b/Wallet.Data/ItemEntry.cs index 83bfc28..39a989f 100644 --- a/Wallet.Data/ItemEntry.cs +++ b/Wallet.Data/ItemEntry.cs @@ -15,6 +15,8 @@ public record ItemEntry public int State { get; set; } = 0; + public BlobEntry? Image { get; set; } + public required string Category { get; set; } public ICollection Tags { get; set; } = new List(); diff --git a/Wallet.Data/LockerContext.cs b/Wallet.Data/LockerContext.cs index 7f2ee40..dbe78ed 100644 --- a/Wallet.Data/LockerContext.cs +++ b/Wallet.Data/LockerContext.cs @@ -13,6 +13,14 @@ public class WalletContext(DbContextOptions options) : protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity() + .HasKey(x => x.Id); + + modelBuilder.Entity() + .Property(x => x.Id) + .ValueGeneratedOnAdd() + .HasDefaultValueSql("NEWID()"); + modelBuilder.Entity() .HasMany(x => x.Tags) .WithOne() @@ -22,5 +30,19 @@ public class WalletContext(DbContextOptions options) : .HasMany(x => x.Blobs) .WithOne() .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne(x => x.Image) + .WithOne() + .HasForeignKey() + .IsRequired(false); + + modelBuilder.Entity() + .HasKey(x => x.Id); + + modelBuilder.Entity() + .Property(x => x.Id) + .ValueGeneratedOnAdd() + .HasDefaultValueSql("NEWID()"); } } \ No newline at end of file diff --git a/Wallet.Data/TagEntry.cs b/Wallet.Data/TagEntry.cs index 9185361..7dfb202 100644 --- a/Wallet.Data/TagEntry.cs +++ b/Wallet.Data/TagEntry.cs @@ -7,7 +7,7 @@ namespace Wallet.Data; public class TagEntry { [Key] - public int Id { get; set; } + public Guid Id { get; set; } public string? Name { get; set; } } \ No newline at end of file diff --git a/Wallet/ConfirmCreateItemHandler.cs b/Wallet/ConfirmCreateItemHandler.cs index b5113fb..1052481 100644 --- a/Wallet/ConfirmCreateItemHandler.cs +++ b/Wallet/ConfirmCreateItemHandler.cs @@ -22,9 +22,9 @@ public class ConfirmCreateItemHandler(IMediator mediator, Item<(Guid, string, string, IImageDescriptor?)> item = new((id, name, category, imageDescriptor)); publisher.Publish(Created.As(item)); - await mediator.Handle, bool>(new CreateEventArgs<(Guid, string, string, - ItemConfiguration)>((id, name, category, itemConfiguration))); + await mediator.Handle, bool>(new CreateEventArgs<(Guid, string, string, IImageDescriptor?, + ItemConfiguration)>((id, name, category, imageDescriptor, itemConfiguration))); publisher.Publish(Changed.As()); } diff --git a/Wallet/CreateItemHandler.cs b/Wallet/CreateItemHandler.cs index b8c3766..ffff391 100644 --- a/Wallet/CreateItemHandler.cs +++ b/Wallet/CreateItemHandler.cs @@ -7,33 +7,54 @@ using Toolkit.Foundation; namespace Wallet; -public class CreateItemHandler(IDbContextFactory dbContextFactory) : - IHandler, bool> +public class CreateItemHandler(IImageWriter imageWriter, + IDbContextFactory dbContextFactory) : + IHandler, bool> { - public async Task Handle(CreateEventArgs<(Guid, string, string, ItemConfiguration)> args, + public async Task Handle(CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)> args, CancellationToken cancellationToken) { - if (args.Sender is (Guid id, string name, string category, ItemConfiguration configuration)) + if (args.Sender is (Guid id, string name, string category, IImageDescriptor imageDescriptor, ItemConfiguration configuration)) { try { string content = JsonSerializer.Serialize(configuration); + byte[]? thumbData = null; + + if (imageDescriptor is not null) + { + using MemoryStream memoryStream = new MemoryStream(); + imageWriter.Write(imageDescriptor, memoryStream); + thumbData = memoryStream.ToArray(); + } + ItemEntry itemEntry = new() { Id = id, Name = name, - Category = category + Category = category, + Image = thumbData != null ? new BlobEntry + { + Id = Guid.NewGuid(), + Data = thumbData, + DateTime = DateTime.UtcNow, + Type = 1 + } : null, + Blobs = + { + new BlobEntry + { + Id = Guid.NewGuid(), + Data = Encoding.UTF8.GetBytes(content), + DateTime = DateTime.UtcNow, + Type = 0 + } + } }; - itemEntry.Blobs.Add(new() - { - Data = Encoding.UTF8.GetBytes(content), - DateTime = DateTime.Now, - Type = 0, - }); - using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken); EntityEntry? result = await context.AddAsync(itemEntry, cancellationToken); + await context.SaveChangesAsync(cancellationToken); if (result is not null)