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
+2
View File
@@ -15,6 +15,8 @@ public record ItemEntry
public int State { get; set; } = 0; public int State { get; set; } = 0;
public Guid? ImageId { get; set; }
public BlobEntry? Image { get; set; } public BlobEntry? Image { get; set; }
public required string Category { get; set; } public required string Category { get; set; }
+5 -4
View File
@@ -31,11 +31,12 @@ public class WalletContext(DbContextOptions<WalletContext> options) :
.WithOne() .WithOne()
.OnDelete(DeleteBehavior.Cascade); .OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ItemEntry>() modelBuilder.Entity<ItemEntry>().
.HasOne(x => x.Image) HasOne(i => i.Image)
.WithOne() .WithOne()
.HasForeignKey<ItemEntry>() .HasForeignKey<ItemEntry>(i => i.ImageId)
.IsRequired(false); .IsRequired(false)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<BlobEntry>() modelBuilder.Entity<BlobEntry>()
.HasKey(x => x.Id); .HasKey(x => x.Id);
+1 -1
View File
@@ -24,7 +24,7 @@ public class ConfirmCreateItemHandler(IMediator mediator,
await mediator.Handle<CreateEventArgs<(Guid, string, string, IImageDescriptor?, await mediator.Handle<CreateEventArgs<(Guid, string, string, IImageDescriptor?,
ItemConfiguration)>, bool>(new 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>()); publisher.Publish(Changed.As<Item>());
} }
+2 -4
View File
@@ -14,8 +14,7 @@ public class CreateItemHandler(IImageWriter imageWriter,
public async Task<bool> Handle(CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)> args, public async Task<bool> Handle(CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)> args,
CancellationToken cancellationToken) 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); string content = JsonSerializer.Serialize(configuration);
@@ -23,7 +22,7 @@ public class CreateItemHandler(IImageWriter imageWriter,
if (imageDescriptor is not null) if (imageDescriptor is not null)
{ {
using MemoryStream memoryStream = new MemoryStream(); using MemoryStream memoryStream = new();
imageWriter.Write(imageDescriptor, memoryStream); imageWriter.Write(imageDescriptor, memoryStream);
thumbData = memoryStream.ToArray(); thumbData = memoryStream.ToArray();
} }
@@ -65,7 +64,6 @@ public class CreateItemHandler(IImageWriter imageWriter,
catch catch
{ {
} }
}
return false; return false;
} }