Fixed item creation when there is no image attached
This commit is contained in:
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user