Image storage to the db WIP
This commit is contained in:
@@ -11,7 +11,7 @@ public record BlobEntry
|
|||||||
public int Type { get; set; }
|
public int Type { get; set; }
|
||||||
|
|
||||||
[Key]
|
[Key]
|
||||||
public int Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
public DateTime DateTime { get; set; }
|
public DateTime DateTime { get; set; }
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,8 @@ public record ItemEntry
|
|||||||
|
|
||||||
public int State { get; set; } = 0;
|
public int State { get; set; } = 0;
|
||||||
|
|
||||||
|
public BlobEntry? Image { get; set; }
|
||||||
|
|
||||||
public required string Category { get; set; }
|
public required string Category { get; set; }
|
||||||
|
|
||||||
public ICollection<TagEntry> Tags { get; set; } = new List<TagEntry>();
|
public ICollection<TagEntry> Tags { get; set; } = new List<TagEntry>();
|
||||||
|
|||||||
@@ -13,6 +13,14 @@ public class WalletContext(DbContextOptions<WalletContext> options) :
|
|||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
modelBuilder.Entity<ItemEntry>()
|
||||||
|
.HasKey(x => x.Id);
|
||||||
|
|
||||||
|
modelBuilder.Entity<ItemEntry>()
|
||||||
|
.Property(x => x.Id)
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasDefaultValueSql("NEWID()");
|
||||||
|
|
||||||
modelBuilder.Entity<ItemEntry>()
|
modelBuilder.Entity<ItemEntry>()
|
||||||
.HasMany(x => x.Tags)
|
.HasMany(x => x.Tags)
|
||||||
.WithOne()
|
.WithOne()
|
||||||
@@ -22,5 +30,19 @@ public class WalletContext(DbContextOptions<WalletContext> options) :
|
|||||||
.HasMany(x => x.Blobs)
|
.HasMany(x => x.Blobs)
|
||||||
.WithOne()
|
.WithOne()
|
||||||
.OnDelete(DeleteBehavior.Cascade);
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
modelBuilder.Entity<ItemEntry>()
|
||||||
|
.HasOne(x => x.Image)
|
||||||
|
.WithOne()
|
||||||
|
.HasForeignKey<ItemEntry>()
|
||||||
|
.IsRequired(false);
|
||||||
|
|
||||||
|
modelBuilder.Entity<BlobEntry>()
|
||||||
|
.HasKey(x => x.Id);
|
||||||
|
|
||||||
|
modelBuilder.Entity<BlobEntry>()
|
||||||
|
.Property(x => x.Id)
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasDefaultValueSql("NEWID()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace Wallet.Data;
|
|||||||
public class TagEntry
|
public class TagEntry
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
public int Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
}
|
}
|
||||||
@@ -22,9 +22,9 @@ public class ConfirmCreateItemHandler(IMediator mediator,
|
|||||||
Item<(Guid, string, string, IImageDescriptor?)> item = new((id, name, category, imageDescriptor));
|
Item<(Guid, string, string, IImageDescriptor?)> item = new((id, name, category, imageDescriptor));
|
||||||
publisher.Publish(Created.As(item));
|
publisher.Publish(Created.As(item));
|
||||||
|
|
||||||
await mediator.Handle<CreateEventArgs<(Guid, string, string,
|
await mediator.Handle<CreateEventArgs<(Guid, string, string, IImageDescriptor?,
|
||||||
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, string, string,
|
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, string, string, IImageDescriptor?,
|
||||||
ItemConfiguration)>((id, name, category, itemConfiguration)));
|
ItemConfiguration)>((id, name, category, imageDescriptor, itemConfiguration)));
|
||||||
|
|
||||||
publisher.Publish(Changed.As<Item>());
|
publisher.Publish(Changed.As<Item>());
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-11
@@ -7,33 +7,54 @@ using Toolkit.Foundation;
|
|||||||
|
|
||||||
namespace Wallet;
|
namespace Wallet;
|
||||||
|
|
||||||
public class CreateItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
|
public class CreateItemHandler(IImageWriter imageWriter,
|
||||||
IHandler<CreateEventArgs<(Guid, string, string, ItemConfiguration)>, bool>
|
IDbContextFactory<WalletContext> dbContextFactory) :
|
||||||
|
IHandler<CreateEventArgs<(Guid, string, string, IImageDescriptor?, ItemConfiguration)>, bool>
|
||||||
{
|
{
|
||||||
public async Task<bool> Handle(CreateEventArgs<(Guid, string, string, 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, ItemConfiguration configuration))
|
if (args.Sender is (Guid id, string name, string category, IImageDescriptor imageDescriptor, ItemConfiguration configuration))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string content = JsonSerializer.Serialize(configuration);
|
string content = JsonSerializer.Serialize(configuration);
|
||||||
|
byte[]? thumbData = null;
|
||||||
|
|
||||||
|
if (imageDescriptor is not null)
|
||||||
|
{
|
||||||
|
using MemoryStream memoryStream = new MemoryStream();
|
||||||
|
imageWriter.Write(imageDescriptor, memoryStream);
|
||||||
|
thumbData = memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
ItemEntry itemEntry = new()
|
ItemEntry itemEntry = new()
|
||||||
{
|
{
|
||||||
Id = id,
|
Id = id,
|
||||||
Name = name,
|
Name = name,
|
||||||
Category = category
|
Category = category,
|
||||||
};
|
Image = thumbData != null ? new BlobEntry
|
||||||
|
|
||||||
itemEntry.Blobs.Add(new()
|
|
||||||
{
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Data = thumbData,
|
||||||
|
DateTime = DateTime.UtcNow,
|
||||||
|
Type = 1
|
||||||
|
} : null,
|
||||||
|
Blobs =
|
||||||
|
{
|
||||||
|
new BlobEntry
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
Data = Encoding.UTF8.GetBytes(content),
|
Data = Encoding.UTF8.GetBytes(content),
|
||||||
DateTime = DateTime.Now,
|
DateTime = DateTime.UtcNow,
|
||||||
Type = 0,
|
Type = 0
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
EntityEntry<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
|
EntityEntry<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
|
||||||
|
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
await context.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
if (result is not null)
|
if (result is not null)
|
||||||
|
|||||||
Reference in New Issue
Block a user