wip retrieve image from db
This commit is contained in:
@@ -22,6 +22,7 @@ public class ItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
|
||||
{
|
||||
x.Id,
|
||||
x.Name,
|
||||
HasImage = x.ImageId != null,
|
||||
x.Description,
|
||||
x.Category,
|
||||
Blob = x.Blobs
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Wallet;
|
||||
|
||||
public record ItemImage;
|
||||
|
||||
public record ItemImage<TValue>(TValue Value);
|
||||
@@ -0,0 +1,38 @@
|
||||
using Wallet.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Wallet;
|
||||
|
||||
public class ItemImageHandler(IDbContextFactory<WalletContext> dbContextFactory,
|
||||
IImageReader imageReader) :
|
||||
IHandler<RequestEventArgs<ItemImage<Guid>>, IImageDescriptor?>
|
||||
{
|
||||
public async Task<IImageDescriptor?> Handle(RequestEventArgs<ItemImage<Guid>> args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (args.Sender is ItemImage<Guid> item)
|
||||
{
|
||||
Guid id = item.Value;
|
||||
|
||||
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var result = await context.Set<ItemEntry>()
|
||||
.Where(x => x.Id == id)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Image
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (result is not null &&
|
||||
result.Image is BlobEntry image &&
|
||||
image.Data is { Length: > 0 } data)
|
||||
{
|
||||
MemoryStream stream = new(data);
|
||||
return imageReader.Get(stream, 200, 200, true);
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user