Added a route to get a summary of all currencies

This commit is contained in:
2023-08-19 18:25:17 -04:00
parent ff327d0a03
commit 1fedd4d016
7 changed files with 58 additions and 12 deletions

View File

@ -63,7 +63,6 @@ namespace IO.Swagger.Controllers
[ValidateModelState] [ValidateModelState]
[SwaggerOperation("GetUserDetails")] [SwaggerOperation("GetUserDetails")]
[ProducesResponseType(typeof(UserDto), 200)] [ProducesResponseType(typeof(UserDto), 200)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> GetUserDetails() public virtual async Task<IActionResult> GetUserDetails()
{ {
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
@ -86,7 +85,6 @@ namespace IO.Swagger.Controllers
[SwaggerOperation("LoginUser")] [SwaggerOperation("LoginUser")]
[ProducesResponseType(typeof(TokenDto), 200)] [ProducesResponseType(typeof(TokenDto), 200)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)] [ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> LoginUser([FromBody]AuthLoginBody body) public virtual async Task<IActionResult> LoginUser([FromBody]AuthLoginBody body)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
@ -109,7 +107,6 @@ namespace IO.Swagger.Controllers
[SwaggerOperation("RegisterUser")] [SwaggerOperation("RegisterUser")]
[ProducesResponseType(typeof(TokenDto), 200)] [ProducesResponseType(typeof(TokenDto), 200)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)] [ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(409)]
public async Task<IActionResult> RegisterUser([FromBody]AuthRegisterBody body) public async Task<IActionResult> RegisterUser([FromBody]AuthRegisterBody body)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)

View File

@ -22,6 +22,7 @@ using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using IO.Swagger.Repositories; using IO.Swagger.Repositories;
using AutoMapper;
namespace IO.Swagger.Controllers namespace IO.Swagger.Controllers
{ {
@ -32,10 +33,12 @@ namespace IO.Swagger.Controllers
public class CurrencyApiController : ControllerBase public class CurrencyApiController : ControllerBase
{ {
private readonly ICurrencyRepository repo; 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.repo = repo ?? throw new ArgumentNullException(nameof(repo));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
} }
/// <summary> /// <summary>
@ -103,10 +106,7 @@ namespace IO.Swagger.Controllers
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState] [ValidateModelState]
[SwaggerOperation("CreateCurrency")] [SwaggerOperation("CreateCurrency")]
[ProducesResponseType(201)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)] [ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(401)]
[ProducesResponseType(422)]
public virtual async Task<IActionResult> CreateCurrency([FromBody]CurrencyCreateBody body) public virtual async Task<IActionResult> CreateCurrency([FromBody]CurrencyCreateBody body)
{ {
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
@ -133,10 +133,7 @@ namespace IO.Swagger.Controllers
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState] [ValidateModelState]
[SwaggerOperation("MintCurrency")] [SwaggerOperation("MintCurrency")]
[ProducesResponseType(200)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)] [ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(401)]
[ProducesResponseType(409)]
public virtual async Task<IActionResult> MintCurrency([FromBody]CurrencyMintBody body) public virtual async Task<IActionResult> MintCurrency([FromBody]CurrencyMintBody body)
{ {
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; 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); var minted = await repo.MintCurrency(body, userId);
return minted ? Ok() : StatusCode(409); 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);
}
} }
} }

View File

@ -53,7 +53,7 @@ namespace IO.Swagger.Controllers
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState] [ValidateModelState]
[SwaggerOperation("GetUserBalances")] [SwaggerOperation("GetUserBalances")]
[ProducesResponseType(typeof(WalletBalanceDto), 200)] [ProducesResponseType(typeof(IEnumerable<WalletBalanceDto>), 200)]
public virtual async Task<IActionResult> GetUserBalances() public virtual async Task<IActionResult> GetUserBalances()
{ {
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;

View File

@ -0,0 +1,9 @@
namespace IO.Swagger.Models.dto
{
public class CurrencyInfoDto : CurrencyDto
{
public UserDto CurrencyOwner { get; set; }
public bool IsOwner { get; set; }
public float AmountInCirculation { get; set; }
}
}

View File

@ -3,6 +3,7 @@ using IO.Swagger.Models.dto;
using IO.Swagger.Services; using IO.Swagger.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,6 +18,11 @@ namespace IO.Swagger.Repositories
this.context = context ?? throw new ArgumentNullException(nameof(context)); this.context = context ?? throw new ArgumentNullException(nameof(context));
} }
public async Task<List<Currency>> GetAllCurrencies()
{
return await context.Currencies.Include(c => c.User).Include(c=>c.Transactions).ToListAsync();
}
public async Task<bool> CreateCurrency(CurrencyCreateBody request, int userId) public async Task<bool> CreateCurrency(CurrencyCreateBody request, int userId)
{ {
request.Symbol = request.Symbol.Trim(); request.Symbol = request.Symbol.Trim();

View File

@ -1,4 +1,6 @@
using IO.Swagger.Models.dto; using IO.Swagger.Models.db;
using IO.Swagger.Models.dto;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IO.Swagger.Repositories namespace IO.Swagger.Repositories
@ -6,6 +8,7 @@ namespace IO.Swagger.Repositories
public interface ICurrencyRepository public interface ICurrencyRepository
{ {
Task<bool> CreateCurrency(CurrencyCreateBody request, int userId); Task<bool> CreateCurrency(CurrencyCreateBody request, int userId);
Task<List<Currency>> GetAllCurrencies();
Task<bool> MintCurrency(CurrencyMintBody request, int userId); Task<bool> MintCurrency(CurrencyMintBody request, int userId);
} }
} }

View File

@ -1,6 +1,7 @@
using AutoMapper; using AutoMapper;
using IO.Swagger.Models.db; using IO.Swagger.Models.db;
using IO.Swagger.Models.dto; using IO.Swagger.Models.dto;
using System.Linq;
namespace IO.Swagger.Services namespace IO.Swagger.Services
{ {
@ -11,6 +12,11 @@ namespace IO.Swagger.Services
CreateMap<User, UserDto>(); CreateMap<User, UserDto>();
CreateMap<Currency, CurrencyDto>(); CreateMap<Currency, CurrencyDto>();
CreateMap<Transaction, TransactionDto>(); CreateMap<Transaction, TransactionDto>();
CreateMap<Currency, CurrencyInfoDto>().ForMember(ci => ci.AmountInCirculation,
mapper => mapper.MapFrom(c => c.Transactions.Where(t => t.ToUserId == t.FromUserId).Sum(t => t.Amount)))
.ForMember(ci => ci.CurrencyOwner,
mapper => mapper.MapFrom(c => c.User));
} }
} }
} }