Files
bank-backend/src/IO.Swagger/Controllers/WalletApi.cs

152 lines
6.4 KiB
C#

/*
* T&J Central Bank API
*
* API documentation for T&J Central Bank's digital wallets
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
using AutoMapper;
using IO.Swagger.Attributes;
using IO.Swagger.Models.RequestDto;
using IO.Swagger.Models.ResponseDto;
using IO.Swagger.Repositories;
using IO.Swagger.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace IO.Swagger.Controllers
{
/// <summary>
/// The controller for accessing routes for wallet management
/// </summary>
[ApiController]
public class WalletApiController : ControllerBase
{
private readonly ITransactionRepository transactionRepository;
private readonly IMapper mapper;
/// <summary>
/// Initializes a new instance of the <see cref="WalletApiController"/> class.
/// </summary>
/// <param name="transactionRepository">The transaction repository.</param>
/// <param name="mapper">The mapper.</param>
/// <exception cref="System.ArgumentNullException">
/// transactionRepository
/// or
/// mapper
/// </exception>
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 balances
/// </summary>
/// <response code="200">Successful response</response>
/// <response code="401">Unauthorized</response>
[HttpGet]
[Route("/v1/api/wallet/balances")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetUserBalances")]
[ProducesResponseType(typeof(IEnumerable<WalletBalanceDto>), 200)]
public virtual async Task<IActionResult> GetUserBalances()
{
string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
List<Tuple<Models.db.Currency, float>> balances = await transactionRepository.GetBalancesForUser(userId);
IEnumerable<WalletBalanceDto> res = balances.Select(t => new WalletBalanceDto
{
Currency = mapper.Map<CurrencyDto>(t.Item1),
Balance = t.Item2
});
return Ok(res);
}
/// <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)]
public virtual async Task<IActionResult> GetUserTransactions()
{
string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
List<Models.db.Transaction> transactions = await transactionRepository.GetTransactionsForUser(userId);
return Ok(transactions.Select(mapper.Map<TransactionDto>));
}
/// <summary>
/// Transfer digital asset to another user
/// </summary>
/// <param name="body"></param>
/// <response code="200">Successful transfer</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
[HttpPost]
[Route("/v1/api/wallet/transferDigital")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("TransferDigitalAsset")]
public virtual IActionResult TransferDigitalAsset([FromBody] WalletTransferDigitalBody body)
{
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(200);
//TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(400);
//TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(401);
throw new NotImplementedException();
}
/// <summary>
/// Transfer physical currency to another user
/// </summary>
/// <param name="body"></param>
/// <response code="200">Successful transfer</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
[HttpPost]
[Route("/v1/api/wallet/transferPhysical")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("TransferPhysicalCurrency")]
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(typeof(TransactionReturnCode), 409)]
public virtual async Task<IActionResult> TransferPhysicalCurrency([FromBody] WalletTransferPhysicalBody body)
{
string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
if (!ModelState.IsValid)
return BadRequest(ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)));
TransactionReturnCode transactionResult = await transactionRepository.TransferPhysical(body, userId);
return transactionResult == TransactionReturnCode.Success ? Ok() : StatusCode(409, (int) transactionResult);
}
}
}