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

188 lines
7.9 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>
///
/// </summary>
/// <seealso cref="Microsoft.AspNetCore.Mvc.ControllerBase" />
[ApiController]
public class CurrencyApiController : ControllerBase
{
private readonly ICurrencyRepository repo;
private readonly IMapper mapper;
/// <summary>
/// Initializes a new instance of the <see cref="CurrencyApiController"/> class.
/// </summary>
/// <param name="repo">The repo.</param>
/// <param name="mapper">The mapper.</param>
/// <exception cref="System.ArgumentNullException">
/// repo
/// or
/// mapper
/// </exception>
public CurrencyApiController(ICurrencyRepository repo, IMapper mapper)
{
this.repo = repo ?? throw new ArgumentNullException(nameof(repo));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// Add a digital asset to the user&#x27;s collection
/// </summary>
/// <param name="body"></param>
/// <response code="201">Successful asset addition</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
[HttpPost]
[Route("/v1/api/currency/addAsset")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("AddDigitalAssetToCollection")]
public virtual IActionResult AddDigitalAssetToCollection([FromBody] CurrencyAddAssetBody body)
{
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201);
//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>
/// Create a new collection of digital assets owned by the user
/// </summary>
/// <param name="body"></param>
/// <response code="201">Successful collection creation</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
[HttpPost]
[Route("/v1/api/currency/createCollection")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("CreateAssetCollection")]
public virtual IActionResult CreateAssetCollection([FromBody] CurrencyCreateCollectionBody body)
{
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201);
//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>
/// Create a new currency type
/// </summary>
/// <param name="body">The currency to create</param>
/// <response code="201">Currency type created successfully</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
/// <response code="422">Unprocessable Content</response>
[HttpPost]
[Route("/v1/api/currency/create")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("CreateCurrency")]
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
public virtual async Task<IActionResult> CreateCurrency([FromBody] CurrencyCreateBody 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)));
bool createdCurr = await repo.CreateCurrency(body, userId);
return createdCurr ? StatusCode(201) : StatusCode(422);
}
/// <summary>
/// Mint additional units of a currency
/// </summary>
/// <param name="body">The information on the currency to mint</param>
/// <response code="200">Successful minting</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
/// <response code="409">Conflict - User is not owner or currency does not exist</response>
[HttpPost]
[Route("/v1/api/currency/mint")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("MintCurrency")]
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
public virtual async Task<IActionResult> MintCurrency([FromBody] CurrencyMintBody 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)));
bool minted = await repo.MintCurrency(body, userId);
return minted ? Ok() : StatusCode(409);
}
/// <summary>
/// Get all Currencies
/// </summary>
/// <response code="200">Returns all known currencies</response>
/// <response code="401">Unauthorized</response>
[HttpGet]
[Route("/v1/api/currency/getAll")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetAllCurrencies")]
[ProducesResponseType(typeof(IEnumerable<CurrencyInfoDto>), 200)]
public virtual async Task<IActionResult> GetAllCurrencies()
{
string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
List<Models.db.Currency> rawCurrencies = await repo.GetAllCurrencies();
List<CurrencyInfoDto> res = new();
foreach (Models.db.Currency raw in rawCurrencies)
{
CurrencyInfoDto c = mapper.Map<CurrencyInfoDto>(raw);
c.IsOwner = raw.UserId == userId;
res.Add(c);
}
return Ok(res);
}
}
}