/*
* 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
{
///
///
///
///
[ApiController]
public class CurrencyApiController : ControllerBase
{
private readonly ICurrencyRepository repo;
private readonly IMapper mapper;
///
/// Initializes a new instance of the class.
///
/// The repo.
/// The mapper.
///
/// repo
/// or
/// mapper
///
public CurrencyApiController(ICurrencyRepository repo, IMapper mapper)
{
this.repo = repo ?? throw new ArgumentNullException(nameof(repo));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
///
/// Add a digital asset to the user's collection
///
///
/// Successful asset addition
/// Bad Request
/// Unauthorized
[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();
}
///
/// Create a new collection of digital assets owned by the user
///
///
/// Successful collection creation
/// Bad Request
/// Unauthorized
[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();
}
///
/// Create a new currency type
///
/// The currency to create
/// Currency type created successfully
/// Bad Request
/// Unauthorized
/// Unprocessable Content
[HttpPost]
[Route("/v1/api/currency/create")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("CreateCurrency")]
[ProducesResponseType(typeof(IEnumerable), 400)]
public virtual async Task 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);
}
///
/// Mint additional units of a currency
///
/// The information on the currency to mint
/// Successful minting
/// Bad Request
/// Unauthorized
/// Conflict - User is not owner or currency does not exist
[HttpPost]
[Route("/v1/api/currency/mint")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("MintCurrency")]
[ProducesResponseType(typeof(IEnumerable), 400)]
public virtual async Task 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);
}
///
/// Get all Currencies
///
/// Returns all known currencies
/// Unauthorized
[HttpGet]
[Route("/v1/api/currency/getAll")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetAllCurrencies")]
[ProducesResponseType(typeof(IEnumerable), 200)]
public virtual async Task GetAllCurrencies()
{
string userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
List rawCurrencies = await repo.GetAllCurrencies();
List res = new();
foreach (Models.db.Currency raw in rawCurrencies)
{
CurrencyInfoDto c = mapper.Map(raw);
c.IsOwner = raw.UserId == userId;
res.Add(c);
}
return Ok(res);
}
}
}