namespace AAIntegration.SimmonsBank.API.Controllers; using Microsoft.AspNetCore.Mvc; using AAIntegration.SimmonsBank.API.Models.Accounts; using AAIntegration.SimmonsBank.API.Services; using System.Collections.Generic; using AAIntegration.SimmonsBank.API.Entities; using Microsoft.AspNetCore.Authorization; using System.Security.Claims; using System.Threading.Tasks; using AAIntegration.SimmonsBank.API.Models.Transactions; [Authorize] [ApiController] [Route("[controller]")] public class AccountsController : ControllerBase { private IUserService _userService; private readonly ILogger _logger; private IPuppeteerService _puppeteerService; public AccountsController( IUserService userService, ILogger logger, IPuppeteerService puppeteerService) { _userService = userService; _logger = logger; _puppeteerService = puppeteerService; } [HttpGet] public async Task GetAllAsync() { List accounts = await _puppeteerService.GetAccounts(GetCurrentUser()); return Ok(accounts); } [HttpGet("{account_guid}")] public async Task GetByGUIDAsync(string account_guid) { return Ok(await GetAccountAsync(account_guid)); } [HttpGet("{account_guid}/transactions")] public async Task GetTransactionsAsync(string account_guid, uint offset = 0, uint limit = 500) { List transactions = await _puppeteerService.GetTransactions(GetCurrentUser(), (await GetAccountAsync(account_guid)).Id, offset, limit); return Ok(transactions); } // Helpers private async Task GetAccountAsync(string account_guid) { List accounts = await _puppeteerService.GetAccounts(GetCurrentUser()); AccountDTO account = accounts.FirstOrDefault(a => a.Id == account_guid) ?? throw new KeyNotFoundException("Account not found"); return account; } private User GetCurrentUser() { string apiKey = User.FindFirstValue(ClaimTypes.NameIdentifier); return _userService.GetUser(apiKey); } }