Added a route to get a summary of all currencies
This commit is contained in:
@ -22,6 +22,7 @@ using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using IO.Swagger.Repositories;
|
||||
using AutoMapper;
|
||||
|
||||
namespace IO.Swagger.Controllers
|
||||
{
|
||||
@ -32,10 +33,12 @@ namespace IO.Swagger.Controllers
|
||||
public class CurrencyApiController : ControllerBase
|
||||
{
|
||||
private readonly ICurrencyRepository repo;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public CurrencyApiController(ICurrencyRepository repo)
|
||||
public CurrencyApiController(ICurrencyRepository repo, IMapper mapper)
|
||||
{
|
||||
this.repo = repo ?? throw new ArgumentNullException(nameof(repo));
|
||||
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -103,10 +106,7 @@ namespace IO.Swagger.Controllers
|
||||
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("CreateCurrency")]
|
||||
[ProducesResponseType(201)]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
|
||||
[ProducesResponseType(401)]
|
||||
[ProducesResponseType(422)]
|
||||
public virtual async Task<IActionResult> CreateCurrency([FromBody]CurrencyCreateBody body)
|
||||
{
|
||||
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
@ -133,10 +133,7 @@ namespace IO.Swagger.Controllers
|
||||
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("MintCurrency")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
|
||||
[ProducesResponseType(401)]
|
||||
[ProducesResponseType(409)]
|
||||
public virtual async Task<IActionResult> MintCurrency([FromBody]CurrencyMintBody body)
|
||||
{
|
||||
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
@ -148,5 +145,33 @@ namespace IO.Swagger.Controllers
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user