Worlds fastest rename

This commit is contained in:
TheXamlGuy
2024-06-09 13:47:58 +01:00
parent 727bf877e9
commit 1f777e19cd
71 changed files with 282 additions and 282 deletions
+62
View File
@@ -0,0 +1,62 @@
using Bitvault.Data;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
namespace Bitvault;
public class QueryWalletHandler(IDbContextFactory<WalletContext> dbContextFactory) :
IHandler<QueryEventArgs<Wallet<(string, string)>>, IReadOnlyCollection<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)>>
{
public async Task<IReadOnlyCollection<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)>>
Handle(QueryEventArgs<Wallet<(string, string)>> args,CancellationToken cancellationToken)
{
List<(Guid Id, string? Name, string Category, bool Favourite, bool Archived)> items = [];
if (args.Value is Wallet<(string, string)> Wallet)
{
(string filter, string text) = Wallet.Value;
ExpressionStarter<ItemEntry> predicate =
PredicateBuilder.New<ItemEntry>(true);
if (filter == "All")
{
predicate = predicate.And(x => x.State != 2);
}
if (filter == "Starred")
{
predicate = predicate.And(x => x.State != 2 && x.State == 1);
}
if (filter == "Archive")
{
predicate = predicate.And(x => x.State == 2);
}
if (text is { Length: > 0 })
{
predicate = predicate.And(x => EF.Functions.Like(x.Name, $"%{text}%"));
}
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var results = await context.Set<ItemEntry>()
.Where(predicate)
.Select(x => new
{
x.Id,
x.Name,
x.Category,
Favourite = x.State == 1,
Archived = x.State == 2
}).ToListAsync(cancellationToken: cancellationToken);
foreach (var result in results.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase))
{
items.Add(new(result.Id, result.Name, result.Category, result.Favourite, result.Archived));
}
}
return items;
}
}