/*
* 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
{
///
///
///
[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));
}
///
/// 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
///
///
/// 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)
{
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);
}
///
/// Mint additional units of a currency
///
///
/// 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)
{
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);
}
///
/// Get all Currencies
///
/// Returns all known currencies
/// Unauthorized
[HttpPost]
[Route("/v1/api/currency/getAll")]
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetAllCurrencies")]
[ProducesResponseType(typeof(IEnumerable), 200)]
public virtual async Task 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();
foreach (var raw in rawCurrencies)
{
var c = mapper.Map(raw);
c.IsOwner = raw.UserId == userId;
res.Add(c);
}
return Ok(res);
}
}
}