Setup wallet and transaction retrieval, better swagger docs, and proper dtos

This commit is contained in:
2023-08-19 16:43:58 -04:00
parent 658bd7ca2a
commit dc9ab74598
16 changed files with 224 additions and 21 deletions

View File

@ -18,6 +18,11 @@ using IO.Swagger.Attributes;
using IO.Swagger.Security;
using Microsoft.AspNetCore.Authorization;
using IO.Swagger.Models.dto;
using System.Linq;
using System.Security.Claims;
using IO.Swagger.Repositories;
using System.Threading.Tasks;
using AutoMapper;
namespace IO.Swagger.Controllers
{
@ -26,26 +31,67 @@ namespace IO.Swagger.Controllers
/// </summary>
[ApiController]
public class WalletApiController : ControllerBase
{
{
private readonly ITransactionRepository transactionRepository;
private readonly IMapper mapper;
public WalletApiController(ITransactionRepository transactionRepository, IMapper mapper)
{
this.transactionRepository = transactionRepository ?? throw new ArgumentNullException(nameof(transactionRepository));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// Get user&#x27;s wallet
/// Get user&#x27;s wallet balances
/// </summary>
/// <response code="200">Successful response</response>
/// <response code="401">Unauthorized</response>
[HttpGet]
[Route("/v1/api/wallet")]
[Route("/v1/api/wallet/balances")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetUserWallet")]
public virtual IActionResult GetUserWallet()
{
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(200);
[SwaggerOperation("GetUserBalances")]
[ProducesResponseType(typeof(WalletBalanceDto), 200)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> GetUserBalances()
{
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
//TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(401);
var transactions = await transactionRepository.GetTransactionsForUser(userId);
var balances = transactions.GroupBy(t => t.Currency)
.Select(g => new WalletBalanceDto
{
Currency = mapper.Map<CurrencyDto>(g.Key),
Balance = g.Sum(t =>t.Amount)
});
return Ok(balances);
}
throw new NotImplementedException();
/// <summary>
/// Get user&#x27;s wallet transactions
/// </summary>
/// <response code="200">Successful response</response>
/// <response code="401">Unauthorized</response>
[HttpGet]
[Route("/v1/api/wallet/transactions")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetUserTransactions")]
[ProducesResponseType(typeof(IEnumerable<TransactionDto>), 200)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> GetUserTransactions()
{
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
var transactions = await transactionRepository.GetTransactionsForUser(userId);
return Ok(transactions.Select(mapper.Map<TransactionDto>));
}
/// <summary>