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