Added ability to modify image of existing items
This commit is contained in:
@@ -6,20 +6,22 @@ using Toolkit.Foundation;
|
||||
|
||||
namespace Wallet;
|
||||
|
||||
public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory) :
|
||||
IHandler<UpdateEventArgs<Item<(Guid, string, ItemConfiguration)>>, bool>
|
||||
public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory,
|
||||
IImageWriter imageWriter) :
|
||||
IHandler<UpdateEventArgs<Item<(Guid, string, string, IImageDescriptor?, ItemConfiguration)>>, bool>
|
||||
{
|
||||
public async Task<bool> Handle(UpdateEventArgs<Item<(Guid, string, ItemConfiguration)>> args,
|
||||
public async Task<bool> Handle(UpdateEventArgs<Item<(Guid, string, string, IImageDescriptor?, ItemConfiguration)>> args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (args.Sender is Item<(Guid, string, ItemConfiguration)> item)
|
||||
if (args.Sender is Item<(Guid, string, string, IImageDescriptor?, ItemConfiguration)> item)
|
||||
{
|
||||
(Guid id, string name, ItemConfiguration configuration) = item.Value;
|
||||
(Guid id, string name, string category, IImageDescriptor? imageDescriptor, ItemConfiguration configuration) = item.Value;
|
||||
|
||||
try
|
||||
{
|
||||
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
ItemEntry? result = result = await context.Set<ItemEntry>().FindAsync([id], cancellationToken);
|
||||
ItemEntry? result = result = await context.Set<ItemEntry>()
|
||||
.Include(x => x.Image).FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (result is not null)
|
||||
{
|
||||
@@ -31,7 +33,45 @@ public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory)
|
||||
Type = 0,
|
||||
});
|
||||
|
||||
if (imageDescriptor is not null)
|
||||
{
|
||||
byte[]? thumbData = null;
|
||||
using MemoryStream memoryStream = new();
|
||||
imageWriter.Write(imageDescriptor, memoryStream);
|
||||
thumbData = memoryStream.ToArray();
|
||||
|
||||
if (result.Image is BlobEntry existingImageBlob)
|
||||
{
|
||||
existingImageBlob.Data = thumbData;
|
||||
existingImageBlob.DateTime = DateTime.UtcNow;
|
||||
|
||||
context.Entry(result.Image).State = EntityState.Modified;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Image = new BlobEntry
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Data = thumbData,
|
||||
DateTime = DateTime.UtcNow,
|
||||
Type = 1
|
||||
};
|
||||
|
||||
context.Entry(result.Image).State = EntityState.Added;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (result.Image is not null)
|
||||
{
|
||||
context.Remove(result.Image);
|
||||
result.Image = null;
|
||||
}
|
||||
}
|
||||
|
||||
result.Name = name;
|
||||
result.Category = category;
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user