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
+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)