Added a route to get a summary of all currencies
This commit is contained in:
@ -63,7 +63,6 @@ namespace IO.Swagger.Controllers
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("GetUserDetails")]
|
||||
[ProducesResponseType(typeof(UserDto), 200)]
|
||||
[ProducesResponseType(401)]
|
||||
public virtual async Task<IActionResult> GetUserDetails()
|
||||
{
|
||||
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
@ -86,7 +85,6 @@ namespace IO.Swagger.Controllers
|
||||
[SwaggerOperation("LoginUser")]
|
||||
[ProducesResponseType(typeof(TokenDto), 200)]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
|
||||
[ProducesResponseType(401)]
|
||||
public virtual async Task<IActionResult> LoginUser([FromBody]AuthLoginBody body)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
@ -109,7 +107,6 @@ namespace IO.Swagger.Controllers
|
||||
[SwaggerOperation("RegisterUser")]
|
||||
[ProducesResponseType(typeof(TokenDto), 200)]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
|
||||
[ProducesResponseType(409)]
|
||||
public async Task<IActionResult> RegisterUser([FromBody]AuthRegisterBody body)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ namespace IO.Swagger.Controllers
|
||||
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("GetUserBalances")]
|
||||
[ProducesResponseType(typeof(WalletBalanceDto), 200)]
|
||||
[ProducesResponseType(typeof(IEnumerable<WalletBalanceDto>), 200)]
|
||||
public virtual async Task<IActionResult> GetUserBalances()
|
||||
{
|
||||
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
|
||||
9
src/IO.Swagger/Models/dto/CurrencyInfoDto.cs
Normal file
9
src/IO.Swagger/Models/dto/CurrencyInfoDto.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ using IO.Swagger.Models.dto;
|
||||
using IO.Swagger.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -17,6 +18,11 @@ namespace IO.Swagger.Repositories
|
||||
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)
|
||||
{
|
||||
request.Symbol = request.Symbol.Trim();
|
||||
|
||||
@ -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;
|
||||
|
||||
namespace IO.Swagger.Repositories
|
||||
@ -6,6 +8,7 @@ namespace IO.Swagger.Repositories
|
||||
public interface ICurrencyRepository
|
||||
{
|
||||
Task<bool> CreateCurrency(CurrencyCreateBody request, int userId);
|
||||
Task<List<Currency>> GetAllCurrencies();
|
||||
Task<bool> MintCurrency(CurrencyMintBody request, int userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using IO.Swagger.Models.db;
|
||||
using IO.Swagger.Models.dto;
|
||||
using System.Linq;
|
||||
|
||||
namespace IO.Swagger.Services
|
||||
{
|
||||
@ -11,6 +12,11 @@ namespace IO.Swagger.Services
|
||||
CreateMap<User, UserDto>();
|
||||
CreateMap<Currency, CurrencyDto>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user