WIP: Item counts

This commit is contained in:
TheXamlGuy
2024-06-11 22:32:14 +01:00
parent 0fecdef1fe
commit ec7d6611a1
28 changed files with 205 additions and 74 deletions
+52
View File
@@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Toolkit.Foundation;
using Wallet.Data;
namespace Wallet;
public class CountCategoriesHandler(IDbContextFactory<WalletContext> dbContextFactory) :
IHandler<CountEventArgs<ItemCategory>, IReadOnlyCollection<(string, int)>>
{
public async Task<IReadOnlyCollection<(string, int)>> Handle(CountEventArgs<ItemCategory> args,
CancellationToken cancellationToken)
{
using WalletContext context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var stateCounts = await context.Items
.GroupBy(i => i.State)
.Select(g => new
{
g.Key,
Count = g.Count()
})
.ToListAsync(cancellationToken: cancellationToken);
var categoryCounts = await context.Items.Where(x => !string.IsNullOrEmpty(x.Category))
.GroupBy(i => i.Category)
.Select(g => new
{
g.Key,
Count = g.Count()
})
.ToListAsync(cancellationToken: cancellationToken);
int allCount = stateCounts.Where(x => x.Key != 2).Sum(x => x.Count);
int favouritesCount = stateCounts.Where(x => x.Key == 1).Sum(x => x.Count);
int archiveCount = stateCounts.Where(x => x.Key == 2).Sum(x => x.Count);
List<(string, int)> combinedCounts =
[
new("Favourites", favouritesCount),
new("Archive", archiveCount),
new("All", allCount)
];
foreach ((string key, int count) in categoryCounts.Select(x => (x.Key, x.Count)))
{
combinedCounts.Add((key, count));
}
return combinedCounts;
}
}