Worlds fastest rename
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user