More edge cases

This commit is contained in:
TheXamlGuy
2024-05-26 14:51:25 +01:00
parent 859d16c85c
commit 307443e4ce
12 changed files with 59 additions and 56 deletions
+2 -1
View File
@@ -71,7 +71,8 @@ public partial class App : Application
services.AddHandler<QueryContainerHandler>();
services.AddHandler<CreateItemHandler>();
services.AddHandler<EditItemHander>();
services.AddHandler<UpdateItemHander>();
services.AddHandler<UpdateItemStateHandler>();
services.AddHandler<OpenContainerHandler>();
+10 -13
View File
@@ -1,11 +1,10 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
using Toolkit.Foundation;
namespace Bitvault;
public class ArchiveItemHandler(IValueStore<Item> valueStore,
IDbContextFactory<ContainerDbContext> dbContextFactory) :
ICache<Item> cache,
IMediator mediator) :
INotificationHandler<ArchiveEventArgs<Item>>
{
public async Task Handle(ArchiveEventArgs<Item> args)
@@ -14,20 +13,18 @@ public class ArchiveItemHandler(IValueStore<Item> valueStore,
{
if (valueStore.Value is Item item)
{
await Task.Run(async () =>
if (cache.Contains(item))
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
await mediator.Handle<UpdateEventArgs<(Guid, int)>,
bool>(new UpdateEventArgs<(Guid, int)>((item.Id, 2)));
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
{
result.State = 2;
await context.SaveChangesAsync();
}
});
}
cache.Remove(item);
}
}
}
catch
{
}
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ public class ConfirmItemHandler(IValueStore<Item> valueStore,
valueStore.Set(newItem);
await mediator.Handle<EditEventArgs<(Guid, ItemConfiguration)>, bool>(new EditEventArgs<(Guid,
await mediator.Handle<UpdateEventArgs<(Guid, ItemConfiguration)>, bool>(new UpdateEventArgs<(Guid,
ItemConfiguration)>((item.Id, new ItemConfiguration { Name = name })));
}
else
+1 -1
View File
@@ -11,5 +11,5 @@ public partial class EditItemActionViewModel(IServiceProvider provider,
IDisposer disposer) : Observable(provider, factory, mediator, publisher, subscriber, disposer)
{
[RelayCommand]
public void Invoke() => Publisher.Publish(Edit.As<Item>());
public void Invoke() => Publisher.Publish(Update.As<Item>());
}
+4 -14
View File
@@ -1,11 +1,9 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
using Toolkit.Foundation;
namespace Bitvault;
public class FavouriteItemHandler(IValueStore<Item> valueStore,
IDbContextFactory<ContainerDbContext> dbContextFactory) :
IMediator mediator) :
INotificationHandler<FavouriteEventArgs<Item>>
{
public async Task Handle(FavouriteEventArgs<Item> args)
@@ -14,16 +12,8 @@ public class FavouriteItemHandler(IValueStore<Item> valueStore,
{
if (valueStore.Value is Item item)
{
await Task.Run(async () =>
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
{
result.State = 1;
await context.SaveChangesAsync();
}
});
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid,
int)>((item.Id, 1)));
}
}
catch
+1 -1
View File
@@ -2,5 +2,5 @@
public record ItemConfiguration
{
public string? Name { get; set; } = "";
public string Name { get; set; } = "";
}
+1 -1
View File
@@ -2,5 +2,5 @@
public record ItemHeaderConfiguration
{
public string? Name { get; init; } = "";
public string Name { get; init; } = "";
}
+2 -2
View File
@@ -6,7 +6,7 @@ namespace Bitvault;
public partial class ItemHeaderViewModel : Observable<string, string>,
IHandler<ValidationEventArgs<Item>, bool>,
IHandler<ConfirmEventArgs<Item>, ItemHeaderConfiguration>,
INotificationHandler<EditEventArgs<Item>>,
INotificationHandler<UpdateEventArgs<Item>>,
INotificationHandler<ConfirmEventArgs<Item>>,
INotificationHandler<CancelEventArgs<Item>>,
IItemEntryViewModel
@@ -38,7 +38,7 @@ public partial class ItemHeaderViewModel : Observable<string, string>,
public Task<ItemHeaderConfiguration> Handle(ConfirmEventArgs<Item> args,
CancellationToken cancellationToken) => Task.FromResult(new ItemHeaderConfiguration { Name = Value });
public Task Handle(EditEventArgs<Item> args) =>
public Task Handle(UpdateEventArgs<Item> args) =>
Task.FromResult(Immutable = false);
public Task Handle(CancelEventArgs<Item> args)
+2 -2
View File
@@ -5,7 +5,7 @@ namespace Bitvault;
public partial class ItemViewModel :
ObservableCollection<IItemEntryViewModel>,
INotificationHandler<EditEventArgs<Item>>,
INotificationHandler<UpdateEventArgs<Item>>,
INotificationHandler<ConfirmEventArgs<Item>>,
INotificationHandler<CancelEventArgs<Item>>
{
@@ -49,7 +49,7 @@ public partial class ItemViewModel :
public IContentTemplate Template { get; set; }
public Task Handle(EditEventArgs<Item> args)
public Task Handle(UpdateEventArgs<Item> args)
{
Publisher.Publish(Notify.As(Factory.Create<CommandCollection>(new List<IDisposable>
{
+4 -15
View File
@@ -1,11 +1,8 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
using Toolkit.Foundation;
namespace Bitvault;
public class UnfavouriteItemHandler(IValueStore<Item> valueStore,
IDbContextFactory<ContainerDbContext> dbContextFactory) :
IMediator mediator) :
INotificationHandler<UnfavouriteEventArgs<Item>>
{
public async Task Handle(UnfavouriteEventArgs<Item> args)
@@ -14,16 +11,8 @@ public class UnfavouriteItemHandler(IValueStore<Item> valueStore,
{
if (valueStore.Value is Item item)
{
await Task.Run(async () =>
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
{
result.State = 0;
await context.SaveChangesAsync();
}
});
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid,
int)>((item.Id, 0)));
}
}
catch
@@ -4,10 +4,10 @@ using Toolkit.Foundation;
namespace Bitvault;
public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<EditEventArgs<(Guid, ItemConfiguration)>, bool>
public class UpdateItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<UpdateEventArgs<(Guid, ItemConfiguration)>, bool>
{
public async Task<bool> Handle(EditEventArgs<(Guid, ItemConfiguration)> args,
public async Task<bool> Handle(UpdateEventArgs<(Guid, ItemConfiguration)> args,
CancellationToken cancellationToken)
{
if (args.Value is (Guid id, ItemConfiguration configuration))
@@ -15,14 +15,12 @@ public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFacto
try
{
string? name = configuration.Name;
using ContainerDbContext context = dbContextFactory.CreateDbContext();
ItemEntry? result = null;
await Task.Run(async () =>
{
result = await context.Set<ItemEntry>().FindAsync(id);
if (result is not null)
{
result.Name = name;
+28
View File
@@ -0,0 +1,28 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
namespace Bitvault;
public class UpdateItemStateHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<UpdateEventArgs<(Guid, int)>, bool>
{
public async Task<bool> Handle(UpdateEventArgs<(Guid, int)> args,
CancellationToken cancellationToken)
{
if (args.Value is (Guid id, int state))
{
await Task.Run(async () =>
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)
{
result.State = state;
await context.SaveChangesAsync();
}
}, cancellationToken);
}
return false;
}
}