diff --git a/MoneyMap/Controllers/AccountsController.cs b/MoneyMap/Controllers/AccountsController.cs new file mode 100644 index 0000000..2a5e188 --- /dev/null +++ b/MoneyMap/Controllers/AccountsController.cs @@ -0,0 +1,65 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MoneyMap.Data; + +namespace MoneyMap.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class AccountsController : ControllerBase +{ + private readonly MoneyMapContext _db; + + public AccountsController(MoneyMapContext db) => _db = db; + + [HttpGet] + public async Task List() + { + var accounts = await _db.Accounts + .Include(a => a.Cards) + .Include(a => a.Transactions) + .OrderBy(a => a.Institution).ThenBy(a => a.Last4) + .Select(a => new + { + a.Id, + a.Institution, + a.Last4, + a.Owner, + Label = a.DisplayLabel, + TransactionCount = a.Transactions.Count, + CardCount = a.Cards.Count + }) + .ToListAsync(); + + return Ok(accounts); + } + + [HttpGet("cards")] + public async Task ListCards([FromQuery] int? accountId = null) + { + var q = _db.Cards + .Include(c => c.Account) + .Include(c => c.Transactions) + .AsQueryable(); + + if (accountId.HasValue) + q = q.Where(c => c.AccountId == accountId.Value); + + var cards = await q + .OrderBy(c => c.Owner).ThenBy(c => c.Last4) + .Select(c => new + { + c.Id, + c.Issuer, + c.Last4, + c.Owner, + Label = c.DisplayLabel, + Account = c.Account != null ? c.Account.Institution + " " + c.Account.Last4 : null, + AccountId = c.AccountId, + TransactionCount = c.Transactions.Count + }) + .ToListAsync(); + + return Ok(cards); + } +}