From db1d96476b388a15c680eecddcb739ccaf3ed033 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 20 Apr 2026 20:35:57 -0400 Subject: [PATCH] feat(api): add DashboardController with overview and monthly-trend endpoints Co-Authored-By: Claude Opus 4.6 --- MoneyMap/Controllers/DashboardController.cs | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 MoneyMap/Controllers/DashboardController.cs diff --git a/MoneyMap/Controllers/DashboardController.cs b/MoneyMap/Controllers/DashboardController.cs new file mode 100644 index 0000000..e108024 --- /dev/null +++ b/MoneyMap/Controllers/DashboardController.cs @@ -0,0 +1,72 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MoneyMap.Data; +using MoneyMap.Services; + +namespace MoneyMap.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class DashboardController : ControllerBase +{ + private readonly IDashboardService _dashboardService; + private readonly MoneyMapContext _db; + + public DashboardController(IDashboardService dashboardService, MoneyMapContext db) + { + _dashboardService = dashboardService; + _db = db; + } + + [HttpGet] + public async Task Get( + [FromQuery] int? topCategoriesCount = null, + [FromQuery] int? recentTransactionsCount = null) + { + var data = await _dashboardService.GetDashboardDataAsync( + topCategoriesCount ?? 8, + recentTransactionsCount ?? 20); + + return Ok(data); + } + + [HttpGet("monthly-trend")] + public async Task MonthlyTrend( + [FromQuery] int? months = null, + [FromQuery] string? category = null) + { + var monthCount = months ?? 6; + var endDate = DateTime.Today; + var startDate = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(-(monthCount - 1)); + + var q = _db.Transactions + .Where(t => t.Date >= startDate && t.Date <= endDate) + .Where(t => t.Amount < 0) + .Where(t => t.TransferToAccountId == null) + .ExcludeTransfers(); + + if (!string.IsNullOrWhiteSpace(category)) + q = q.Where(t => t.Category == category); + + var monthly = await q + .GroupBy(t => new { t.Date.Year, t.Date.Month }) + .Select(g => new + { + Year = g.Key.Year, + Month = g.Key.Month, + Total = g.Sum(t => Math.Abs(t.Amount)), + Count = g.Count() + }) + .OrderBy(x => x.Year).ThenBy(x => x.Month) + .ToListAsync(); + + var result = monthly.Select(m => new + { + Period = $"{m.Year}-{m.Month:D2}", + m.Total, + m.Count + }).ToList(); + + return Ok(new { Category = category ?? "All Spending", Months = result }); + } +}