Add ability to fully delete items from the DB

This commit is contained in:
TheXamlGuy
2024-06-23 09:58:19 +01:00
parent 95de9d187f
commit db20ee15cc
18 changed files with 98 additions and 22 deletions
+30
View File
@@ -0,0 +1,30 @@
using Toolkit.Foundation;
namespace Wallet;
public class ConfirmDeleteItemHandler(IDecoratorService<Item<(Guid, string)>> decoratorService,
ICache<Item<(Guid, string)>> cache,
IMediator mediator,
IPublisher publisher) :
INotificationHandler<DeleteEventArgs<Item>>
{
public async Task Handle(DeleteEventArgs<Item> args)
{
try
{
if (decoratorService.Service is Item<(Guid, string)> item)
{
(Guid id, string name) = item.Value;
await mediator.Handle<DeleteEventArgs<Item<Guid>>,
bool>(new DeleteEventArgs<Item<Guid>>(new Item<Guid>(id)));
cache.Add(item);
publisher.Publish(Changed.As<Item>());
}
}
catch
{
}
}
}
+26
View File
@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
using Wallet.Data;
namespace Wallet;
public class DeleteItemHandler(IDbContextFactory<WalletContext> dbContextFactory) :
IHandler<DeleteEventArgs<Item<Guid>>, bool>
{
public async Task<bool> Handle(DeleteEventArgs<Item<Guid>> args,
CancellationToken cancellationToken)
{
if (args.Sender is Item<Guid> item)
{
Guid id = item.Value;
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)
{
context.Items.Remove(result);
await context.SaveChangesAsync(cancellationToken);
}
}
return false;
}
}
+7 -4
View File
@@ -23,6 +23,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
INotificationHandler<UnarchiveEventArgs<Item>>,
INotificationHandler<FavouriteEventArgs<Item>>,
INotificationHandler<UnfavouriteEventArgs<Item>>,
INotificationHandler<DeleteEventArgs<Item>>,
INotificationHandler<NotifyEventArgs<ItemHeader<string>>>,
IKeyed<Guid>,
ISelectable,
@@ -43,15 +44,14 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
[ObservableProperty]
private Guid id = id;
[ObservableProperty]
private bool isSelected = isSelected;
[ObservableProperty]
private string? name = name;
[ObservableProperty]
private string named = $"{named}";
[ObservableProperty]
private bool isSelected = isSelected;
public IContentTemplate Template { get; set; } = template;
public Task Handle(ArchiveEventArgs<Item> args) =>
@@ -75,4 +75,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
return Task.CompletedTask;
}
public Task Handle(DeleteEventArgs<Item> args) =>
Task.Run(Dispose);
}
+1
View File
@@ -108,6 +108,7 @@ public partial class ItemViewModel :
Publisher.Publish(Notify.As(Factory.Create<ItemCommandHeaderCollection>(new List<IDisposable>
{
Factory.Create<UnarchiveItemActionViewModel>(),
Factory.Create<DeleteItemActionViewModel>(),
})));
}
else
+1 -1
View File
@@ -12,7 +12,7 @@ public class UpdateItemHander(IDbContextFactory<WalletContext> dbContextFactory)
public async Task<bool> Handle(UpdateEventArgs<Item<(Guid, string, ItemConfiguration)>> args,
CancellationToken cancellationToken)
{
if (args.Value is Item<(Guid, string, ItemConfiguration)> item)
if (args.Sender is Item<(Guid, string, ItemConfiguration)> item)
{
(Guid id, string name, ItemConfiguration configuration) = item.Value;
+1 -1
View File
@@ -10,7 +10,7 @@ public class UpdateItemStateHandler(IDbContextFactory<WalletContext> dbContextFa
public async Task<bool> Handle(UpdateEventArgs<(Guid, int)> args,
CancellationToken cancellationToken)
{
if (args.Value is (Guid id, int state))
if (args.Sender is (Guid id, int state))
{
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)