Include uncategorized spending and show all categories on dashboard
Remove CategoryId.HasValue filter so uncategorized transactions appear in the spending breakdown. Remove Take(N) limit to show all categories. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -52,20 +52,20 @@ public class DashboardService : IDashboardService
|
|||||||
var changePercent = lastMonthSpending == 0 ? 0 : ((thisMonthSpending - lastMonthSpending) / lastMonthSpending) * 100;
|
var changePercent = lastMonthSpending == 0 ? 0 : ((thisMonthSpending - lastMonthSpending) / lastMonthSpending) * 100;
|
||||||
|
|
||||||
var spendingByCategory = await _db.Transactions
|
var spendingByCategory = await _db.Transactions
|
||||||
.Where(t => t.Account.UserId == userId && t.Date >= startOfMonth && t.Amount < 0 && !t.IsVoid && t.CategoryId.HasValue)
|
.Where(t => t.Account.UserId == userId && t.Date >= startOfMonth && t.Amount < 0 && !t.IsVoid)
|
||||||
.GroupBy(t => t.CategoryId)
|
.GroupBy(t => t.CategoryId)
|
||||||
.Select(g => new { CategoryId = g.Key, Total = -g.Sum(t => t.Amount) })
|
.Select(g => new { CategoryId = g.Key, Total = -g.Sum(t => t.Amount) })
|
||||||
.OrderByDescending(x => x.Total)
|
.OrderByDescending(x => x.Total)
|
||||||
.Take(5)
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
var categoryIds = spendingByCategory.Where(x => x.CategoryId.HasValue).Select(x => x.CategoryId!.Value);
|
||||||
var categoryNames = await _db.Categories
|
var categoryNames = await _db.Categories
|
||||||
.Where(c => spendingByCategory.Select(x => x.CategoryId).Contains(c.Id))
|
.Where(c => categoryIds.Contains(c.Id))
|
||||||
.ToDictionaryAsync(c => c.Id, c => c.Name);
|
.ToDictionaryAsync(c => c.Id, c => c.Name);
|
||||||
|
|
||||||
var totalSpending = spendingByCategory.Sum(x => x.Total);
|
var totalSpending = spendingByCategory.Sum(x => x.Total);
|
||||||
var topCategories = spendingByCategory.Select(x => new CategorySpending(
|
var topCategories = spendingByCategory.Select(x => new CategorySpending(
|
||||||
categoryNames.GetValueOrDefault(x.CategoryId!.Value, "Unknown"),
|
x.CategoryId.HasValue ? categoryNames.GetValueOrDefault(x.CategoryId.Value, "Unknown") : "Uncategorized",
|
||||||
x.Total,
|
x.Total,
|
||||||
totalSpending > 0 ? (x.Total / totalSpending) * 100 : 0,
|
totalSpending > 0 ? (x.Total / totalSpending) * 100 : 0,
|
||||||
null)).ToList();
|
null)).ToList();
|
||||||
|
|||||||
Reference in New Issue
Block a user