feat(api): add DashboardController with overview and monthly-trend endpoints
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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<IActionResult> 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<IActionResult> 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user