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

178 lines
7.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 System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
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 System.Threading.Tasks;
using IO.Swagger.Repositories;
using AutoMapper;
namespace IO.Swagger.Controllers
{
/// <summary>
///
/// </summary>
[ApiController]
public class CurrencyApiController : ControllerBase
{
private readonly ICurrencyRepository repo;
private readonly IMapper mapper;
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"></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)
{
var 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)));
var createdCurr = await repo.CreateCurrency(body, userId);
return createdCurr ? StatusCode(201) : StatusCode(422);
}
/// <summary>
/// Mint additional units of a currency
/// </summary>
/// <param name="body"></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)
{
var 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)));
var 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>
[HttpPost]
[Route("/v1/api/currency/getAll")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetAllCurrencies")]
[ProducesResponseType(typeof(IEnumerable<CurrencyInfoDto>), 200)]
public virtual async Task<IActionResult> GetAllCurrencies()
{
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
var rawCurrencies = await repo.GetAllCurrencies();
var res = new List<CurrencyInfoDto>();
foreach (var raw in rawCurrencies)
{
var c = mapper.Map<CurrencyInfoDto>(raw);
c.IsOwner = raw.UserId == userId;
res.Add(c);
}
return Ok(res);
}
}
}