/* * 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 { /// /// The controller for accessing routes for wallet management /// [ApiController] public class WalletApiController : ControllerBase { private readonly ITransactionRepository transactionRepository; private readonly IMapper mapper; /// /// Initializes a new instance of the class. /// /// The transaction repository. /// The mapper. /// /// transactionRepository /// or /// mapper /// public WalletApiController(ITransactionRepository transactionRepository, IMapper mapper) { this.transactionRepository = transactionRepository ?? throw new ArgumentNullException(nameof(transactionRepository)); this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); } /// /// Get user's wallet balances /// /// Successful response /// Unauthorized [HttpGet] [Route("/v1/api/wallet/balances")] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetUserBalances")] [ProducesResponseType(typeof(IEnumerable), 200)] public virtual async Task GetUserBalances() { string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; if (!int.TryParse(userIdString, out int userId)) return Unauthorized(); List> balances = await transactionRepository.GetBalancesForUser(userId); IEnumerable res = balances.Select(t => new WalletBalanceDto { Currency = mapper.Map(t.Item1), Balance = t.Item2 }); return Ok(res); } /// /// Get user's wallet transactions /// /// Successful response /// Unauthorized [HttpGet] [Route("/v1/api/wallet/transactions")] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetUserTransactions")] [ProducesResponseType(typeof(IEnumerable), 200)] public virtual async Task GetUserTransactions() { string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; if (!int.TryParse(userIdString, out int userId)) return Unauthorized(); List transactions = await transactionRepository.GetTransactionsForUser(userId); return Ok(transactions.Select(mapper.Map)); } /// /// Transfer digital asset to another user /// /// /// Successful transfer /// Bad Request /// Unauthorized [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(); } /// /// Transfer physical currency to another user /// /// /// Successful transfer /// Bad Request /// Unauthorized [HttpPost] [Route("/v1/api/wallet/transferPhysical")] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("TransferPhysicalCurrency")] [ProducesResponseType(typeof(IEnumerable), 400)] [ProducesResponseType(typeof(TransactionReturnCode), 409)] public virtual async Task 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); } } }