Add validation

This commit is contained in:
TheXamlGuy
2024-07-07 20:09:03 +01:00
parent 015af41fc8
commit 75bdd275c9
16 changed files with 142 additions and 104 deletions
+4 -4
View File
@@ -28,12 +28,12 @@ public class CreateItemHandler(IImageWriter imageWriter,
thumbData = memoryStream.ToArray();
}
ItemEntry itemEntry = new()
ItemEntity itemEntry = new()
{
Id = id,
Name = name,
Category = category,
Image = thumbData != null ? new BlobEntry
Image = thumbData != null ? new BlobEntity
{
Id = Guid.NewGuid(),
Data = thumbData,
@@ -42,7 +42,7 @@ public class CreateItemHandler(IImageWriter imageWriter,
} : null,
Blobs =
{
new BlobEntry
new BlobEntity
{
Id = Guid.NewGuid(),
Data = Encoding.UTF8.GetBytes(content),
@@ -53,7 +53,7 @@ public class CreateItemHandler(IImageWriter imageWriter,
};
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
EntityEntry<ItemEntry>? result = await context.AddAsync(itemEntry, cancellationToken);
EntityEntry<ItemEntity>? result = await context.AddAsync(itemEntry, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
+1 -1
View File
@@ -15,7 +15,7 @@ public class DeleteItemHandler(IDbContextFactory<WalletContext> dbContextFactory
Guid id = item.Value;
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)
if (await context.FindAsync<ItemEntity>(id) is ItemEntity result)
{
context.Items.Remove(result);
await context.SaveChangesAsync(cancellationToken);
+3
View File
@@ -0,0 +1,3 @@
namespace Wallet;
public record ItemEntry;
+6 -6
View File
@@ -19,9 +19,9 @@ public partial class ItemEntryViewModel<TValue>(IServiceProvider provider,
double width) :
Observable<string, TValue>(provider, factory, mediator, publisher, subscriber, disposer, key, value),
IItemEntryViewModel,
INotificationHandler<UpdateEventArgs<Item>>,
INotificationHandler<ConfirmEventArgs<Item>>,
INotificationHandler<CancelEventArgs<Item>>
INotificationHandler<ConfirmEventArgs<ItemEntry>>,
INotificationHandler<UpdateEventArgs<ItemEntry>>,
INotificationHandler<CancelEventArgs<ItemEntry>>
where TValue : notnull
{
[ObservableProperty]
@@ -36,10 +36,10 @@ public partial class ItemEntryViewModel<TValue>(IServiceProvider provider,
[ObservableProperty]
private double width = width;
public Task Handle(UpdateEventArgs<Item> args) =>
public Task Handle(UpdateEventArgs<ItemEntry> args) =>
Task.FromResult(State = ItemState.Write);
public Task Handle(CancelEventArgs<Item> args)
public Task Handle(CancelEventArgs<ItemEntry> args)
{
Revert();
@@ -47,7 +47,7 @@ public partial class ItemEntryViewModel<TValue>(IServiceProvider provider,
return Task.CompletedTask;
}
public Task Handle(ConfirmEventArgs<Item> args)
public Task Handle(ConfirmEventArgs<ItemEntry> args)
{
Commit();
+2 -2
View File
@@ -16,7 +16,7 @@ public class ItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
Guid id = item.Value;
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var result = await context.Set<ItemEntry>()
var result = await context.Set<ItemEntity>()
.Where(x => x.Id == id)
.Select(x => new
{
@@ -35,7 +35,7 @@ public class ItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
if (result is not null)
{
ItemConfiguration? configuration = null;
if (result.Blob is BlobEntry blob && blob.Data is { Length: > 0 } data)
if (result.Blob is BlobEntity blob && blob.Data is { Length: > 0 } data)
{
try
{
+21 -6
View File
@@ -1,14 +1,17 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Xml.Linq;
using Toolkit.Foundation;
using Wallet.Data;
namespace Wallet;
public partial class ItemHeaderViewModel :
Observable<string>,
INotificationHandler<UpdateEventArgs<Item>>,
INotificationHandler<ConfirmEventArgs<Item>>,
INotificationHandler<CancelEventArgs<Item>>,
IHandler<ValidateEventArgs<ItemEntry>, bool>,
INotificationHandler<UpdateEventArgs<ItemEntry>>,
INotificationHandler<ConfirmEventArgs<ItemEntry>>,
INotificationHandler<CancelEventArgs<ItemEntry>>,
INotificationHandler<NotifyEventArgs<ItemCategory<string>>>,
INotificationHandler<NotifyEventArgs<Item<IImageDescriptor>>>,
IItemViewModel
@@ -30,11 +33,14 @@ public partial class ItemHeaderViewModel :
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
IValidation validation,
ItemHeaderConfiguration configuration,
ItemState state,
string value,
IImageDescriptor? imageDescriptor = null) : base(provider, factory, mediator, publisher, subscriber, disposer, value)
{
Validation = validation;
this.configuration = configuration;
State = state;
@@ -43,12 +49,18 @@ public partial class ItemHeaderViewModel :
Track(nameof(Value), () => Value, x => Value = x);
Track(nameof(ImageDescriptor), () => ImageDescriptor, x => ImageDescriptor = x);
Validation.Add(() => Value, [new ValidationRule(() => Value is { Length: > 0 }, "Name is required")],
ValidationTrigger.Deferred);
}
public Task Handle(UpdateEventArgs<Item> args) =>
[ObservableProperty]
private IValidation validation;
public Task Handle(UpdateEventArgs<ItemEntry> args) =>
Task.FromResult(State = ItemState.Write);
public Task Handle(CancelEventArgs<Item> args)
public Task Handle(CancelEventArgs<ItemEntry> args)
{
Revert();
@@ -56,7 +68,7 @@ public partial class ItemHeaderViewModel :
return Task.CompletedTask;
}
public Task Handle(ConfirmEventArgs<Item> args)
public Task Handle(ConfirmEventArgs<ItemEntry> args)
{
Commit();
@@ -85,6 +97,9 @@ public partial class ItemHeaderViewModel :
return Task.CompletedTask;
}
public async Task<bool> Handle(ValidateEventArgs<ItemEntry> args,
CancellationToken cancellationToken) => await Validation.Validate();
protected override void OnValueChanged()
{
if (configuration is not null)
+2 -2
View File
@@ -16,7 +16,7 @@ public class ItemImageHandler(IDbContextFactory<WalletContext> dbContextFactory,
Guid id = item.Value;
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var result = await context.Set<ItemEntry>()
var result = await context.Set<ItemEntity>()
.Where(x => x.Id == id)
.Select(x => new
{
@@ -25,7 +25,7 @@ public class ItemImageHandler(IDbContextFactory<WalletContext> dbContextFactory,
.FirstOrDefaultAsync(cancellationToken);
if (result is not null &&
result.Image is BlobEntry image &&
result.Image is BlobEntity image &&
image.Data is { Length: > 0 } data)
{
MemoryStream stream = new(data);
+22 -10
View File
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Toolkit.Foundation;
using Wallet.Data;
namespace Wallet;
@@ -77,6 +78,7 @@ public partial class ItemViewModel :
public Task Handle(UpdateEventArgs<Item> args)
{
Publisher.Publish(Update.As<ItemEntry>());
Publisher.Publish(Notify.As(Factory.Create<ItemCommandHeaderCollection>(new List<IDisposable>
{
Factory.Create<ConfirmItemActionViewModel>(),
@@ -89,6 +91,7 @@ public partial class ItemViewModel :
public Task Handle(CancelEventArgs<Item> args)
{
Publisher.Publish(Cancel.As<ItemEntry>());
Publisher.Publish(Notify.As(Factory.Create<ItemCommandHeaderCollection>(new List<IDisposable>
{
Factory.Create<EditItemActionViewModel>(),
@@ -99,20 +102,29 @@ public partial class ItemViewModel :
return Task.CompletedTask;
}
public Task Handle(ConfirmEventArgs<Item> args)
public async Task Handle(ConfirmEventArgs<Item> args)
{
Publisher.Publish(Notify.As(Factory.Create<ItemCommandHeaderCollection>(new List<IDisposable>
List<bool> results = [];
await foreach (bool result in Mediator.HandleManyAsync<ValidateEventArgs<ItemEntry>, bool>(Validate.As<ItemEntry>()))
{
Factory.Create<FavouriteItemActionViewModel>(Favourite),
Factory.Create<EditItemActionViewModel>(),
Factory.Create<ArchiveItemActionViewModel>(),
})));
results.Add(result);
}
Publisher.Publish(Confirm.As<Item>(),
State is ItemState.New ? nameof(ItemState.New) : nameof(ItemState.Write));
if (results.All(result => result))
{
Publisher.Publish(Confirm.As<ItemEntry>());
Publisher.Publish(Notify.As(Factory.Create<ItemCommandHeaderCollection>(new List<IDisposable>
{
Factory.Create<FavouriteItemActionViewModel>(Favourite),
Factory.Create<EditItemActionViewModel>(),
Factory.Create<ArchiveItemActionViewModel>(),
})));
State = ItemState.Read;
return Task.CompletedTask;
Publisher.Publish(Confirm.As<Item>(),
State is ItemState.New ? nameof(ItemState.New) : nameof(ItemState.Write));
State = ItemState.Read;
}
}
public override Task OnActivated()
+3 -3
View File
@@ -16,8 +16,8 @@ public class QueryWalletHandler(IDbContextFactory<WalletContext> dbContextFactor
{
(string filter, string text) = Wallet.Value;
ExpressionStarter<ItemEntry> predicate =
PredicateBuilder.New<ItemEntry>(true);
ExpressionStarter<ItemEntity> predicate =
PredicateBuilder.New<ItemEntity>(true);
if (filter is { Length: <= 0 })
{
@@ -49,7 +49,7 @@ public class QueryWalletHandler(IDbContextFactory<WalletContext> dbContextFactor
}
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var results = await context.Set<ItemEntry>()
var results = await context.Set<ItemEntity>()
.Where(predicate)
.Select(x => new
{
+3 -3
View File
@@ -20,7 +20,7 @@ public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory,
try
{
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
ItemEntry? result = result = await context.Set<ItemEntry>()
ItemEntity? result = result = await context.Set<ItemEntity>()
.Include(x => x.Image).FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
if (result is not null)
@@ -40,7 +40,7 @@ public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory,
imageWriter.Write(imageDescriptor, memoryStream);
thumbData = memoryStream.ToArray();
if (result.Image is BlobEntry existingImageBlob)
if (result.Image is BlobEntity existingImageBlob)
{
existingImageBlob.Data = thumbData;
existingImageBlob.DateTime = DateTime.UtcNow;
@@ -49,7 +49,7 @@ public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory,
}
else
{
result.Image = new BlobEntry
result.Image = new BlobEntity
{
Id = Guid.NewGuid(),
Data = thumbData,
+1 -1
View File
@@ -13,7 +13,7 @@ public class UpdateItemStateHandler(IDbContextFactory<WalletContext> dbContextFa
if (args.Sender is (Guid id, int state))
{
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)
if (await context.FindAsync<ItemEntity>(id) is ItemEntity result)
{
result.State = state;
await context.SaveChangesAsync(cancellationToken);