Image storage to the db WIP

This commit is contained in:
TheXamlGuy
2024-07-01 22:33:02 +01:00
parent e913e08f40
commit bacabe771c
6 changed files with 62 additions and 17 deletions
+1 -1
View File
@@ -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; }
}
+2
View File
@@ -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<TagEntry> Tags { get; set; } = new List<TagEntry>();
+22
View File
@@ -13,6 +13,14 @@ public class WalletContext(DbContextOptions<WalletContext> options) :
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemEntry>()
.HasKey(x => x.Id);
modelBuilder.Entity<ItemEntry>()
.Property(x => x.Id)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("NEWID()");
modelBuilder.Entity<ItemEntry>()
.HasMany(x => x.Tags)
.WithOne()
@@ -22,5 +30,19 @@ public class WalletContext(DbContextOptions<WalletContext> options) :
.HasMany(x => x.Blobs)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ItemEntry>()
.HasOne(x => x.Image)
.WithOne()
.HasForeignKey<ItemEntry>()
.IsRequired(false);
modelBuilder.Entity<BlobEntry>()
.HasKey(x => x.Id);
modelBuilder.Entity<BlobEntry>()
.Property(x => x.Id)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("NEWID()");
}
}
+1 -1
View File
@@ -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; }
}
+3 -3
View File
@@ -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<CreateEventArgs<(Guid, string, string,
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, string, string,
ItemConfiguration)>((id, name, category, itemConfiguration)));
await mediator.Handle<CreateEventArgs<(Guid, string, string, IImageDescriptor?,
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, string, string, IImageDescriptor?,
ItemConfiguration)>((id, name, category, imageDescriptor, itemConfiguration)));
publisher.Publish(Changed.As<Item>());
}
+33 -12
View File
@@ -7,33 +7,54 @@ using Toolkit.Foundation;
namespace Wallet;
public class CreateItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
IHandler<CreateEventArgs<(Guid, string, string, ItemConfiguration)>, bool>
public class CreateItemHandler(IImageWriter imageWriter,
IDbContextFactory<WalletContext> dbContextFactory) :
IHandler<CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)>, bool>
{
public async Task<bool> Handle(CreateEventArgs<(Guid, string, string, ItemConfiguration)> args,
public async Task<bool> 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<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
if (result is not null)