66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
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<AccountsController> _logger;
|
|
private IPuppeteerService _puppeteerService;
|
|
|
|
public AccountsController(
|
|
IUserService userService,
|
|
ILogger<AccountsController> logger,
|
|
IPuppeteerService puppeteerService)
|
|
{
|
|
_userService = userService;
|
|
_logger = logger;
|
|
_puppeteerService = puppeteerService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAllAsync()
|
|
{
|
|
List<AccountDTO> accounts = await _puppeteerService.GetAccounts(GetCurrentUser());
|
|
return Ok(accounts);
|
|
}
|
|
|
|
[HttpGet("{account_guid}")]
|
|
public async Task<IActionResult> GetByGUIDAsync(string account_guid)
|
|
{
|
|
return Ok(await GetAccountAsync(account_guid));
|
|
}
|
|
|
|
[HttpGet("{account_guid}/transactions")]
|
|
public async Task<IActionResult> GetTransactionsAsync(string account_guid, uint offset = 0, uint limit = 500)
|
|
{
|
|
List<TransactionDTO> transactions = await _puppeteerService.GetTransactions(GetCurrentUser(), (await GetAccountAsync(account_guid)).Id, offset, limit);
|
|
return Ok(transactions);
|
|
}
|
|
|
|
// Helpers
|
|
|
|
private async Task<AccountDTO> GetAccountAsync(string account_guid)
|
|
{
|
|
List<AccountDTO> 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);
|
|
}
|
|
} |