Fixed item creation when there is no image attached

This commit is contained in:
TheXamlGuy
2024-07-02 11:49:57 +01:00
parent bacabe771c
commit 840d8fb679
4 changed files with 39 additions and 38 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ public class ConfirmCreateItemHandler(IMediator mediator,
await mediator.Handle<CreateEventArgs<(Guid, string, string, IImageDescriptor?,
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, string, string, IImageDescriptor?,
ItemConfiguration)>((id, name, category, imageDescriptor, itemConfiguration)));
ItemConfiguration)>((id, name, category, imageDescriptor ?? default, itemConfiguration)));
publisher.Publish(Changed.As<Item>());
}
+31 -33
View File
@@ -14,34 +14,33 @@ public class CreateItemHandler(IImageWriter imageWriter,
public async Task<bool> Handle(CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)> args,
CancellationToken cancellationToken)
{
if (args.Sender is (Guid id, string name, string category, IImageDescriptor imageDescriptor, ItemConfiguration configuration))
(Guid id, string name, string category, IImageDescriptor? imageDescriptor, ItemConfiguration configuration) = args.Sender;
try
{
try
string content = JsonSerializer.Serialize(configuration);
byte[]? thumbData = null;
if (imageDescriptor is not null)
{
string content = JsonSerializer.Serialize(configuration);
byte[]? thumbData = null;
using MemoryStream memoryStream = new();
imageWriter.Write(imageDescriptor, memoryStream);
thumbData = memoryStream.ToArray();
}
if (imageDescriptor is not null)
ItemEntry itemEntry = new()
{
Id = id,
Name = name,
Category = category,
Image = thumbData != null ? new BlobEntry
{
using MemoryStream memoryStream = new MemoryStream();
imageWriter.Write(imageDescriptor, memoryStream);
thumbData = memoryStream.ToArray();
}
ItemEntry itemEntry = new()
{
Id = id,
Name = name,
Category = category,
Image = thumbData != null ? new BlobEntry
Id = Guid.NewGuid(),
Data = thumbData,
DateTime = DateTime.UtcNow,
Type = 1
} : null,
Blobs =
{
Id = Guid.NewGuid(),
Data = thumbData,
DateTime = DateTime.UtcNow,
Type = 1
} : null,
Blobs =
{
new BlobEntry
{
Id = Guid.NewGuid(),
@@ -50,22 +49,21 @@ public class CreateItemHandler(IImageWriter imageWriter,
Type = 0
}
}
};
};
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
EntityEntry<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
EntityEntry<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);
if (result is not null)
{
return true;
}
}
catch
if (result is not null)
{
return true;
}
}
catch
{
}
return false;
}